What is @tapjs/test?
@tapjs/test is a testing framework for Node.js that provides a simple and powerful way to write and run tests. It is part of the TAP (Test Anything Protocol) ecosystem and is designed to be easy to use while offering a wide range of features for testing JavaScript code.
What are @tapjs/test's main functionalities?
Basic Test
This feature allows you to write basic tests using the @tapjs/test framework. The example demonstrates a simple test that checks if 1 + 1 equals 2.
const t = require('@tapjs/test');
t.test('basic test', t => {
t.equal(1 + 1, 2, '1 + 1 should equal 2');
t.end();
});
Asynchronous Test
This feature supports asynchronous tests. The example shows how to write a test that waits for a promise to resolve and then checks the result.
const t = require('@tapjs/test');
t.test('async test', async t => {
const result = await Promise.resolve(42);
t.equal(result, 42, 'result should be 42');
t.end();
});
Nested Tests
This feature allows you to create nested tests. The example demonstrates a parent test containing a child test.
const t = require('@tapjs/test');
t.test('parent test', t => {
t.test('child test', t => {
t.ok(true, 'this is a child test');
t.end();
});
t.end();
});
Assertions
This feature provides various assertion methods. The example shows how to use different assertions like ok, equal, and notEqual.
const t = require('@tapjs/test');
t.test('assertions', t => {
t.ok(true, 'true is truthy');
t.equal(3, 3, '3 equals 3');
t.notEqual(3, 4, '3 does not equal 4');
t.end();
});
Other packages similar to @tapjs/test
mocha
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. It provides a variety of interfaces for writing tests, including BDD, TDD, and exports-style. Compared to @tapjs/test, Mocha offers more flexibility in terms of test interfaces and is widely used in the JavaScript community.
jest
Jest is a delightful JavaScript testing framework with a focus on simplicity. It works out of the box for most JavaScript projects and provides a rich API for writing tests. Jest includes features like snapshot testing, a built-in mocking library, and code coverage reports. Compared to @tapjs/test, Jest is more opinionated and comes with more built-in features, making it a popular choice for React applications.
ava
AVA is a test runner for Node.js with a concise API, detailed error output, and support for new language features. It runs tests concurrently, which can lead to faster test execution. Compared to @tapjs/test, AVA emphasizes simplicity and performance, making it a good choice for projects that require fast and straightforward testing.
@tapjs/test
The plugin-ified Test
class in node-tap.
Class Test
This is the object that's actually provided to your tests. When
you do t.pass('this is fine')
, this is the t
.
It's also the base class of the TAP
class which is the default
export of the 'tap'
module, so anything on Test
is also on
TAP
.
The Test
class itself adds only the following methods, but it
has the sum of all methods provided by all loaded plugins.
t.applyPlugin(plugin: TapPlugin): Test
This returns a new version of the Test object which has the
plugin applied.
Note that it's actually a different object, but anything done to
the copy will also affect the base, and it will inherit all
properties and methods that the base has, so the new object can
be used in place of the original.
For example:
import t from 'tap'
const plugin = t => ({
hello: () => console.log('hello from ', t.name),
blowUp: () => t.fail('blowing up'),
})
t.test('apply a plugin', original => {
const t = original.applyPlugin(plugin)
console.log(typeof original.hello)
console.log(typeof t.hello)
t.hello()
t.blowUp()
console.log(original.passing())
t.end()
})
t.pluginLoaded(plugin: TapPlugin): boolean
Returns true if the plugin is loaded.
Also asserts that t
implements the type returned by the plugin
function.
So, for example, if a plugin might be loaded, you can use this
to get TypeScript to know about it.
import t from 'tap'
import { Test } from '@tapjs/test'
const plugin = t => ({
hello: () => console.log('hello from ', t.name),
blowUp: () => t.fail('blowing up'),
})
const maybeBlowup = (t: Test) => {
if (t.pluginLoaded(plugin)) {
t.blowUp()
} else {
t.pass('no blowup required')
}
}
t.test('maybe blow up', original => {
const t = original.applyPlugin(plugin)
maybeBlowup(original)
maybeBlowup(t)
t.end()
})
t.test()
, t.todo()
, t.skip()
Creates a subtest. You've seen this one before. This is the class
that implements it.
signature: string
The signature of the plugins built into this Test class.
loaders: string[]
The loaders added by plugins.
testFileExtensions: Set<string>
The test file extensions added by plugins.