1.0.1 • Published 5 years ago

@singleware/testing v1.0.1

Weekly downloads
15
License
MIT
Repository
github
Last release
5 years ago

Testing

This package provides a simple way to automate your test scripts.

How to use

First you need to create your test case, all test cases must extends the library test case, see below

import * as Class from '@singleware/class';
import * as Testing from '@singleware/testing';

@Class.Describe()
export class MyTestCase extends Testing.Case {
  @Class.Public()
  @Testing.Method()
  public testMethod(): void {
    // Assertions here...
  }
}

After you need to create the suite runner, the suite runner script can be similar the code below

import * as Testing from '@singleware/testing';

import { MyTestCase } from './mytestcase.spec';

const suite = new Testing.Suite();

suite.addCase(MyTestCase);

suite.perform().then((report: Testing.Report.Suite) => {
  const logger = new Testing.Loggers.Tap();
  logger.print(report);
  process.exitCode = report.errors > 0 ? 1 : 0;
});

For browser compatibility, remove the line process.exitCode

Now you are ready to add assertions in your test case, you can add as many assertions as you want, after that you just need to build and run the suite script like any JavaScript file.

Assertions

The package provides a collection of assertion methods to help you to test expected values and put its results in the report object. Any assertion method can be accessed by the test case instance using the keyword this.

NameDescription
isTrueDetermines whether the specified condition is true.
isFalseDetermines whether the specified condition is false.
isNaNDetermines whether the specified value is NaN.
isNullDetermines whether the specified value is null.
isInfiniteDetermines whether the specified value is infinite.
isUndefinedDetermines whether the specified value is undefined.
isEmptyDetermines whether the specified value is empty.
isBooleanDetermines whether the specified value is a boolean type.
isStringDetermines whether the specified value is a string type.
isNumberDetermines whether the specified value is a number type.
isArrayDetermines whether the specified value is an array type.
isInstanceOfDetermines whether the specified value is an instance of the expected type.
isGreaterThanDetermines whether the actual is greater than the expected value.
isGreaterThanOrEqualDetermines whether the actual value is greater than or equal the expected value.
isLessThanDetermines whether the actual value is less than the expected value.
isLessThanOrEqualDetermines whether the actual value is less than or equal the expected value.
areEqualsDetermines whether the specified values are equals.
areNotEqualsDetermines whether the specified values are not equals.
areSameDetermines whether the specified values and types are the same.
areNotSameDetermines whether the specified values and types are not the same.
hasIndexDetermines whether the specified array has the expected index.
hasPropertyDetermines whether the specified object has the expected property.
hasMethodDetermines whether the specified object has the expected method.
hasValueDetermines whether the specified array contains the given value.
hasOnlyDetermines whether the specified array contains only the expected value.
hasExceptionDetermines whether the specified callback throws an expected exception.

Report

After performing all test cases, the library will generates a report object that can be converted to multiple output formats, you can check the Loggers namespace to see which output formats are available for your current version.

Install

Using npm:

npm i @singleware/testing

License

MIT © Silas B. Domingos