When a test fails via `common.mustNotCall` it is sometimes hard to determine exactly what was called. This modification stores the caller's file and line number by using the V8 Error API to capture a stack at the time `common.mustNotCall()` is called. In the event of failure, this information is printed. This change also exposes a new function in test/common, `getCallSite()` which accepts a `function` and returns a `String` with the file name and line number for the function. Backport-PR-URL: https://github.com/nodejs/node/pull/19355 PR-URL: https://github.com/nodejs/node/pull/17257 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Khaidi Chu <i@2333.moe>
14 KiB
Node.js Core Test Common Modules
This directory contains modules used to test the Node.js implementation.
Table of Contents
Common Module API
The common module is used by tests for consistency across repeated
tasks.
allowGlobals(...whitelist)
Takes whitelist and concats that with predefined knownGlobals.
arrayStream
A stream to push an array into a REPL
busyLoop(time)
time<Number>
Blocks for time amount of time.
canCreateSymLink()
- return [<Boolean>]
Checks whether the current running process can create symlinks. On Windows, this
returns false if the process running doesn't have privileges to create
symlinks
(SeCreateSymbolicLinkPrivilege).
On non-Windows platforms, this always returns true.
crashOnUnhandledRejection()
Installs a process.on('unhandledRejection') handler that crashes the process
after a tick. This is useful for tests that use Promises and need to make sure
no unexpected rejections occur, because currently they result in silent
failures.
ddCommand(filename, kilobytes)
- return <Object>
Platform normalizes the dd command
enoughTestMem
Indicates if there is more than 1gb of total memory.
expectsError(settings)
-
settings<Object> with the following optional properties:code<String> expected error must have this value for itscodepropertytype<Function> expected error must be an instance oftypemessage<String> or <RegExp> if a string is provided formessage, expected error must have it for itsmessageproperty; if a regular expression is provided formessage, the regular expression must match themessageproperty of the expected error
-
return function suitable for use as a validation function passed as the second argument to
assert.throws()
The expected error should be subclassed by the internal/errors module.
expectWarning(name, expected)
Tests whether name and expected are part of a raised warning.
fileExists(pathname)
Checks if pathname exists
fixturesDir
Path to the 'fixtures' directory.
getArrayBufferViews(buf)
buf<Buffer>- return <ArrayBufferView[]>
Returns an instance of all possible ArrayBufferViews of the provided Buffer.
getCallSite(func)
func[<Function>]- return [<String>]
Returns the file name and line number for the provided Function.
globalCheck
Set to false if the test should not check for global leaks.
hasCrypto
Indicates whether OpenSSL is available.
hasFipsCrypto
Indicates hasCrypto and crypto with fips.
hasIPv6
Indicates whether IPv6 is supported on this platform.
hasMultiLocalhost
Indicates if there are multiple localhosts available.
inFreeBSDJail
Checks whether free BSD Jail is true or false.
isAix
Platform check for Advanced Interactive eXecutive (AIX).
isAlive(pid)
Attempts to 'kill' pid
isFreeBSD
Platform check for Free BSD.
isLinux
Platform check for Linux.
isLinuxPPCBE
Platform check for Linux on PowerPC.
isOSX
Platform check for macOS.
isSunOS
Platform check for SunOS.
isWindows
Platform check for Windows.
isWOW64
Platform check for Windows 32-bit on Windows 64-bit.
leakedGlobals
Indicates whether any globals are not on the knownGlobals list.
localhostIPv4
IP of localhost.
localIPv6Hosts
Array of IPV6 representations for localhost.
mustCall([fn][, exact])
fn<Function> default = () => {}exact<Number> default = 1- return <Function>
Returns a function that calls fn. If the returned function has not been called
exactly expected number of times when the test is complete, then the test will
fail.
If fn is not provided, an empty function will be used.
mustCallAtLeast([fn][, minimum])
fn<Function> default = () => {}minimum<Number> default = 1- return <Function>
Returns a function that calls fn. If the returned function has not been called
at least minimum number of times when the test is complete, then the test will
fail.
If fn is not provided, an empty function will be used.
mustNotCall([msg])
msg<String> default = 'function should not have been called'- return <Function>
Returns a function that triggers an AssertionError if it is invoked. msg is used as the error message for the AssertionError.
nodeProcessAborted(exitCode, signal)
Returns true if the exit code exitCode and/or signal name signal represent the exit code and/or signal name of a node process that aborted, false otherwise.
opensslCli
Indicates whether 'opensslCli' is supported.
platformTimeout(ms)
Platform normalizes timeout.
PIPE
Path to the test socket.
PORT
- <Number> default =
12346
A port number for tests to use if one is needed.
printSkipMessage(msg)
msg<String>
Logs '1..0 # Skipped: ' + msg
refreshTmpDir
- return <String>
Deletes the 'tmp' dir and recreates it
rootDir
Path to the 'root' directory. either / or c:\\ (windows)
skip(msg)
msg<String>
Logs '1..0 # Skipped: ' + msg and exits with exit code 0.
spawnPwd(options)
Platform normalizes the pwd command.
spawnSyncPwd(options)
Synchronous version of spawnPwd.
tmpDir
The realpath of the 'tmp' directory.
tmpDirName
- return <String>
Name of the temp directory used by tests.
Countdown Module
The Countdown module provides a simple countdown mechanism for tests that
require a particular action to be taken after a given number of completed
tasks (for instance, shutting down an HTTP server after a specific number of
requests).
const Countdown = require('../common/countdown');
function doSomething() {
console.log('.');
}
const countdown = new Countdown(2, doSomething);
countdown.dec();
countdown.dec();
new Countdown(limit, callback)
limit{number}callback{function}
Creates a new Countdown instance.
Countdown.prototype.dec()
Decrements the Countdown counter.
Coutndown.prototype.remaining
Specifies the remaining number of times Countdown.prototype.dec() must be
called before the callback is invoked.
Duplex pair helper
The common/duplexpair module exports a single function makeDuplexPair,
which returns an object { clientSide, serverSide } where each side is a
Duplex stream connected to the other side.
There is no difference between client or server side beyond their names.
Fixtures Module
The common/fixtures module provides convenience methods for working with
files in the test/fixtures directory.
fixtures.fixturesDir
- [<String>]
The absolute path to the test/fixtures/ directory.
fixtures.path(...args)
...args[<String>]
Returns the result of path.join(fixtures.fixturesDir, ...args).
fixtures.readSync(args[, enc])
args[<String>] | [<Array>]
Returns the result of
fs.readFileSync(path.join(fixtures.fixturesDir, ...args), 'enc').
fixtures.readKey(arg[, enc])
arg[<String>]
Returns the result of
fs.readFileSync(path.join(fixtures.fixturesDir, 'keys', arg), 'enc').
WPT Module
The wpt.js module is a port of parts of W3C testharness.js for testing the Node.js WHATWG URL API implementation with tests from W3C Web Platform Tests.