├── .nycrc.json ├── src ├── yourClass.js └── myClass.js ├── specs ├── dummy.spec.js ├── yourClass.spec.js └── myClass.spec.js ├── package.json └── README.md /.nycrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "include": "src", 3 | "exclude": "specs", 4 | "check-coverage": true, 5 | "branches": 80, 6 | "lines": 80, 7 | "functions": 80, 8 | "statements": 80, 9 | "report-dir": "./reports", 10 | "skip-full": true 11 | } 12 | -------------------------------------------------------------------------------- /src/yourClass.js: -------------------------------------------------------------------------------- 1 | class YourClass { 2 | constructor() {} 3 | addSubtract(arg1, arg2, add) { 4 | var result; 5 | if (add) { 6 | result = arg1 + arg2; 7 | return result; 8 | } else { 9 | result = arg1 - arg2; 10 | return result; 11 | } 12 | } 13 | } 14 | 15 | module.exports = YourClass; 16 | -------------------------------------------------------------------------------- /specs/dummy.spec.js: -------------------------------------------------------------------------------- 1 | var chai = require("chai"); 2 | var expect = chai.expect; 3 | 4 | describe("Dummy test suit", function() { 5 | it("dummy test case ", function() { 6 | expect(true).to.be.true; 7 | }); 8 | }); 9 | 10 | // Root level hook 11 | beforeEach(function() { 12 | console.log("------------ Root level hook in dummy.spec.js"); 13 | }); 14 | -------------------------------------------------------------------------------- /specs/yourClass.spec.js: -------------------------------------------------------------------------------- 1 | var chai = require("chai"); 2 | var expect = chai.expect; 3 | 4 | var YourClass = require("../src/yourClass.js"); 5 | var yourObj = new YourClass(); 6 | 7 | describe("Test suit ", function() { 8 | it("should return sum of the numbers", function() { 9 | expect(yourObj.addSubtract(1, 2, true)).to.equal(3); 10 | }); 11 | }); 12 | 13 | // Root level hook 14 | beforeEach(function() { 15 | console.log("------------ Root level hook in yourClass.spec.js"); 16 | }); 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "unittest", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha './specs/**/*.spec.js' --watch", 8 | "coverage": "nyc --reporter=lcov --reporter=text npm run test" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "devDependencies": { 13 | "chai": "^4.2.0", 14 | "chai-as-promised": "^7.1.1", 15 | "mocha": "^7.1.0", 16 | "nock": "^12.0.2", 17 | "nyc": "^15.0.0", 18 | "sinon": "^9.0.0" 19 | }, 20 | "dependencies": { 21 | "xmlhttprequest": "^1.8.0" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mocha---sample-test-cases 2 | 3 | This is a sample repo created for unit testing beginners. The link to the corresponding video tutorial series that you should visit to understand the concepts, is given in the end of description section. 4 | 5 | ## Steps to run the test cases: 6 | 1. Clone the repo 7 | 2. Open terminal and navigate to the repo 8 | 3. Run - ```npm i``` 9 | 4. Run - ```npm run coverage``` 10 | 11 | ## Video tutorial web-series 12 | 13 | [![IMAGE ALT TEXT HERE](https://img.youtube.com/vi/NBjYY8P08lI/0.jpg)](https://www.youtube.com/watch?v=NBjYY8P08lI&list=%20-vU0JLfDBsZGbSUdNX4mQ8) 14 | -------------------------------------------------------------------------------- /src/myClass.js: -------------------------------------------------------------------------------- 1 | var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; 2 | 3 | class MyClass { 4 | constructor() { 5 | console.log("initiate"); 6 | } 7 | 8 | sayHello(str) { 9 | console.log(str); 10 | } 11 | 12 | add(arg1, arg2) { 13 | if (1) { 14 | var result; 15 | result = arg1 + arg2; 16 | return result; 17 | } else { 18 | return 2; 19 | } 20 | } 21 | 22 | callAnotherFn(arg1, arg2) { 23 | this.sayHello("hello world"); 24 | var result = this.add(arg1, arg2); 25 | return result; 26 | } 27 | 28 | testPromise() { 29 | return new Promise(function(resolve, reject) { 30 | setTimeout(() => resolve(3), 3000); 31 | }).then(function(result) { 32 | return result * 2; 33 | }); 34 | } 35 | 36 | callTheCallback(callback) { 37 | callback(); 38 | } 39 | 40 | xhrFn() { 41 | return new Promise((resolve, reject) => { 42 | var xhr = new XMLHttpRequest(); 43 | xhr.open("post", "https://echo-service-new.herokuapp.com/echo", true); 44 | 45 | xhr.onreadystatechange = function() { 46 | if (xhr.readyState == 4) { 47 | if (xhr.status == 200) { 48 | resolve(JSON.parse(xhr.responseText)); 49 | } else { 50 | reject(xhr.status); 51 | } 52 | } 53 | }; 54 | xhr.send(); 55 | }) 56 | .then(function(result) { 57 | return result; 58 | }) 59 | .catch(error => { 60 | return error; 61 | }); 62 | } 63 | } 64 | 65 | module.exports = MyClass; 66 | -------------------------------------------------------------------------------- /specs/myClass.spec.js: -------------------------------------------------------------------------------- 1 | var MyClass = require("../src/myclass.js"); 2 | var sinon = require("sinon"); 3 | var myObj = new MyClass(); 4 | var chai = require("chai"); 5 | var expect = chai.expect; 6 | const chaiaspromise = require("chai-as-promised"); 7 | chai.use(chaiaspromise); 8 | const nock = require("nock"); 9 | 10 | describe("Test suit", function() { 11 | after(function() { 12 | console.log("===================== After the test suit"); 13 | }); 14 | before(function() { 15 | console.log("===================== Before the test suit"); 16 | }); 17 | afterEach(function() { 18 | console.log("======= After every test case inside this test suit"); 19 | }); 20 | beforeEach(function() { 21 | // Sinon wrappers must be restored before or after a test case. 22 | // Hooks makes it easier implement 23 | sinon.restore(); 24 | console.log("======= Before every test case inside this test suit"); 25 | }); 26 | 27 | it("Test the add method", function() { 28 | expect(myObj.add(1, 2)).to.be.equal(3); 29 | }); 30 | 31 | it("spy the add method", function() { 32 | var spy = sinon.spy(myObj, "add"); 33 | var arg1 = 10, 34 | arg2 = 20; 35 | myObj.callAnotherFn(arg1, arg2); 36 | //sinon.assert.calledTwice(spy); 37 | expect(spy.calledOnce).to.be.true; 38 | expect(spy.calledWith(10, 20)).to.be.true; 39 | }); 40 | 41 | it("copy of spy the add method", function() { 42 | // This test will fail in case sinon.restore() is not called 43 | var spy = sinon.spy(myObj, "add"); 44 | var arg1 = 10, 45 | arg2 = 20; 46 | myObj.callAnotherFn(arg1, arg2); 47 | //sinon.assert.calledTwice(spy); 48 | expect(spy.calledOnce).to.be.true; 49 | expect(spy.calledWith(10, 20)).to.be.true; 50 | }); 51 | 52 | it("spy the callabck method", function() { 53 | var callback = sinon.spy(); 54 | myObj.callTheCallback(callback); 55 | expect(callback.calledOnce).to.be.true; 56 | }); 57 | 58 | it("mock the sayHello method", function() { 59 | var mock = sinon.mock(myObj); 60 | var expetcation = mock.expects("sayHello"); 61 | expetcation.exactly(1); 62 | expetcation.withArgs("hello world"); 63 | myObj.callAnotherFn(10, 20); 64 | mock.verify(); 65 | }); 66 | }); 67 | 68 | describe("Test suit for stub", function() { 69 | it("Stub the add method", function() { 70 | var stub = sinon.stub(myObj, "add"); 71 | stub 72 | .withArgs(10, 20) 73 | .onFirstCall() 74 | .returns(100) 75 | .onSecondCall() 76 | .returns(200); 77 | expect(myObj.callAnotherFn(10, 20)).to.be.equal(100); 78 | expect(myObj.callAnotherFn(10, 20)).to.be.equal(200); 79 | }); 80 | }); 81 | 82 | describe("Test the promise", function() { 83 | it("Promise test case", function(done) { 84 | this.timeout(4000); 85 | myObj.testPromise().then(function(result) { 86 | expect(result).to.be.equal(6); 87 | done(); 88 | }); 89 | }); 90 | }); 91 | 92 | describe("Test the promise", function() { 93 | it("Promise test case with chai-as-promised", function() { 94 | this.timeout(0); 95 | return expect(myObj.testPromise()).to.eventually.equal(6); 96 | }); 97 | }); 98 | 99 | describe("XHR test suit", function() { 100 | it("Mock and stub xhr call", function(done) { 101 | var obj = { id: 123 }; 102 | const scope = nock("https://echo-service-new.herokuapp.com") 103 | .post("/echo") 104 | .reply(200, obj); 105 | myObj 106 | .xhrFn() 107 | .then(function(result) { 108 | expect(result).to.be.eql(obj); 109 | done(); 110 | }) 111 | .catch(error => { 112 | done(new Error("test case failed: " + error)); 113 | }); 114 | }); 115 | }); 116 | --------------------------------------------------------------------------------