2

Is it possible to write mocha test inside class e.g :

 class test
 {

   // mocha test
   describe("test goes here", function(){
   it("sample test", function(){})
   })
 }

how to trigger test in this case ??

user522170
  • 623
  • 1
  • 6
  • 21

1 Answers1

1

Tests must be defined synchronously for mocha to be able to collect your tests.

index.test.js:

const expect = require("chai").expect;

class Test {
  run() {
    describe("test goes here", function() {
      it("sample test", function() {
        expect(1 + 1).to.be.eq(2);
      });
    });
  }
}

new Test().run();

Test results:

  test goes here
    ✓ sample test


  1 passing (5ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |      100 |      100 |      100 |      100 |                   |
 index.test.js |      100 |      100 |      100 |      100 |                   |
---------------|----------|----------|----------|----------|-------------------|

Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/59984203

Lin Du
  • 88,126
  • 95
  • 281
  • 483
  • thanks for answer, but got new issue : https://stackoverflow.com/questions/60212359/how-to-make-sure-this-inside-mocha-test-have-access-to-class-properties – user522170 Feb 13 '20 at 16:22