1.6.2 • Published 10 months ago

@locustjs/test v1.6.2

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@locustjs/test

This library provides a simple test runner for unit-testing.

Install

npm i @locustjs/test

Example:

import TestRunner from '@locustjs/test';

const tests = [
  ['test 1', expect => expect(2 + 2).toBe(4)],
  ['test 2', expect => expect(undefined).toBeUndefined()],
  ['test 3', expect => expect(5).toBeGt(10)],  // this test fails
  ...
];

const runner = new TestRunner();

await runner.run(tests);

runner.report();
runner.log();

Sample output:

  1. Test 1: number: passed (0 sec)

  2. Test 2: string: passed (0 sec)

  3. Test 3: empty array: passed (0 sec)

  4. Test 4: array: passed (0 sec)

  5. Test 5: empty object: passed (0 sec)

  6. Test 6: object: passed (0 sec)

  7. Test 7: error: failed (0.001 sec)

  8. Test 8: not expected: expect not used (0 sec)

  9. Test 9: error without expect: faulted (0 sec)

  10. Test 10: class: passed (0 sec)

Test result types

  • passed: Test passed.
  • failed: Test failed (at least one expect did not succeed).
  • faulted: Test crashed (no expect was called).
  • unknown: Test executed, however, no expect was seen.

expect

Positive

  • toBe(value)
  • toBeGt(value)
  • toBeGreaterThan(value)
  • toBeGte(value)
  • toBeGreaterThanOrEqualTo(value)
  • toBeLt(value)
  • toBeLowerThan(value)
  • toBeLte(value)
  • toBeLowerThanOrEqualTo(value)
  • toBeBetween(n, m)
  • toBeOfType(type)
  • toBeString()
  • toBeSomeString()
  • toBeNumber()
  • toBeNumeric()
  • toBeDate()
  • toBeBool()
  • toBeBasicType()
  • toBePrimitive()
  • toBeEmpty()
  • toBeObject()
  • toBeSomeObject()
  • toBeFunction()
  • toBeArray()
  • toBeEmptyArray()
  • toBeSomeArray()
  • toBeIterable()
  • toBeSubClassOf(type)
  • toBeInstanceOf(type)
  • toMatch(pattern, flags)
  • toBeDefined()
  • toBeUndefined()
  • toBeNull()
  • toBeNullOrUndefined()
  • toBeValid(fnValidation)
  • toThrow(ex, shape = false, strict = false)
  • async toThrowAsync(ex, shape = false, strict = false)
  • toBeTruthy()
  • toBeTrue()
  • toBeFalsy()
  • toBeFalse()
  • toBeNaN()

Negative

  • notToBe(value)
  • notToBeBetween(n, m)
  • notToBeOfType(type)
  • notToBeString()
  • notToBeSomeString()
  • notToBeNumber()
  • notToBeDate()
  • notToBeBool()
  • notToBeBasicType()
  • notToBePrimitive()
  • notToBeEmpty()
  • notToBeObject()
  • notToBeSomeObject()
  • notToBeFunction()
  • notToBeNumeric()
  • notToBeArray()
  • notToBeSomeArray()
  • notToBeIterable()
  • notToBeSubClassOf(type)
  • notToBeInstanceOf(type)
  • notToMatch(pattern, flags)
  • doesNotMatch(pattern, flags)
  • notToBeDefined()
  • notToBeUndefined()
  • notToBeNull()
  • notToBeNullOrUndefined()
  • notToBeValid(fnValidation)
  • notToThrow(ex, shape = false, strict = false)
  • async notToThrowAsync(ex, shape = false, strict = false)
  • notToBeNaN()

Qucik Testing

TestRunner has a static method start() that simplifies running tests.

const tests = [
  ...
];

TestRunner.start(tests);

It is possible to pass multiple tests to start() method.

import tests1 from './test1.js'
import tests2 from './test2.js'
import tests3 from './test3.js'

TestRunner.start(tests1, tests2, tests3);

The above example is equal to merging all tests arrays and pass one array to start().

TestRunner.start([...tests1, ...tests2, ...tests3])

By default, test runner shows detailed output only when there is at least one error. However, by passing true as the last argument of start() method, we can ask the detailed output is always shows.

TestRunner.start(tests, true);
TestRunner.start(tests1, tests2, tests3, true);
1.6.2

10 months ago

1.2.0

1 year ago

1.6.1

12 months ago

1.6.0

12 months ago

1.5.0

12 months ago

1.4.0

12 months ago

1.3.1

1 year ago

1.3.0

1 year ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago