@kompanie/testwerk
v2.1.1
Published
A dependency-free, buildless test runner for browsers and node.js
Readme
Testwerk 🏭
A dependency-free, buildless test runner for browsers and node.js environments.
Getting Started
At first you need to install the package using the following command:
npm i @kompanie/testwerkWriting tests
Tests are organized into classes.
Each public function is treated as a test.
If you need helper functions inside your test class, use private functions.
Async functions are also supported and will automatically fail if they reach the configured asyncTestTimeout.
The following examples use @kompanie/assert. You can use any other assertion library that throws errors if an assertion fails.
import { Assert } from "@kompanie/assert";
export class MyTests {
add_shouldCorrectlyAdd() {
const actual = 1 + 1;
Assert.equal(actual, 2);
}
async async_add_shouldCorrectlyAdd() {
await new Promise(resolve => setTimeout(resolve, 1000));
const actual = 1 + 1;
Assert.equal(actual, 2);
}
#myHelperFunction() {
// I'm not treated as test
}
afterAll() {
// Executed after all tests are finished
}
afterEach() {
// Runs after each individual test
}
beforeAll() {
// Runs before test execution starts
}
beforeEach() {
// Runs before each individual test
}
}Running tests
Your tests can be executed and visualized either as HTML (browser) or in the console (browser or node.js).
Running tests with HTML output
import { TestRunnerHtml } from "@kompanie/testwerk";
import { MyTests } from "./tests/myTest.js";
const runnerConfig = {
asyncTestTimeout: 5000,
afterEachTimeout: 500,
beforeEachTimeout: 500
}; // Optional, these are the defaults
const testResultContainer = document.getElementById("test-result-container");
const testRunnerHtml = new TestRunnerHtml(runnerConfig);
testRunnerHtml.run(testResultContainer, MyTest); // Add your test classes here. No array declaration needed.Running tests with console output
import { TestRunnerConsole } from "@kompanie/testwerk";
import { MyTests } from "./tests/myTest.js";
const runnerConfig = {
asyncTestTimeout: 5000,
afterEachTimeout: 500,
beforeEachTimeout: 500
}; // Optional, these are the defaults
const testRunnerConsole = new TestRunnerConsole(runnerConfig);
testRunnerConsole.run(MyTest); // Add your test classes here. No array declaration needed.Running tests without visual output
You can also use your own code to parse and visualize the output of TestRunner.
import { TestRunner } from "@kompanie/testwerk";
import { MyOtherTests } from "./tests/myOtherTests.js";
const runnerConfig = {
asyncTestTimeout: 5000,
afterEachTimeout: 500,
beforeEachTimeout: 500
}; // Optional, these are the defaults
const testRunner = new TestRunner(runnerConfig);
const testResults = await testRunner.run(MyOtherTests); // Add your test classes here. No array declaration needed.This example shows what testResults can look like.
{
"completionTime": 1739974557847,
"executionTime": 3014,
"testResults": [
{
"name": "MyOtherTests",
"results": [
{
"name": "throws_shouldFail_whenFunctionDoesNotThrowAsync",
"executionTime": 0,
"error": {
"columnNumber": 8,
"fileName": "http://localhost:8000/source/assert.js",
"lineNumber": 1,
"message": "Expected an error, but none was thrown",
"stack": "AssertionError@http://localhost:8000/source/assert.js:1:8..."
}
},
{
"name": "throws_shouldPass_whenFunctionThrowsAsync",
"executionTime": 2
}
]
}
]
}Test project
Testwerk includes tests which can be run with the following command:
npm testYou can then open your browser. The test project outputs to the console and HTML. It also has three failing tests to show the visualization of failed tests in the console and the HTML.
