@travetto/test

5.0.15 • Public • Published

Testing

Declarative test framework

Install: @travetto/test

npm install @travetto/test

# or

yarn add @travetto/test

This module provides unit testing functionality that integrates with the framework. It is a declarative framework, using decorators to define tests and suites. The test produces results in the following formats:

  • TAP 13, default and human-readable
  • JSON, best for integrating with at a code level
  • xUnit, standard format for CI/CD systems e.g. Jenkins, Bamboo, etc.

Note: All tests should be under the **/* folders. The pattern for tests is defined as as a standard glob using Node's built in globbing support.

Definition

A test suite is a collection of individual tests. All test suites are classes with the @Suite decorator. Tests are defined as methods on the suite class, using the @Test decorator. All tests intrinsically support async/await.

A simple example would be:

Code: Example Test Suite

import assert from 'node:assert';

import { Suite, Test } from '@travetto/test';

@Suite()
class SimpleTest {

  #complexService: {
    doLongOp(): Promise<number>;
    getText(): string;
  };

  @Test()
  async test1() {
    const val = await this.#complexService.doLongOp();
    assert(val === 5);
  }

  @Test()
  test2() {
    const text = this.#complexService.getText();
    assert(/abc/.test(text));
  }
}

Assertions

A common aspect of the tests themselves are the assertions that are made. Node provides a built-in assert library. The framework uses AST transformations to modify the assertions to provide integration with the test module, and to provide a much higher level of detail in the failed assertions. For example:

Code: Example assertion for deep comparison

import assert from 'node:assert';

import { Suite, Test } from '@travetto/test';

@Suite()
class SimpleTest {

  @Test()
  async test() {
    assert.deepStrictEqual({ size: 20, address: { state: 'VA' } }, {});
  }
}

would translate to:

Code: Transpiled test Code

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const Ⲑ_debug_1 = tslib_1.__importStar(require("@travetto/runtime/src/debug.js"));
const Ⲑ_check_1 = tslib_1.__importStar(require("@travetto/test/src/assert/check.js"));
const Ⲑ_function_1 = tslib_1.__importStar(require("@travetto/runtime/src/function.js"));
var ᚕm = ["@travetto/test", "doc/assert-example.ts"];
const node_assert_1 = tslib_1.__importDefault(require("node:assert"));
const test_1 = require("@travetto/test");
let SimpleTest = class SimpleTest {
    static Ⲑinit = Ⲑ_function_1.registerFunction(SimpleTest, ᚕm, { hash: 1887908328, lines: [5, 12] }, { test: { hash: 102834457, lines: [8, 11, 10] } }, false, false);
    async test() {
        if (Ⲑ_debug_1.tryDebugger)
            debugger;
        Ⲑ_check_1.AssertCheck.check({ module: ᚕm, line: 10, text: "{ size: 20, address: { state: 'VA' } }", operator: "deepStrictEqual" }, true, { size: 20, address: { state: 'VA' } }, {});
    }
};
tslib_1.__decorate([
    (0, test_1.Test)()
], SimpleTest.prototype, "test", null);
SimpleTest = tslib_1.__decorate([
    (0, test_1.Suite)()
], SimpleTest);

This would ultimately produce the error like:

Code: Sample Validation Error

AssertionError(
  message="{size: 20, address: {state: 'VA' }} should deeply strictly equal {}"
)

The equivalences for all of the assert operations are:

  • assert(a == b) as assert.equal(a, b)
  • assert(a !== b) as assert.notEqual(a, b)
  • assert(a === b) as assert.strictEqual(a, b)
  • assert(a !== b) as assert.notStrictEqual(a, b)
  • assert(a >= b) as assert.greaterThanEqual(a, b)
  • assert(a > b) as assert.greaterThan(a, b)
  • assert(a <= b) as assert.lessThanEqual(a, b)
  • assert(a < b) as assert.lessThan(a, b)
  • assert(a instanceof b) as assert.instanceOf(a, b)
  • assert(a.includes(b)) as assert.ok(a.includes(b))
  • assert(/a/.test(b)) as assert.ok(/a/.test(b)) In addition to the standard operations, there is support for throwing/rejecting errors (or the inverse). This is useful for testing error states or ensuring errors do not occur.

Throws

throws/doesNotThrow is for catching synchronous rejections

Code: Throws vs Does Not Throw

import assert from 'node:assert';

import { Suite, Test } from '@travetto/test';

@Suite()
class SimpleTest {

  @Test()
  async testThrows() {
    assert.throws(() => {
      throw new Error();
    });

    assert.doesNotThrow(() => {

      let a = 5;
    });
  }
}

Rejects

rejects/doesNotReject is for catching asynchronous rejections

Code: Rejects vs Does Not Reject

import assert from 'node:assert';

import { Suite, Test } from '@travetto/test';

@Suite()
class SimpleTest {

  @Test()
  async testRejects() {
    await assert.rejects(async () => {
      throw new Error();
    });

    await assert.doesNotReject(async () => {

      let a = 5;
    });
  }
}

Error Matching

Additionally, the throws/rejects assertions take in a secondary parameter to allow for specification of the type of error expected. This can be:

  • A regular expression or string to match against the error's message
  • A class to ensure the returned error is an instance of the class passed in
  • A function to allow for whatever custom verification of the error is needed

Code: Example of different Error matching paradigms

import assert from 'node:assert';

import { Suite, Test } from '@travetto/test';

@Suite()
class SimpleTest {

  @Test()
  async errorTypes() {
    assert.throws(() => {
      throw new Error('Big Error');
    }, 'Big Error');

    assert.throws(() => {
      throw new Error('Big Error');
    }, /B.*Error/);

    await assert.rejects(() => {
      throw new Error('Big Error');
    }, Error);

    await assert.rejects(() => {
      throw new Error('Big Error');
    }, (err: Error) =>
      err.message.startsWith('Big') && err.message.length > 4
    );
  }
}

Running Tests

To run the tests you can either call the Command Line Interface by invoking

Terminal: Test Help Output

$ trv test --help

Usage: test [options] [first:string] [globs...:string]

Options:
  -f, --format <string>         Output format for test results (default: "tap")
  -c, --concurrency <number>    Number of tests to run concurrently (default: 4)
  -m, --mode <single|standard>  Test run mode (default: "standard")
  -t, --tags <string>           Tags to target or exclude
  -h, --help                    display help for command

The regexes are the patterns of tests you want to run, and all tests must be found under the test/ folder.

Travetto Plugin

The VSCode plugin also supports test running, which will provide even more functionality for real-time testing and debugging.

Additional Considerations

During the test execution, a few things additionally happen that should be helpful. The primary addition, is that all console output is captured, and will be exposed in the test output. This allows for investigation at a later point in time by analyzing the output.

Like output, all promises are also intercepted. This allows the code to ensure that all promises have been resolved before completing the test. Any uncompleted promises will automatically trigger an error state and fail the test.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
5.0.0-rc.130rc
2.0.0-alpha.120alpha
1.1.0-rc.00next
1.0.0-beta.10beta
5.0.1569latest

Version History

VersionDownloads (Last 7 Days)Published
5.0.1569
5.0.1420
5.0.131
5.0.120
5.0.110
5.0.100
5.0.90
5.0.80
5.0.70
5.0.60
5.0.50
5.0.40
5.0.30
5.0.20
5.0.10
5.0.00
5.0.0-rc.130
5.0.0-rc.120
5.0.0-rc.110
5.0.0-rc.100
5.0.0-rc.90
5.0.0-rc.80
5.0.0-rc.70
5.0.0-rc.60
5.0.0-rc.50
5.0.0-rc.40
5.0.0-rc.30
5.0.0-rc.20
5.0.0-rc.10
5.0.0-rc.00
4.1.12
4.1.00
4.0.70
4.0.60
4.0.50
4.0.40
4.0.30
4.0.20
4.0.10
4.0.00
4.0.0-rc.80
4.0.0-rc.70
4.0.0-rc.60
4.0.0-rc.50
4.0.0-rc.40
4.0.0-rc.30
4.0.0-rc.20
4.0.0-rc.10
4.0.0-rc.00
3.4.33
3.4.20
3.4.10
3.4.00
3.4.0-rc.70
3.4.0-rc.60
3.4.0-rc.50
3.4.0-rc.40
3.4.0-rc.30
3.4.0-rc.20
3.4.0-rc.10
3.4.0-rc.00
3.3.60
3.3.50
3.3.40
3.3.30
3.3.20
3.3.10
3.3.00
3.2.30
3.2.20
3.2.10
3.2.00
3.2.0-rc.00
3.1.60
3.1.50
3.1.40
3.1.30
3.1.20
3.1.10
3.1.00
3.1.0-rc.40
3.1.0-rc.31
3.1.0-rc.20
3.1.0-rc.10
3.1.0-rc.00
3.0.30
3.0.20
3.0.2-rc.10
3.0.2-rc.00
3.0.10
3.0.1-rc.10
3.0.00
3.0.0-rc.281
3.0.0-rc.270
3.0.0-rc.260
3.0.0-rc.250
3.0.0-rc.240
3.0.0-rc.230
3.0.0-rc.220
3.0.0-rc.210
3.0.0-rc.200
3.0.0-rc.191
3.0.0-rc.180
3.0.0-rc.170
3.0.0-rc.160
3.0.0-rc.150
3.0.0-rc.140
3.0.0-rc.130
3.0.0-rc.120
3.0.0-rc.110
3.0.0-rc.90
3.0.0-rc.80
3.0.0-rc.70
3.0.0-rc.60
3.0.0-rc.40
3.0.0-rc.30
3.0.0-rc.20
3.0.0-rc.10
3.0.0-rc.00
2.2.40
2.2.30
2.2.20
2.2.10
2.2.00
2.1.50
2.1.40
2.1.30
2.1.20
2.1.10
2.1.00
2.0.40
2.0.30
2.0.20
2.0.10
2.0.00
2.0.0-rc.60
2.0.0-rc.50
2.0.0-rc.40
2.0.0-rc.30
2.0.0-rc.20
2.0.0-rc.10
2.0.0-rc.00
2.0.0-alpha.120
2.0.0-alpha.110
2.0.0-alpha.100
2.0.0-alpha.90
2.0.0-alpha.80
2.0.0-alpha.70
2.0.0-alpha.60
2.0.0-alpha.50
2.0.0-alpha.40
2.0.0-alpha.30
2.0.0-alpha.20
2.0.0-alpha.10
1.1.10
1.1.00
1.1.0-rc.00
1.1.0-alpha.60
1.1.0-alpha.50
1.1.0-alpha.40
1.1.0-alpha.30
1.1.0-alpha.20
1.1.0-alpha.10
1.1.0-alpha.00
1.0.70
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00
1.0.0-rc.90
1.0.0-rc.80
1.0.0-rc.70
1.0.0-rc.60
1.0.0-rc.50
1.0.0-rc.40
1.0.0-rc.30
1.0.0-rc.20
1.0.0-rc.10
1.0.0-rc.00
1.0.0-beta.10
0.7.40
0.7.30
0.7.20
0.7.10
0.7.1-alpha.60
0.7.1-alpha.50
0.7.1-alpha.40
0.7.1-alpha.30
0.7.1-alpha.20
0.7.1-alpha.10
0.7.1-alpha.00
0.7.0-alpha.00
0.6.70
0.6.60
0.6.50
0.6.40
0.6.30
0.6.10
0.6.00
0.6.0-rc.100
0.6.0-rc.90
0.6.0-rc.80
0.6.0-rc.70
0.6.0-rc.60
0.6.0-rc.40
0.6.0-rc.30
0.6.0-rc.20
0.6.0-rc.10
0.6.0-rc.00
0.5.110
0.5.100
0.5.90
0.5.80
0.5.70
0.5.60
0.5.50
0.5.40
0.5.30
0.5.20
0.5.10
0.5.00
0.4.90
0.4.80
0.4.70
0.4.60
0.4.50
0.4.40
0.4.30
0.4.20
0.4.10
0.4.00
0.3.210
0.3.200
0.3.190
0.3.180
0.3.170
0.3.160
0.3.150
0.3.140
0.3.130
0.3.120
0.3.110
0.3.90
0.3.80
0.3.70
0.3.60
0.3.50
0.3.40
0.3.30
0.3.20
0.3.00
0.2.120
0.2.110
0.2.100
0.2.90
0.2.80
0.2.70
0.2.60
0.2.50
0.2.30
0.2.21
0.2.01
0.1.61
0.1.51
0.1.41
0.1.30
0.1.20
0.1.10
0.0.831
0.0.820
0.0.810
0.0.800
0.0.790
0.0.780
0.0.770
0.0.760
0.0.750
0.0.740
0.0.730
0.0.720
0.0.710
0.0.700
0.0.690
0.0.680
0.0.670
0.0.660
0.0.650
0.0.640
0.0.630
0.0.620
0.0.610
0.0.600
0.0.590
0.0.580
0.0.570
0.0.560
0.0.550
0.0.540
0.0.530
0.0.520
0.0.510
0.0.500
0.0.490
0.0.480
0.0.470
0.0.460
0.0.450
0.0.440
0.0.430
0.0.420
0.0.410
0.0.400
0.0.390
0.0.380
0.0.370
0.0.360
0.0.350
0.0.340
0.0.320
0.0.310
0.0.300
0.0.290
0.0.280
0.0.270
0.0.260
0.0.250
0.0.241
0.0.230
0.0.220
0.0.210
0.0.200
0.0.191
0.0.180
0.0.170
0.0.150
0.0.140
0.0.130
0.0.120
0.0.110
0.0.100
0.0.90
0.0.80
0.0.70
0.0.60
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i @travetto/test

Homepage

travetto.io

Weekly Downloads

106

Version

5.0.15

License

MIT

Unpacked Size

112 kB

Total Files

54

Last publish

Collaborators

  • arcsine