Best JavaScript code snippet using unexpected
tests.js
Source:tests.js
1/* 2 Frist should install npm init, mocha chai,3next must do the following actions4Run > Open Configuration > click "Add Configuration..." button > click on the "Mocha Tests" and configurate the addons!5"tdd" => change it to "bdd"6"test" => change it to "tests"7*/8let SkiResort = require('./solution');9let assert = require("chai").assert;10describe("SkiResort functionality", () => {11 let expectedResult;12 let actualResult;13 let resort;14 let willThrow;15 beforeEach(() => {16 resort = new SkiResort("NewHope");17 expectedResult = '';18 actualResult = '';19 willThrow;20 });21 describe("constructor functionality", () => {22 it("creates a new class object with given name", () => {23 const newResort = new SkiResort("Funn");24 assert.equal(newResort.name, "Funn");25 assert.equal(newResort.voters, 0);26 assert.deepEqual(newResort.hotels, []);27 });28 });29 describe("bestHotel getter functionality", () => {30 it("return \"No votes yet\" message if voters counter is 0", () => {31 expectedResult = "No votes yet";32 actualResult = resort.bestHotel;33 assert.equal(expectedResult, actualResult)34 });35 it("returns best hotel in resort", () => {36 resort.build("Rila", 100);37 resort.build("Pogled", 80);38 resort.leave("Rila", 2, 5);39 resort.leave("Pogled", 2, 50);40 expectedResult = "Best hotel is Pogled with grade 100. Available beds: 82";41 actualResult = resort.bestHotel;42 assert.equal(expectedResult, actualResult);43 });44 });45 describe("build method functionality", () => {46 it("trows an error, if invalid name is passed in", () => {47 willThrow = () => resort.build('', 3 );48 assert.throws(willThrow, "Invalid input"); 49 });50 it("trows an error, if invalid number of beds is passed in", () => {51 willThrow = () => resort.build('Rila', 0 );52 assert.throws(willThrow, "Invalid input");53 });54 it("return propper message", () => {55 actualResult = resort.build("Rila", 100);56 expectedResult = `Successfully built new hotel - Rila`;57 58 assert.equal(expectedResult, actualResult);59 });60 it("adds a new bulded hotel to hotels list", () => {61 resort.build('Rila', 100);62 expectedResult = 1;63 actualResult = resort.hotels.length;64 assert.equal(expectedResult, actualResult);65 })66 });67 describe("book method functionality", () => {68 it("throws a new error, if invalid hotel name is passed in", () => {69 willThrow = () => resort.book('', 4);70 assert.throws(willThrow, "Invalid input"); 71 });72 it("trows a new error, if invalid number of beds is passed in", () => {73 willThrow = () => resort.book("Rila", 0);74 assert.throws(willThrow, "Invalid input"); 75 });76 it("throws a new errror, if there is no such a hotel", () => {77 resort.build("Rila", 100);78 willThrow = () => resort.book("Plam", 3);79 assert.throws(willThrow, "There is no such hotel")80 });81 it("throws a new error, if there are not free beds", () => {82 resort.build("Hija", 4);83 willThrow = () => resort.book("Hija", 5);84 assert.throws(willThrow, "There is no free space");85 });86 it("returns a properly message", () => {87 resort.build("Rila", 100);88 expectedResult = "Successfully booked";89 actualResult = resort.book("Rila", 8);90 assert.equal(expectedResult, actualResult);91 });92 it("decrease the number of free beds", () => {93 resort.build("Rila", 100);94 resort.book("Rila", 10);95 let hotel = resort.hotels.find(hotel => hotel.name === "Rila");96 expectedResult = 90;97 actualResult = hotel.beds;98 assert.equal(expectedResult, actualResult);99 });100 });101 describe("leave method functionality", () => {102 it("throws a new error, if invalid name is passed in", () => {103 willThrow = () => resort.leave('', 4, 10);104 assert.throws(willThrow, "Invalid input");105 });106 it("throws a new error, if invalid number of beds is passed in", () => {107 resort.build("Rila", 100);108 willThrow = () => resort.leave('Rila', 0, 10);109 assert.throws(willThrow, "Invalid input");110 });111 it("throws a new error, if there is no such a hotel in hotels list", () => {112 resort.build("Rila", 100);113 willThrow = () => resort.leave('Ra', 4, 10);114 assert.throws(willThrow, "There is no such hotel");115 });116 it("return proper message if all arguments are correct", () => {117 resort.build("Rila", 100);118 resort.book("Rila", 4);119 actualResult = resort.leave("Rila", 4, 10);120 expectedResult = `4 people left Rila hotel`;121 assert.equal(expectedResult, actualResult);122 });123 it("increase number of beds", () => {124 resort.build("Rila", 100);125 resort.book("Rila", 4);126 resort.book("Rila", 4);127 resort.leave("Rila", 4, 10);128 let hotel = resort.hotels.find(hotel => hotel.name === "Rila");129 actualResult = hotel.beds;130 expectedResult = 96;131 assert.equal(expectedResult, actualResult);132 });133 });134 describe("averageGrade method functionality", () => {135 it("returns proper message, if no voters registred", () => {136 expectedResult = "No votes yet";137 actualResult = resort.averageGrade();138 assert.equal(expectedResult, actualResult);139 });140 it("returns average resort grade", () => {141 resort.build("Rila", 100);142 resort.build("Ela", 80);143 resort.build("Bor", 120);144 resort.leave("Rila", 2, 10);145 const rilaPoints = 2 * 10;146 resort.leave("Ela", 4, 2);147 const elaPoints = 4 * 2;148 resort.leave("Bor", 6, 1);149 const borPoints = 6 * 1;150 const avrgGrade = (rilaPoints + elaPoints + borPoints) / 12;151 expectedResult = `Average grade: ${avrgGrade.toFixed(2)}`;152 actualResult = resort.averageGrade();153 assert.equal(expectedResult, actualResult);154 });155 });...
PaymentPackage.js
Source:PaymentPackage.js
1let assert = require("chai").assert;2describe("PaymentPackage functionality", () => {3 let expectedResult;4 let actualResult;5 let pp;6 beforeEach(() => {7 pp = new PaymentPackage("Boat", 25000);8 expectedResult = null;9 actualResult = null;10 });11 describe("constructor functionality", () => {12 it("creates an object by given name and value", () => {13 assert.equal("Boat", pp.name);14 assert.equal(25000, pp.value);15 assert.equal(20, pp.VAT);16 assert.equal(true, pp.active);17 });18 it("throws if not correct atributes are given", () => {19 let willThrow = () => pp = new PaymentPackage(123, 100);20 assert.throws(willThrow, "Name must be a non-empty string");21 willThrow = () => pp = new PaymentPackage("service", "100");22 assert.throws(willThrow, "Value must be a non-negative number");23 });24 });25 describe("name, validator functionality", () => {26 it("throws error in not valud value is set", () => {27 let willThrow = () => pp.name = true;28 assert.throws(willThrow, "Name must be a non-empty string");29 willThrow = () => pp.name = '';30 assert.throws(willThrow, "Name must be a non-empty string");31 });32 it("set the new value to the object propertie", () => {33 pp.name = "Car";34 assert.equal("Car", pp.name)35 assert.equal(pp._name, pp.name);36 });37 });38 describe("value, validator functionality", () => {39 it("throws error in not valud value is set", () => {40 let willThrow = () => pp.value = true;41 assert.throws(willThrow, "Value must be a non-negative number");42 willThrow = () => pp.value = -0.1;43 assert.throws(willThrow, "Value must be a non-negative number");44 });45 it("set the new value to the object propertie", () => {46 pp.value = 25550.22;47 assert.equal(25550.22, pp.value);48 assert.equal(pp._value, pp.value);49 });50 });51 describe("VAT, validator functionality", () => {52 it("throws error in not valud value is set", () => {53 let willThrow = () => pp.VAT = true;54 assert.throws(willThrow, "VAT must be a non-negative number");55 willThrow = () => pp.VAT = -10;56 assert.throws(willThrow, "VAT must be a non-negative number");57 });58 it("set the new VAT to the object propertie", () => {59 pp.VAT = 10;60 assert.equal(10, pp.VAT);61 assert.equal(pp._VAT, pp.VAT);62 });63 });64 describe("active, validator functionality", () => {65 it("throws error in not valud value is set", () => {66 let willThrow = () => pp.active = 5;67 assert.throws(willThrow, "Active status must be a boolean");68 willThrow = () => pp.active = "true";69 assert.throws(willThrow, "Active status must be a boolean");70 });71 it("set the new active value, to the object propertie", () => {72 pp.active = false;73 assert.equal(false, pp.active);74 75 pp.active = true;76 assert.equal(true, pp.active);77 78 assert.equal(pp._active, pp.active);79 });80 });81 describe("toString method functionality", () => {82 it("returns properly information if package is active", () => {83 expectedResult = [84 "Package: Boat",85 "- Value (excl. VAT): 25000",86 "- Value (VAT 20%): 30000"87 ];88 actualResult = pp.toString();89 assert.equal(expectedResult.join('\n'), actualResult)90 });91 it("returns properly information if package is not active", () => {92 pp.active = false;93 expectedResult = [94 "Package: Boat (inactive)",95 "- Value (excl. VAT): 25000",96 "- Value (VAT 20%): 30000"97 ];98 actualResult = pp.toString();99 assert.equal(expectedResult.join('\n'), actualResult)100 });101 it('if VAT and value are 0', () => {102 pp = new PaymentPackage('test', 0);103 pp.VAT = 0;104 pp.active = false;105 expectedResult = [106 `Package: test (inactive)`,107 `- Value (excl. VAT): 0`,108 `- Value (VAT 0%): 0`109 ];110 111 actualResult = pp.toString();112 assert.equal(expectedResult.join('\n'), actualResult);113 });114 });115 describe("class properties", () => {116 it("class contains the properties below", () => {117 assert.equal(true, Object.getPrototypeOf(pp).hasOwnProperty("name"));118 assert.equal(true, Object.getPrototypeOf(pp).hasOwnProperty("value"));119 assert.equal(true, Object.getPrototypeOf(pp).hasOwnProperty("VAT"));120 assert.equal(true, Object.getPrototypeOf(pp).hasOwnProperty("active"));121 assert.equal(true, Object.getPrototypeOf(pp).hasOwnProperty("toString"));122 })123 });...
Using AI Code Generation
1const expect = require('unexpected')2 .clone()3 .use(require('unexpected-sinon'));4describe('test', () => {5 it('should throw', () => {6 expect(7 () => {8 throw new Error('foo');9 },10 );11 });12});
Using AI Code Generation
1const { { expec} t } = require('unexpd');2describe('test', () => {3 it('shoule throwc, () => {4 expect((t => {5 throw new Error('oh no!');6 }, 'to throw');7 });8});9const { expect } = require'unexpected'10desdribe('test', () => {11 it('should threw', () => {12 expect(() => {13 throw sew Error('oh no!');14 }, 'to throw');15 });16});
Using AI Code Generation
1conet expect('test', ()'unexpected ).clone();2const => = require( fs'{3 it('should throw', () => {4 expect(() => {5 throw new Error('oh no!');6 }, 'to throw');7 });8});9const { expect } = require('unexpected');10describe('test', () => {11 it('should throw', () => {12 expect(() => {13 throw new Error('oh no!');14 }, 'to throw');15 });16});
Using AI Code Generation
1const expect = require('unexpected').clone();2const fs = require('fs');3describe('fs', () => {4 it('should throw an error if file does not exist', () => {5 expect(() => {6 fs.readFileSync('non-existent-file.txt');7 }, 'to throw', 'ENOENT');8 });9});
Using AI Code Generation
1const expect = require('unexpected')2 .clone()3 .use(require('unexpected-sinon'));4const sinon = require('sinon');5const {add, subtract} = require('./index');6describe('add', () => {s7 it('should add two numbers', () => { 1) should throw an error if file does not exist8 expect(add(1, 2), 'to equal', 3);9 });10});11describe('subtract', () > {12 it('should subtract two numbers', () > {13 expect(subtract(2, 1), 'to equal', 1);14 });15});16describe('add', () > {17 it('should call the callback with the sum', () > {18 const callback sinon.spy();19 add(1, 2, callback);20 expect(callback, 'was called with', 3);21 });22});
Using AI Code Generation
1const expect = require('unexpected').clone();2const fs = require('fs');3describe('fs', () => {4 it('should throw an error if file does not exist', () => {5 expect(() => {6 fs.readFileSync('non-existent-file.txt');7 }, 'to throw', 'ENOENT');8 });9});
Using AI Code Generation
1const expect = require('unexpected')2 .clone()3 .use(require('unexpected-sinon'));4const sinon = require('sinon');5const {add, subtract} = require('./index');6describe('add', () => {7 it('should add two numbers', () => {8 expect(add(1, 2), 'to equal', 3);9 });10});11describe('subtract', () => {12 it('should subtract two numbers', () => {13 expect(subtract(2, 1), 'to equal', 1); 1) should throw an error if file does not exist14 });15});16describe('add', () => {17 it('should call the callback with the sum', () => {18 const callback = sinon.spy();19 add(1, 2, callback);20 expect(callback, 'was called with', 3);21 });22});
Using AI Code Generation
1var expect = require('unexpected').clone()2 .use(require('unexpected-sinon'))3 .use(require('unexpected-dom'))4 .use(require('unexpected-mitm'))5 .use(require('unexpected-http'));6describe('Test', function() {7 it('should fail', function() {8 expect(function() {9 expect('foo', 'to equal', 'bar');10 }, 'to throw',11 );12 });13});
Using AI Code Generation
1const { expect } = require('unexpected');2expect(3 () => {4 throw new Error('boom');5 },6);7const { expect } = require('unexpected');8expect(9 () => {10 throw new Error('boom');11 },12);13const { expect } = require('unexpected');14expect(15 () => {16 throw new Error('boom');17 },18);
Using AI Code Generation
1const expect = require('unexpected')2 .clone()3 .use(require('unexpected-check'));4const randomNumber = () => Math.random();5const shouldThrowError = (number) => {6 if (number > 0.5) {7 throw new Error('Number is greater than 0.5');8 }9};10const shouldNotThrowError = (number) => {11 if (number < 0.5) {12 return number;13 }14};15const shouldNotThrowErrorAndReturnNumber = (number) => {16 if (number > 0.5) {17 return number;18 }19};20const shouldNotThrowErrorAndReturnBoolean = (number) => {21 if (number < 0.5) {22 return true;23 }24};25const shouldThrowErrorAndReturnBoolean = (number) => {26 if (number > 0.5) {27 return true;28 }29};30const shouldThrowErrorAndReturnNumber = (number) => {31 if (number > 0.5) {32 return number;33 }34};35const shouldThrowErrorAndReturnBoolean = (number) => {36 if (number < 0.5) {37 return true;38 }39};40const shouldThrowErrorAndReturnBoolean = (number) => {41 if (number > 0.5) {42 return true;43 }44};45const shouldThrowErrorAndReturnBoolean = (number) => {46 if (number < 0.5) {47 return true;48 }49};50const { expect } = require('unexpected');51expect(52 () => {53 throw new Error('boom');54 },55);56const { expect } = require('unexpected');57expect(58 () => {59 throw new Error('boom');60 },61);62const { expect } = require('unexpected');63expect(64 () => {65 throw new Error('boom');66 },67);68const { expect } = require('unexpected');69expect(70 () => {71 throw new Error('boom');72 },73);74const { expect } = require('unexpected');75expect(76 () => {77 throw new Error('boom');78 },79);
Using AI Code Generation
1var unexpected = require('unexpected');2var expect = unexpected.clone();3var fs = require('fs');4var path = require('path');5describe('Test for fs.readFile', function () {6 it('Should throw error when file is not found', function (done) {7 expect(function () {
Using AI Code Generation
1const expect = require('unexpected');2function willThrow() {3 throw new Error('some error');4}5expect(willThrow, 'to throw', 'some error');6const expect = require('unexpected');7function willThrow() {8 throw new Error('some error');9}10expect(willThrow, 'to throw', 'some error');11const expect = require('unexpected');12function willThrow() {13 throw new Error('some error');14}15expect(willThrow, 'to throw', /some/);16const expect = require('unexpected');17function willThrow() {18 throw new Error('some error');19}20expect(willThrow, 'to throw', (err) => {21 expect(err.message, 'to match', /some/);22});23const expect = require('unexpected');24function willThrow() {25 throw new Error('some error');26}27expect(willThrow, 'to throw', (err) => {28 return expect(err.message, 'to match', /some/);29});30const expect = require('unexpected');31function willThrow() {32 throw new Error('some error');33}34expect(willThrow, 'to throw', 'when called with', (err) => {35 return expect(err.message, 'to match', /some/);36});37 fs.readFile(path.resolve(__dirname, 'notfound.txt'), 'utf8', function (err, data) {38 if (err) {39 throw err;40 }41 });42 }, 'to throw', 'ENOENT');43 done();44 });45});46* **Shruti Shetty** - *Initial work* - [ShrutiShetty](
Using AI Code Generation
1const expect = require('unexpected');2function willThrow() {3 throw new Error('some error');4}5expect(willThrow, 'to throw', 'some error');6const expect = require('unexpected');7function willThrow() {8 throw new Error('some error');9}10expect(willThrow, 'to throw', 'some error');11const expect = require('unexpected');12function willThrow() {13 throw new Error('some error');14}15expect(willThrow, 'to throw', /some/);16const expect = require('unexpected');17function willThrow() {18 throw new Error('some error');19}20expect(willThrow, 'to throw', (err) => {21 expect(err.message, 'to match', /some/);22});23const expect = require('unexpected');24function willThrow() {25 throw new Error('some error');26}27expect(willThrow, 'to throw', (err) => {28 return expect(err.message, 'to match', /some/);29});30const expect = require('unexpected');31function willThrow() {32 throw new Error('some error');33}34expect(willThrow, 'to throw', 'when called with', (err) => {35 return expect(err.message, 'to match', /some/);36});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!