Optional
[SIGNAL]Bail out on the first failed test point
Numeric identifier for this test
Method that writes to stderr when debug: true
is set in the options,
or no-ops otherwise
Show diagnostics for this test. A value of null
means that
diagnostics will be shown only if the test is failing.
Set true when @tapjs/core!test-base.TestBase#end is called
number of spaces to indent the TAP stream
Nesting level, for serialization to node test runner
Note that this is zero for parent-less tests, and also zero for the first level of children.
Do not emit the TAP version 14
line at the start
Options provided to this test
Do not elide extraneous whitespace and empty lines.
true if the stream can be read
Set upon test completion when a child test is ready to be processed by its parent.
True if this test should be buffered and only emit data if it fails
Treat non-TAP data as an error.
May be set with pragma +strict
in the TAP stream, or unset with
pragma: -strict
.
Set to true when the test times out, so its failure status can be determined later.
true if the stream can be written
Static
Readonly
captureValue: Symbol.for('nodejs.rejection')
See how to write a custom rejection handler
.
Static
captureValue: boolean
Change the default captureRejections
option on all new EventEmitter
objects.
Static
defaultBy default, a maximum of 10
listeners can be registered for any single
event. This limit can be changed for individual EventEmitter
instances
using the emitter.setMaxListeners(n)
method. To change the default
for allEventEmitter
instances, the events.defaultMaxListeners
property
can be used. If this value is not a positive number, a RangeError
is thrown.
Take caution when setting the events.defaultMaxListeners
because the
change affects all EventEmitter
instances, including those created before
the change is made. However, calling emitter.setMaxListeners(n)
still has
precedence over events.defaultMaxListeners
.
This is not a hard limit. The EventEmitter
instance will allow
more listeners to be added but will output a trace warning to stderr indicating
that a "possible EventEmitter memory leak" has been detected. For any single
EventEmitter
, the emitter.getMaxListeners()
and emitter.setMaxListeners()
methods can be used to
temporarily avoid this warning:
import { EventEmitter } from 'node:events';
const emitter = new EventEmitter();
emitter.setMaxListeners(emitter.getMaxListeners() + 1);
emitter.once('event', () => {
// do stuff
emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
});
The --trace-warnings
command-line flag can be used to display the
stack trace for such warnings.
The emitted warning can be inspected with process.on('warning')
and will
have the additional emitter
, type
, and count
properties, referring to
the event emitter instance, the event's name and the number of attached
listeners, respectively.
Its name
property is set to 'MaxListenersExceededWarning'
.
Static
Readonly
errorThis symbol shall be used to install a listener for only monitoring 'error'
events. Listeners installed using this symbol are called before the regular 'error'
listeners are called.
Installing a listener using this symbol does not change the behavior once an 'error'
event is emitted. Therefore, the process will still crash if no
regular 'error'
listener is installed.
True if the stream has been aborted.
No-op setter. Stream aborted status is set via the AbortSignal provided in the constructor options.
true if this is an async stream
Set to true to make this stream async.
Once set, it cannot be unset, as this would potentially cause incorrect behavior. Ie, a sync stream can be made async, but an async stream cannot be safely made sync.
The amount of data stored in the buffer waiting to be read.
For Buffer strings, this will be the total byte length.
For string encoding streams, this will be the string character length,
according to JavaScript's string.length
logic.
For objectMode streams, this is a count of the items waiting to be
emitted.
Method that will be called on snapshot strings. This can be used to remove transient run-specific data from snapshots using simple string transforms.
Options that will be used when formatting snapshots and diffing/comparing objects using any assertion methods.
true if the stream has been forcibly destroyed
true if the 'end' event has been emitted
The BufferEncoding
currently in use, or null
true if the stream is currently in a flowing state, meaning that any writes will be immediately emitted.
Function that turns an object into a snapshot string.
By default tcompare!format is used. If a string is returned, then that string is the snapshot string. If any other type is returned, then the returned value will be formatted using tcompare!format.
The full name of the test, starting with the main script name, and including all parent names.
True if the test is currently in an idle state
True if this is an objectMode stream
true if the stream is currently in a paused state
The string signature of the plugins built into this Test class
Set whether the fixture should be saved or not
Must be set BEFORE calling @tapjs/fixture!index.TestFixtures#testdir, or it will not have any effect.
The file where snapshots will be written to and read from
Boolean indicating whether the underlying stream can be written to, or if it has been ended.
In the worker thread, the worker data that was provided to the t.worker method.
In the main thread, this field is undefined
.
Static
isAlias for isStream
Former export location, maintained for backwards compatibility.
Optional
[captureRest
...args: AnyRestAlias for Minipass#on
Rest
...args: TestBaseEvents[Event]output a TAP comment, formatted like console.log()
If the test is currently awaiting a child test, it will be deferred until after the child test completes.
If the test is already completed, the comment will be emitted on the parent, or if no parent is available, it will be printed to standard output.
Rest
...args: any[]Destroy a stream, preventing it from being used for any further purpose.
If the stream has a close()
method, then it will be called on
destruction.
After destruction, any attempt to write data, read data, or emit most events will be ignored.
If an error argument is provided, then it will be emitted in an 'error' event.
Optional
er: unknownReturns an array listing the events for which the emitter has registered
listeners. The values in the array are strings or Symbol
s.
import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.on('foo', () => {});
myEE.on('bar', () => {});
const sym = Symbol('symbol');
myEE.on(sym, () => {});
console.log(myEE.eventNames());
// Prints: [ 'foo', 'bar', Symbol(symbol) ]
Returns the current max listener value for the EventEmitter
which is either
set by emitter.setMaxListeners(n)
or defaults to defaultMaxListeners.
Returns the number of listeners listening for the event named eventName
.
If listener
is provided, it will return how many times the listener is found
in the list of the listeners of the event.
The name of the event being listened for
Optional
listener: FunctionThe event handler function
Returns a copy of the array of listeners for the event named eventName
.
server.on('connection', (stream) => {
console.log('someone connected!');
});
console.log(util.inspect(server.listeners('connection')));
// Prints: [ [Function] ]
Set some mocks that will be used for all subsequent @tapjs/mock!index.TapMock#mockImport and @tapjs/mock!index.TapMock#mockRequire calls made by this test.
Mocks added with mockAll
are overridden by any explicit mocks set in the
t.mockRequire
or t.mockImport
call.
Repeated calls to t.mockAll()
will add mocks to the set. If the same
name is used again, it will replace the previous value, not merge.
If a key is set to undefined
or null
, then it will be removed from
the mockAll
set.
Reset by calling t.mockAll(null)
Call with no args to return the current mockAll
object.
Optional
mocks: null | Record<string, any>Mostly identical to EventEmitter.off
If a 'data' event handler is removed, and it was the last consumer (ie, there are no pipe destinations or other 'data' event listeners), then the flow of data will stop until there is another consumer or Minipass#resume is explicitly called.
Rest
...args: TestBaseEvents[Event]Mostly identical to EventEmitter.on
, with the following
behavior differences to prevent data loss and unnecessary hangs:
Adding a 'data' event handler will trigger the flow of data
Adding a 'readable' event handler when there is data waiting to be read will cause 'readable' to be emitted immediately.
Adding an 'endish' event handler ('end', 'finish', etc.) which has already passed will cause the event to be emitted immediately and all handlers removed.
Adding an 'error' event handler after an error has been emitted will cause the event to be re-emitted immediately with the error previously raised.
Rest
...args: TestBaseEvents[Event]Adds a one-time listener
function for the event named eventName
. The
next time eventName
is triggered, this listener is removed and then invoked.
server.once('connection', (stream) => {
console.log('Ah, we have our first user!');
});
Returns a reference to the EventEmitter
, so that calls can be chained.
By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener()
method can be used as an alternative to add the
event listener to the beginning of the listeners array.
import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
// b
// a
The name of the event.
The callback function
Rest
...args: any[]Adds the listener
function to the beginning of the listeners array for the
event named eventName
. No checks are made to see if the listener
has
already been added. Multiple calls passing the same combination of eventName
and listener
will result in the listener
being added, and called, multiple times.
server.prependListener('connection', (stream) => {
console.log('someone connected!');
});
Returns a reference to the EventEmitter
, so that calls can be chained.
The name of the event.
The callback function
Rest
...args: any[]Adds a one-timelistener
function for the event named eventName
to the beginning of the listeners array. The next time eventName
is triggered, this
listener is removed, and then invoked.
server.prependOnceListener('connection', (stream) => {
console.log('Ah, we have our first user!');
});
Returns a reference to the EventEmitter
, so that calls can be chained.
The name of the event.
The callback function
Rest
...args: any[]Returns a copy of the array of listeners for the event named eventName
,
including any wrappers (such as those created by .once()
).
import { EventEmitter } from 'node:events';
const emitter = new EventEmitter();
emitter.once('log', () => console.log('log once'));
// Returns a new Array with a function `onceWrapper` which has a property
// `listener` which contains the original listener bound above
const listeners = emitter.rawListeners('log');
const logFnWrapper = listeners[0];
// Logs "log once" to the console and does not unbind the `once` event
logFnWrapper.listener();
// Logs "log once" to the console and removes the listener
logFnWrapper();
emitter.on('log', () => console.log('log persistently'));
// Will return a new Array with a single function bound by `.on()` above
const newListeners = emitter.rawListeners('log');
// Logs "log persistently" twice
newListeners[0]();
emitter.emit('log');
Low-level explicit read method.
In objectMode, the argument is ignored, and one item is returned if available.
n
is the number of bytes (or in the case of encoding streams,
characters) to consume. If n
is not provided, then the entire buffer
is returned, or null
is returned if no data is available.
If n
is greater that the amount of data in the internal buffer,
then null
is returned.
Optional
n: null | numberMostly identical to EventEmitter.removeAllListeners
If all 'data' event handlers are removed, and they were the last consumer (ie, there are no pipe destinations), then the flow of data will stop until there is another consumer or Minipass#resume is explicitly called.
Optional
ev: EventAlias for Minipass#off
Rest
...args: TestBaseEvents[Event]Resume the stream if it is currently in a paused state
If called when there are no pipe destinations or data
event listeners,
this will place the stream in a "discarded" state, where all data will
be thrown away. The discarded state is removed if a pipe destination or
data handler is added, if pause() is called, or if any synchronous or
asynchronous iteration is started.
By default EventEmitter
s will print a warning if more than 10
listeners are
added for a particular event. This is a useful default that helps finding
memory leaks. The emitter.setMaxListeners()
method allows the limit to be
modified for this specific EventEmitter
instance. The value can be set to Infinity
(or 0
) to indicate an unlimited number of listeners.
Returns a reference to the EventEmitter
, so that calls can be chained.
Similar to the normal @tapjs/core!test-base.TestBase#timeout, but
with the added feature that it will kill the process with SIGALRM
if it
has been registered, and will decorate the diagnostics with some
information about currently running handles and requests, as these may be
the reason the process is not gracefully closing in time.
The root test runner will time out if the process receives a SIGALRM
signal, or if it receives a timeout message via IPC or worker thread
channel.
Optional
expired?: stringOptional
signal?: null | SignalsFully unhook a piped destination stream.
If the destination stream was the only consumer of this stream (ie,
there are no other piped destinations or 'data'
event listeners)
then the flow of data will stop until there is another consumer or
Minipass#resume is explicitly called.
Static
addExperimental
Listens once to the abort
event on the provided signal
.
Listening to the abort
event on abort signals is unsafe and may
lead to resource leaks since another third party with the signal can
call e.stopImmediatePropagation()
. Unfortunately Node.js cannot change
this since it would violate the web standard. Additionally, the original
API makes it easy to forget to remove listeners.
This API allows safely using AbortSignal
s in Node.js APIs by solving these
two issues by listening to the event such that stopImmediatePropagation
does
not prevent the listener from running.
Returns a disposable so that it may be unsubscribed from more easily.
import { addAbortListener } from 'node:events';
function example(signal) {
let disposable;
try {
signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
disposable = addAbortListener(signal, (e) => {
// Do something when signal is aborted.
});
} finally {
disposable?.[Symbol.dispose]();
}
}
Disposable that removes the abort
listener.
Static
getReturns a copy of the array of listeners for the event named eventName
.
For EventEmitter
s this behaves exactly the same as calling .listeners
on
the emitter.
For EventTarget
s this is the only way to get the event listeners for the
event target. This is useful for debugging and diagnostic purposes.
import { getEventListeners, EventEmitter } from 'node:events';
{
const ee = new EventEmitter();
const listener = () => console.log('Events are fun');
ee.on('foo', listener);
console.log(getEventListeners(ee, 'foo')); // [ [Function: listener] ]
}
{
const et = new EventTarget();
const listener = () => console.log('Events are fun');
et.addEventListener('foo', listener);
console.log(getEventListeners(et, 'foo')); // [ [Function: listener] ]
}
Static
getReturns the currently set max amount of listeners.
For EventEmitter
s this behaves exactly the same as calling .getMaxListeners
on
the emitter.
For EventTarget
s this is the only way to get the max event listeners for the
event target. If the number of event handlers on a single EventTarget exceeds
the max set, the EventTarget will print a warning.
import { getMaxListeners, setMaxListeners, EventEmitter } from 'node:events';
{
const ee = new EventEmitter();
console.log(getMaxListeners(ee)); // 10
setMaxListeners(11, ee);
console.log(getMaxListeners(ee)); // 11
}
{
const et = new EventTarget();
console.log(getMaxListeners(et)); // 10
setMaxListeners(11, et);
console.log(getMaxListeners(et)); // 11
}
Static
listenerA class method that returns the number of listeners for the given eventName
registered on the given emitter
.
import { EventEmitter, listenerCount } from 'node:events';
const myEmitter = new EventEmitter();
myEmitter.on('event', () => {});
myEmitter.on('event', () => {});
console.log(listenerCount(myEmitter, 'event'));
// Prints: 2
The emitter to query
The event name
Static
onimport { on, EventEmitter } from 'node:events';
import process from 'node:process';
const ee = new EventEmitter();
// Emit later on
process.nextTick(() => {
ee.emit('foo', 'bar');
ee.emit('foo', 42);
});
for await (const event of on(ee, 'foo')) {
// The execution of this inner block is synchronous and it
// processes one event at a time (even with await). Do not use
// if concurrent execution is required.
console.log(event); // prints ['bar'] [42]
}
// Unreachable here
Returns an AsyncIterator
that iterates eventName
events. It will throw
if the EventEmitter
emits 'error'
. It removes all listeners when
exiting the loop. The value
returned by each iteration is an array
composed of the emitted event arguments.
An AbortSignal
can be used to cancel waiting on events:
import { on, EventEmitter } from 'node:events';
import process from 'node:process';
const ac = new AbortController();
(async () => {
const ee = new EventEmitter();
// Emit later on
process.nextTick(() => {
ee.emit('foo', 'bar');
ee.emit('foo', 42);
});
for await (const event of on(ee, 'foo', { signal: ac.signal })) {
// The execution of this inner block is synchronous and it
// processes one event at a time (even with await). Do not use
// if concurrent execution is required.
console.log(event); // prints ['bar'] [42]
}
// Unreachable here
})();
process.nextTick(() => ac.abort());
Use the close
option to specify an array of event names that will end the iteration:
import { on, EventEmitter } from 'node:events';
import process from 'node:process';
const ee = new EventEmitter();
// Emit later on
process.nextTick(() => {
ee.emit('foo', 'bar');
ee.emit('foo', 42);
ee.emit('close');
});
for await (const event of on(ee, 'foo', { close: ['close'] })) {
console.log(event); // prints ['bar'] [42]
}
// the loop will exit after 'close' is emitted
console.log('done'); // prints 'done'
Optional
options: StaticEventEmitterIteratorOptionsAn AsyncIterator
that iterates eventName
events emitted by the emitter
Optional
options: StaticEventEmitterIteratorOptionsStatic
onceCreates a Promise
that is fulfilled when the EventEmitter
emits the given
event or that is rejected if the EventEmitter
emits 'error'
while waiting.
The Promise
will resolve with an array of all the arguments emitted to the
given event.
This method is intentionally generic and works with the web platform EventTarget interface, which has no special'error'
event
semantics and does not listen to the 'error'
event.
import { once, EventEmitter } from 'node:events';
import process from 'node:process';
const ee = new EventEmitter();
process.nextTick(() => {
ee.emit('myevent', 42);
});
const [value] = await once(ee, 'myevent');
console.log(value);
const err = new Error('kaboom');
process.nextTick(() => {
ee.emit('error', err);
});
try {
await once(ee, 'myevent');
} catch (err) {
console.error('error happened', err);
}
The special handling of the 'error'
event is only used when events.once()
is used to wait for another event. If events.once()
is used to wait for the
'error'
event itself, then it is treated as any other kind of event without
special handling:
import { EventEmitter, once } from 'node:events';
const ee = new EventEmitter();
once(ee, 'error')
.then(([err]) => console.log('ok', err.message))
.catch((err) => console.error('error', err.message));
ee.emit('error', new Error('boom'));
// Prints: ok boom
An AbortSignal
can be used to cancel waiting for the event:
import { EventEmitter, once } from 'node:events';
const ee = new EventEmitter();
const ac = new AbortController();
async function foo(emitter, event, signal) {
try {
await once(emitter, event, { signal });
console.log('event emitted!');
} catch (error) {
if (error.name === 'AbortError') {
console.error('Waiting for the event was canceled!');
} else {
console.error('There was an error', error.message);
}
}
}
foo(ee, 'foo', ac.signal);
ac.abort(); // Abort waiting for the event
ee.emit('foo'); // Prints: Waiting for the event was canceled!
Optional
options: StaticEventEmitterOptionsOptional
options: StaticEventEmitterOptionsStatic
setimport { setMaxListeners, EventEmitter } from 'node:events';
const target = new EventTarget();
const emitter = new EventEmitter();
setMaxListeners(5, target, emitter);
Optional
n: numberA non-negative number. The maximum number of listeners per EventTarget
event.
Rest
...eventTargets: (EventEmitter<DefaultEventMap> | EventTarget)[]Returns the error object if it throws and that does not fail the test (by virtue of being marked skip or todo). Otherwise returns the passing status, like other assertions.
Rest
...__namedParameters: MessageExtraAsserts that the emitter emits the specified event before the test ends. Returns a promise that resolves when the event is emitted. Note that waiting on the returned promise within a test can deadlock the test, if the event never emits, but the returned promise can be a handy way to pause a test until an event happens, if you are reasonably confident that it will fire.
Rest
...__namedParameters: MessageExtraVerify that the values are equal
Rest
...__namedParameters: MessageExtraAssert that an error object is not provided.
Like with @tapjs/asserts!index.Assertions#doesNotThrow, you can also usually just throw the error, and tap will handle that reasonably.
This is useful in cases where you just want to assert that a callback did not receive an error, without necessarily aborting the callback function entirely. Both the origin of the error and the location of the failed assertion will be printed in the test results.
It is also used internally in @tapjs/asserts!index.Assertions#resolves, @tapjs/asserts!index.Assertions#doesNotThrow, and @tapjs/asserts!index.Assertions#resolveMatch to show both the source of a raised error as well as the callsite where the assertion failed.
Rest
...__namedParameters: MessageExtraA failing (not ok) Test Point
Rest
...__namedParameters: MessageExtraVerify that the object has all of the properties and values in the pattern, matching loosely.
Rest
...__namedParameters: MessageExtraVerify that the object has the wanted property, using Object#hasOwnProperty
Rest
...__namedParameters: MessageExtraVerify that the object has all of the properties listed in the
wanted
list, using Object#hasOwnProperties()
Rest
...__namedParameters: MessageExtraVerify that the object has all of the properties listed in the
wanted
list, using Object#hasOwnProperties(), and no others
Rest
...__namedParameters: MessageExtraVerify that the object has the wanted property, anywhere in its prototype chain.
Rest
...__namedParameters: MessageExtraVerify that the object has all of the properties in the wanted
list, anywhere in its prototype chain.
Rest
...__namedParameters: MessageExtraVerify that the value has all of the properties and values in the pattern, matching strictly.
Rest
...__namedParameters: MessageExtraVerify that the value matches the pattern provided
Rest
...__namedParameters: MessageExtraVerify that the value matches the pattern provided, with no extra properties.
Rest
...__namedParameters: MessageExtraVerify that the value matches the pattern provided, with no extra properties.
Rest
...__namedParameters: MessageExtraIn --snapshot
mode, takes a snapshot of the object provided, and writes
to the snapshot file.
Otherwise, reads the snapshot file, and verifies that a snapshot of the object provided matches the stored snapshot.
Rest
...__namedParameters: MessageExtraVerify that the value matches the pattern provided, but fail if any fields only match via type coercion.
For example,
t.matchStrict({ a: 1 }, { a: Number }, 'this passes')
t.matchStrict({ a: 1 }, { a: '1' }, 'this fails')
Rest
...__namedParameters: MessageExtraVerify that the values are not equal
Rest
...__namedParameters: MessageExtraVerify that the object does NOT have all of the properties and values in the pattern, matching loosely.
Rest
...__namedParameters: MessageExtraVerify that the value does NOT contain all of the properties and values in the test pattern, comparing strictly.
Note that this will pass if the value has some of the listed properties, or if they do not match the same type.
Rest
...__namedParameters: MessageExtraVerify that the value does NOT match the pattern provided.
Rest
...__namedParameters: MessageExtraVerify that the value does not match the pattern provided, with no extra properties. Ie, it might either not match, or have extra props.
Rest
...__namedParameters: MessageExtraVerify that the value does not match the pattern provided, with no extra properties. Ie, it might either not match, or have extra props.
Rest
...__namedParameters: MessageExtraVerify that the value does not match the pattern provided, without type coercion.
Rest
...__namedParameters: MessageExtraVerify that the value is not truthy
Rest
...__namedParameters: MessageExtraVerify that the value is not loosely equivalent to the supplied pattern
Rest
...__namedParameters: MessageExtraVerify that the value is truthy
Rest
...__namedParameters: MessageExtraA passing (ok) Test Point.
Rest
...__namedParameters: MessageExtraresolves to the error object rejected if it rejects as expected, 'false' if it does not, or 'true' if it fails to reject but is marked as skip/todo.
Rest
...__namedParameters: ThrowsArgsTest the resolved promise result with t.match()
Resolves to true if it passes, false if the promise rejects or the match fails, or the rejection error value if the promise rejects but the assertion passes by being marked todo/skip.
Rest
...__namedParameters: MessageExtraResolve a promise, and verify that the resulting value matches the snapshot.
Rest
...__namedParameters: MessageExtraResolves to 'true' if the promise resolves successfully, 'false' if it rejects and fails, or the rejection error if it rejects but the failure is accepted by by being marked todo or skip
Rest
...__namedParameters: MessageExtraVerify that the value is loosely equivalent to the supplied pattern
Rest
...__namedParameters: MessageExtraVerify that the value is not strictly equivalent to the supplied pattern object
Rest
...__namedParameters: MessageExtraVerify that the value is strictly equivalent to the supplied pattern
Rest
...__namedParameters: MessageExtraVerify that the function throws an error.
Thrown error is tested against the wanted
param if provided, using
t.match()
.
Returns false on failure, or the error object thrown on success
Rest
...__namedParameters: ThrowsArgsVerify that the value is of the type specified Type can be either a string, or a constructor.
If a string, then it can match either the typeof
result
or 'null' for null
values, or the name
property of the
object's constructor.
Rest
...__namedParameters: MessageExtraSubtests that are currently in process.
Internal
bufferedTrue if this test should be buffered and only processed on completion
Optional
Internal
cbFunction that will get this test as an argument when it is processed
Optional
deferredA Deferred
promise wrapper that is resolved when this test completes.
Optional
doneA promise that resolves when the test is done.
TapWrap AsyncResource that limits the async-hook-domain
the async-hook-domain that catches throws and Promise rejections
Internal
outputthe TAP stream data for buffered tests
Optional
parentParent test of this TAP stream
The tap parser attached to this TAP stream
The pool of parallel tests currently in process
Queue of items awaiting processing. Can be any @tapjs/core!test-base.QueueEntry item.
high resolution bigint time when this test started
Array of all subtests that have been added/scheduled, and have not yet completed.
Optional
timerThe timer that fires when the test times out
The current assertion being processed. Set at the start of all assertions, and used for calculating stack traces.
Internal
EventEmitter emit method, but closes the
@tapjs/core!base.Base#hook and
@tapjs/core!base.Base#hookDomain when emitting 'end'
.
Rest
...data: TestBaseEvents[Event]Internal
Called when a test times out or bails out, or the process ends, marking all currently active or queued subtests as incomplete.
No need to ever call this directly, exposed so that it can be extended by @tapjs/core!spawn.Spawn and @tapjs/core!worker.Worker, which have special behaviors that are required when a process hangs indefinitely.
Optional
sub: booleanInternal
Method called when the parser encounters a bail out
To listen to bailout events, listen to the @tapjs/core!base.TapBaseEvents#bailout event:
t.on('bailout', message => {
// test bailed out!
})
Optional
reason: stringInternal
Method called when the parser completes and emits its final results
Extended by @tapjs/core!worker.Worker and @tapjs/core!tap.TAP classes
Internal
Run the main
test function. Called by
@tapjs/core!test-base.TestBase when starting a subtest.
Initializes the TapWrap hook
Internal
Return true if the child test represented by the options object
should be skipped. Extended by the @tapjs/filter
plugin.
Internal
Await the end of a Promise before proceeding. The supplied callback is called with the Waiter object.
This is internal, used in some plugins when a promise must be awaited
before proceeding. In normal test usage, it's usually best to simply use
an async test function and await
promises as normal.
Add a plugin at run-time.
Creates a subclass of @tapjs/test!index.Test which has the specified plugin, and which applies the plugin to all child tests it creates.
Typically, it's best to load plugins using configuration, set via the
tap plugin <add|rm>
command.
However, in some cases, for example while developing plugins or if a certain plugin is only needed in a small number of tests, it can be useful to apply it after the fact.
This is best used sparingly, as it may result in poor typescript compilation performance, which can manifest in slower test start-up times and lag loading autocomplete in editors. If you find yourself calling applyPlugin often, consider whether it'd be better to just add the plugin to the entire test suite, so that it can be built up front.
The name of the folder that this test will use with @tapjs/fixture!index.TestFixtures#testdir.
By default, it uses a folder name based on the name of the test file
and subtest, within .tap/fixtures
in the root of the project.
Intercept calls to a method to track the arguments, call site, and return/throw status, and replace the implementation.
By default, the method is set to a no-op. To retain the method behavior, pass the current value of the method as the third argument. For example:
const results = t.capture(obj, 'foo', obj.foo)
Automatically restores at t.teardown()
if the @tapjs/after
plugin is not disabled. Otherwise, it is important to call the
restore()
method on the returned function when you are done capturing.
Just wrap the function and return it. Does not have any logic to restore, since it's not actually modifying anything. The results hang off the function as the 'calls' property.
The added fn.args()
method will return an array of the arguments
passed to each call since the last time it was inspected.
Convenience method to create a mock from an existing object by overriding some (possibly deeply nested) methods or properties.
Example:
import * as fs from 'node:fs'
const mockedThing = t.mockRequire('./module.js', t.createMock(
{ fs },
{ fs: { statSync: myMockedStatSync }}
)
This can also appear anywhere in the object hierarchy, which may be more convenient in some cases:
import * as blah from '@long-name/blah-api'
const mockedThing = t.mockRequire('./module.js', {
fs: t.createMock(fs, { statSync: myMockedStatSync }),
child_process: t.createMock(child_process, { spawn: mockSpawn }),
'@long-name/blah-api': t.createMock(blah, {
some: {
nested: {
prop: true
}
}
})
})
To remove a property, set it as undefined in the override.
Create a fixture object for use in a @tapjs/fixture!index.TestFixtures#testdir method.
Intercept and track object property sets and gets.
If a PropertyDescriptor is set, then it will be used as the replacement value. Otherwise, the original descriptor will be used.
If the strictMode
param is set, then attempts to write to read-only
properties will throw an error.
Deprecated alias for @tapjs/mock!index.TapMock#mockRequire
Prints a warning to stderr the first time it used, otherwise identical.
Optional
mocks: { Load the supplied module asynchronously using import(), replacing any of the referenced modules with the mocks provided.
Works with either ESM or CommonJS modules, but as with import()
of
CommonJS modules, the module.exports
value will be set as the
default
property on the resolved object, making
@tapjs/mock!index.TapMock#mockRequire somewhat more intuitive in
those cases.
For type info, cast using as typeof import(...)
or use the type
parameter, as TypeScript lacks a way to infer imports dynamically.
For example:
const myThing = await t.mockImport<
typeof import('../my-thing.js')
>('../my-thing.js', {
some: { tricky: 'mocks' },
})
Note: The terms "mock" and "import" are unfortunately very overloaded in the testing space. This is not "mock all imports of this module". It's "load this module, but with its imports mocked". The code of the target module is run normally, but its dependencies are injected with the supplied values, which is useful for triggering hard-to-reach error cases and other situations.
It is also useful for just loading a fresh copy of a module in your tests, if for example your program behaves differently based on environment variables or other system settings. For example:
t.test('windows behavior', async t => {
t.intercept(process, 'platform', { value: 'win32' })
const myThing = t.mockImport('../my-thing.js')
t.equal(myThing.separator, '\\')
})
t.test('posix behavior', async t => {
t.intercept(process, 'platform', { value: 'linux' })
const myThing = t.mockImport('../my-thing.js')
t.equal(myThing.separator, '/')
})
Optional
mocks: Record<string, any>Load the supplied module synchronously using require()
,
replacing any of the referenced modules with the mocks provided.
Only works with CommonJS modules.
For type info, cast using as typeof import(...)
or use the type
parameter, as TypeScript lacks a way to infer imports dynamically.
For example:
const myThing = t.mockRequire<
typeof import('../my-thing.js')
>('../my-thing.js', {
some: { tricky: 'mocks' },
})
Optional
mocks: Record<string, any>Create a test directory, optionally filling it up with contents
The testdir will be automatically deleted at the end of the test.
To not delete the directory after the test, use the
saveFixture: true
option when creating the test, or specify
--save-fixture
on the command line or in the tap configuration.
Optional
content: FixtureDirContentRun a child test that will run when the --only
config is set,
or { runOnly: true }
is set in the parent test options.
Create a subtest which is marked as skip
Spawn a child process and parse its standard output as a subtest
Optional
name: stringOptional
name: stringOptional
name: stringParse standard input as a child test
Parse stdin as the only tap stream (ie, not as a child test) If used, then no other subtests or assertions are allowed.
Optional
extra: T & { Internal
Mount a subtest, using this Test object as a harness.
Exposed so that it can be used by some builtin plugins, but perhaps
the least convenient way imaginable to create subtests. Just use
t.test()
to do that, it's much easier.
Create a child Test object and parse its output as a subtest
Create a subtest which is marked as todo
Start a Node Worker thread and parse its standard output as a child test
Optional
name: stringOptional
name: stringThe number of subtests to run in parallel, if allowed
Run the supplied function after every child test, and any of those child tests' children, and so on.
The test that has just completed is passed in as an argument to the function. Note that at this point, the test is fully ended, so attempting to call assertion methods on it will raise an error.
Run the supplied function before any child tests, and all of their children, and so on.
The test about to run is an argument to the function. While its test
method has not yet run, it is safe to call test methods on it, but note
that this may potentially be confusing if for example you call t.plan()
and this conflicts with the t.plan()
called in the test method.
Set the amount of time in milliseconds before this test is considered
a timeout. The time is counted from right now, so for example, repeatedly
calling t.setTimeout(100)
can keep it going indefinitely, as long as
you call it more often than every 100ms.
Calling setTimeout(0)
will remove the timer and allow the test to run
indefinitely.
Alias for @tapjs/after!After#after
True if this test emitted a bailout
The count of all assertions made directly on this test.
The count of all assertions that this stream emitted
Unrecoverable TAP protocol errors in the stream
High resolution time in ns that this test took to complete.
True if in the main thread. False when running in a worker thread spawned by t.worker
Lists of todo, skip, and failure test points. If passes: true
is
set in the options, then passing test points will also be tracked.
the name of this test
Optional
resultsSet on completion. The results of the test run.
Attached when the Test class is instantiated from a TestBase, as a reference to the final plugged-in Test instance. If TestBase is used directly (outside the context of a plugin) or during plugin setup time, this will be undefined, so watch out.
Amount of time in milliseconds that this test took to complete.
true if the test is currently waiting for something to finish
True if the test has printed some output of any kind
true if the test has printed at least one TestPoint
Returns true if this test has begun
This is a singleton subclass of the @tapjs/test!index.Test base class.
Instantiate it by calling the exported @tapjs/core!tap.tap method.
It has all of the same plugins, fields, properties etc of a "normal" Test object, but with some additional characteristics to make it suitable for use as the root test runner.
The @tapjs/core!tap.TAP#register method will hook onto the process object, to set the exit code to 1 if there are test failures, and ignore any
EPIPE
errors that happen on stdout. (This is quite common in cases where a test aborts, and then attempts to write more data.)A brief summary is printed at the end of the test run.
If piped to stdout, then
this.register()
will be called automatically.If not piped anywhere else, the first time it writes any data, it will begin piping to stdout.
Options are set based on relevant environment variables, rather than taking an options object, since in normal cases, it will be instantiated automatically before any user code is run.
The test will automatically implicitly end when the process exits. If there are any unfinished tests at this time, they will be emitted as failures.
If a
teardown
function is added, then the test will automatically implicitly end if it is idle for 3 consecutivesetTimeout
deferrals. This is a bit of a kludge, but it allows tests to run servers or other things that would prevent a graceful process exit, and close them down in a teardown function.Lastly, since test files are often spawned by the runner using
t.spawn()
, this class listens for the timeout signal, and attempts to print diagnostic information about currently active handles and requests, as these are usually the cause of a test hanging indefinitely.