Tests

With testdeck you author tests by decorating methods of your suite with the @test decorator.

And when using inheritance you are also able to inherit common tests for a family of SUTs.

Synchronous Tests

1
2
3
4
5
6
7
8
9
10
11
12
import { suite, test } from '@testdeck/mocha';
import { assert } from 'chai';

@suite
class Suite {

  @test
  test() {

    assert.isOk(false);  
  }
}

Run Tests

npm test

...

  Suite
    1) test

...

  1) Suite
       test:
     AssertionError: expected false to be truthy

...

Asynchronous Tests

As with chaining of asynchronous lifecycle hooks, writing asynchronous tests is also somewhat more inclined.

Callback Style

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { suite, test } from '@testdeck/mocha';
import { assert } from 'chai';

function asyncSUT(cb) {

  setTimeout(() => cb(null, false), 100);
}

@suite
class Suite {

  @test
  test(done) {

    asyncSUT((err, res) => {

      if (err) {

        // expected error testing anyone?
        return done(err);
      }

      try {

        assert.isOk(res);

        return done();
      } catch (ex) {

        // we want the test to fail with the assertion error and not just due
        // to some timeout because done was not called back in time
        return done(ex);
      }
    });
  }
}

Run Tests

npm test

...

  Suite
    1) test

...

  1) Suite
       test:
     AssertionError: expected false to be truthy

...

Promise Style

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { suite, test } from '@testdeck/mocha';
import { assert } from 'chai';

function asyncSUT(): Promise<any> {

  return Promise.resolve(false);
}

@suite
class Suite {

  @test
  test() {

    return asyncSUT().then((res) => {
    
      assert.isOk(res);
    }, (err) => {
    
      // expected error testing anyone?
      // ...
    });  
  }
}

Run Tests

npm test

...

  Suite
    1) test

...

  1) Suite
       test:
     AssertionError: expected false to be truthy

...

Async/Await Style

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { suite, test } from '@testdeck/mocha';
import { assert } from 'chai';

async function asyncSUT(): Promise<any> {

  return Promise.resolve(false);
}

@suite
class Suite {

  @test
  async test() {

    let res;
    
    try {
    
      res = await asyncSUT();
    } catch (ex) {

      // expected exception testing anyone?    
      // ...
      return;
    }

    // we would not want to run these assertions in above try because otherwise we would 
    // have to filter them or use a nested try/catch/rethrow
    assert.isOk(res);
  }
}

Run Tests

npm test

...

  Suite
    1) test

...

  1) Suite
       test:
     AssertionError: expected false to be truthy

...