2.1.5 • Published 1 year ago

@netatwork/mocha-utils v2.1.5

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
1 year ago

@netatwork/mocha-utils

npm version npm download build status

A bunch of utilities to be used for a mocha-based test setup.

Reporters

This packages offers 2 different kinds of reporters.

JunitSpecReporter

This meant to be used directly with Mocha. If you are already familiar with Mocha, then you may know that Mocha does not support multiple reporters out of the box. This reporter simply combines the mocha-junit-reporter with the Mocha spec reporter. The goal is to facilitate generation of the JUnit report at the end of test, as well as have the result printed on the terminal.

Usage

// .mocharc.js

module.exports = {
  reporter: '@netatwork/mocha-utils/dist/JunitSpecReporter.js',
  // Optional: provide the path for the generated JUnit report
  reporterOptions: ['mochaFile=./tests/.artifacts/results.xml'],
};

Karma-mocha HTML reporter

This is a similar reporter like karma-jasmine-html-reporter, meant to be used in a karma-mocha-based setup.

Usage

const isDev = !!process.env.DEV;
const reporters = [...];

// this is a dev-only reporter
if (isDev) {
    reporters.push("kmhtml");
}

module.exports = function(config) {
    config.set({
        frameworks: ['mocha'],
        plugins: [
            // allows karma to load the conventional plugins
            "karma-*",
            // add the custom reporter itself.
            require("@netatwork/mocha-utils/dist/karma-html-reporter/index"),
        ],

        client: {
            clearContext: false,
        },

        reporters,
    })
};

Spec wrapper

This utility function provides an alternative of using the beforeEach, and afterEach hooks. The idea is to have a wrapper function that sets up the environment for the test, calls the test functions (this is where the assertions are), and after the test also performs the cleanup. This pattern is based on the principle of increasing isolation between tests, and reduce the usage of shared state. In fact eslint has a rule for this.

Usage

import { createSpecFunction, TestContext, TestFunction } from '@netatwork/mocha-utils';

describe('example', function () {
  interface TestSetupContext {
    propA: boolean;
  }

  // Wrapper function
  async function runTest(
    testFunction: TestFunction<TestContext<SystemUnderTest>>,
    { propA = false }: Partial<TestSetupContext> = {}
  ) {
    // arrange/setup as per the options provided via TestSetupContext
    const sut = new SystemUnderTest(propA);

    // act and Assert
    await testFunction({ sut });

    // cleanup
    sut.dispose();
  }

  // Create the wrapped `it`
  const $it = createSpecFunction(runTest);

  $it('works#1', function ({ sut }) {
    assert.strictEqual(sut?.doSomething(), false);
  });

  $it('works#2', function ({ sut }) {
    assert.strictEqual(sut?.doSomething(), true);
  }, { propA: true });
}

class SystemUnderTest {

  public constructor(
    private readonly propA: boolean,
  ) { }

  public doSomething() {
    return this.propA;
  }

  public dispose() {
    //...
  }
}

Acknowledgements

2.1.5

1 year ago

2.1.4

1 year ago

2.1.2

2 years ago

2.1.3

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago