No description, website, or topics provided.
Clone or download
Latest commit 202ae85 Dec 12, 2018

README.md

mocha-typescript

Get it on NPM Downloads per Week Dependencies Issues Pull Requests Travis Build Status Appveyor Build Status Apache 2.0 License

Mocha testing with style - the OOP way

import { assert } from "chai";
import { suite, test } from "mocha-typescript";

@suite
class Hello {
  
    @test
    world() {
      
        assert.equal(1, 2, "Expected one to equal two.");
    }
}

Documentation Licensing

All documentation is licenced under the CC BY-NC 4.0 license.

You need legal consent by all direct maintainers of the project in order to use (parts of) the documentation for any commercial purposes.

Features

  • Declarative/Object Oriented BDD/TDD Interface
  • Parameterised Tests
  • Dependency Injection Support
  • Watcher
  • IDE Integration

Installation

Just run

npm install --save-dev mocha-typescript
npm install --save-dev @types/mocha

and you are ready to go.

You might also want to consider using the very good Chai BDD/TDD assertion framework.

npm install --save-dev chai
npm install --save-dev @types/chai

And, depending on your project, you will also have to install the type declarations for node.

npm install --save-dev @types/node

Usage

Classes can be annotated with @suite to make them test suites, similarly so, individual methods of test suites can be annotated using @test. And, by using inheritance, one can build complex test suites and reuse existing code.

The test interface also supports mocha's built-in BDD and TDD interfaces such as describe and it.

And you can mix both the OOP style and the declarative style, and you can even create nested suites by using a mixture of both styles.

import { assert } from "chai";
import { suite, test, describe, it } from "mocha-typescript";

// suite is an alias for describe
suite("one", () => {

    // test is an alias for it
    test("test", () => {});
});

describe("two", () => {
    it("test", () => {});
});

@suite
class Three {
  
    @test
    method() {}
}

describe("suite four", () => {
    it("test one", () => {});
    
    @suite
    class SuiteFourOne {
      
        @test
        method() {}
    }
});

abstract class TestBase {
  
  @test
  commonTest() {
    
    assert.equal(...);
  }
}

@suite
class ConcreteTest extends TestBase {
  
  @test
  specificTest() {
    
    assert.equal(...);
  }
}

Please keep in mind, that when the tests are run, a new instance of the test suite will be instantiated before each test method is executed.

Watcher

mocha-typescript comes with a watcher script that runs the TypeScript compiler in watch mode. So whenever your code changes and compiles, your tests will be run, too.

Further Reading

Contributors