@haraldki/unittesterjs - v2.0.0
    Preparing search index...

    Class UnitTester

    Provides a test runner for unit tests.

    Index

    Constructors

    Properties

    instancesSkipped: string[] = []
    testsCrashed: string[] = []
    testsFailed: string[] = []
    testsIgnored: string[] = []
    testsSucceeded: string[] = []

    Methods

    • May be called from a test function to fail it if it takes longer than the given milliseconds. Be sure to call this early enough in the test, typically at the very beginning.

      NOTE: if the subsequent code goes into a busy loop, the timeout does not work. Rather the assumption here is that the test code goes async and that this asynchronous part does not provide a result before the timeout.

      Parameters

      • timeoutMs: number

        the milliseconds after which the UnitTester tries to pre-empt the test function. This counts then as a crashed test.

      Returns void

      Error if called while no test is beijng executed

    • Runs methods found in the prototype of testInstance. Methods in the prototype are treated as follows

      • A method with a name matching isTest and no parameters is called as a test. If a test function fails, it is reported as failed or crashed (see UnitTester.constructor}. Test methods may override superclass test methods to allow writing a general test and a more specific one for, say, a subclass of the class to be tested.

      • The two functions beforeEach and afterEach, if defined, are called before and after each invocation of a test function. The may be inherited.

      • The function afterAll, if defined, is called only once, after all tests are run, in particular after the afterEach function. An afterEach is only necessary if state external to the test instance needs to cleaned up. There is no beforeAll, use the constructor. One-off state of the test class itself may just go into the constructor and needs no cleanup anyway. Both functions may be inherited.

      • All other functions are not touched and are assumed to be helper functions for the individual tests.

      • If a function matches isTest or has one of the before/after names but also has parameters, it is logged and ignored.

      Parameters

      • testInstance: object
      • isTest: RegExp = ...

      Returns Promise<void>

    • The modules is scanned for function objects which look like a class. If their name checks truewithisTestClass.test(), the object is called with newand the result is passed to {@link run}, together with parameterisTest`. One use case being like:

      import * as SomeOfMyTests from "SomeOfMyTests.js";
      ut.runAll(AllMyTests);

      If a module object contains a name field, this name is added to test method names used for logging and statistics. An example of setting the name in a module of test classes:

      export const name = import.meta.url.split("/").slice(-1);
      

      Parameters

      • module: object

        an object to be scanned for test classes, i.e. functions with a prototype.constructor, their name matching isTest.

      • isTestClass: RegExp = ...

        overrides constructor option UnitTesterOptions.isTestClass

      • isTest: RegExp = ...

        overrides constructor option UnitTesterOptions.isTest

      Returns Promise<void>

    • Drop-in replacement for runAll to skip the module in the same manner as runX does for test functions.

      Parameters

      • module: object
      • isTestClass: RegExp = ...
      • Optional_isTest: RegExp

      Returns Promise<void>

    • Ignores testInstance, but records it and adds it to the output summary.

      To temporarily ignore a whole test instance, as opposed to an individual test function, you could simply comment the respective line, like in

      const ut = new UnitTester();
      // ut.run(new AnimalTest(), "^animal");

      But this has two disadvantages: The compiler or linter likely complains that AnimalTest is unused and, even worse, you may forget to remove the comment again. Instead of commenting out, switch from using run to runX like

      const ut = new UnitTester();
      ut.runX(new AnimalTest(), "^animal");

      Then the compiler will not complain and the skipped instance will show up in the summary (at least for default test loggers).

      Parameters

      • testInstance: object
      • Optional_unused: RegExp

      Returns Promise<void>

    • Calls TestLogger.summarize on our logger.

      Returns void