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>
374 lines
14 KiB
Markdown
374 lines
14 KiB
Markdown
# Node.js Core Test Common Modules
|
|
|
|
This directory contains modules used to test the Node.js implementation.
|
|
|
|
## Table of Contents
|
|
|
|
* [Common module API](#common-module-api)
|
|
* [Duplex pair helper](#duplex-pair-helper)
|
|
* [WPT module](#wpt-module)
|
|
|
|
## Common Module API
|
|
|
|
The `common` module is used by tests for consistency across repeated
|
|
tasks.
|
|
|
|
### allowGlobals(...whitelist)
|
|
* `whitelist` [<Array>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) Array of Globals
|
|
* return [<Array>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
|
|
|
|
Takes `whitelist` and concats that with predefined `knownGlobals`.
|
|
|
|
### arrayStream
|
|
A stream to push an array into a REPL
|
|
|
|
### busyLoop(time)
|
|
* `time` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)
|
|
|
|
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](https://msdn.microsoft.com/en-us/library/windows/desktop/bb530716(v=vs.85).aspx)).
|
|
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>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
|
|
Platform normalizes the `dd` command
|
|
|
|
### enoughTestMem
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Indicates if there is more than 1gb of total memory.
|
|
|
|
### expectsError(settings)
|
|
* `settings` [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
with the following optional properties:
|
|
* `code` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
expected error must have this value for its `code` property
|
|
* `type` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)
|
|
expected error must be an instance of `type`
|
|
* `message` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
or [<RegExp>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)
|
|
if a string is provided for `message`, expected error must have it for its
|
|
`message` property; if a regular expression is provided for `message`, the
|
|
regular expression must match the `message` property 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](https://github.com/nodejs/node/blob/master/doc/guides/using-internal-errors.md#api).
|
|
|
|
### expectWarning(name, expected)
|
|
* `name` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
* `expected` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) | [<Array>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
|
|
|
|
Tests whether `name` and `expected` are part of a raised warning.
|
|
|
|
### fileExists(pathname)
|
|
* pathname [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Checks if `pathname` exists
|
|
|
|
### fixturesDir
|
|
* [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
|
|
Path to the 'fixtures' directory.
|
|
|
|
### getArrayBufferViews(buf)
|
|
* `buf` [<Buffer>](https://nodejs.org/api/buffer.html#buffer_class_buffer)
|
|
* return [<ArrayBufferView[]>](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView)
|
|
|
|
Returns an instance of all possible `ArrayBufferView`s of the provided Buffer.
|
|
|
|
### getCallSite(func)
|
|
* `func` [<Function>]
|
|
* return [<String>]
|
|
|
|
Returns the file name and line number for the provided Function.
|
|
|
|
### globalCheck
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Set to `false` if the test should not check for global leaks.
|
|
|
|
### hasCrypto
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Indicates whether OpenSSL is available.
|
|
|
|
### hasFipsCrypto
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Indicates `hasCrypto` and `crypto` with fips.
|
|
|
|
### hasIPv6
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Indicates whether `IPv6` is supported on this platform.
|
|
|
|
### hasMultiLocalhost
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Indicates if there are multiple localhosts available.
|
|
|
|
### inFreeBSDJail
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Checks whether free BSD Jail is true or false.
|
|
|
|
### isAix
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Platform check for Advanced Interactive eXecutive (AIX).
|
|
|
|
### isAlive(pid)
|
|
* `pid` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)
|
|
* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Attempts to 'kill' `pid`
|
|
|
|
### isFreeBSD
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Platform check for Free BSD.
|
|
|
|
### isLinux
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Platform check for Linux.
|
|
|
|
### isLinuxPPCBE
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Platform check for Linux on PowerPC.
|
|
|
|
### isOSX
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Platform check for macOS.
|
|
|
|
### isSunOS
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Platform check for SunOS.
|
|
|
|
### isWindows
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Platform check for Windows.
|
|
|
|
### isWOW64
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Platform check for Windows 32-bit on Windows 64-bit.
|
|
|
|
### leakedGlobals
|
|
* [<Array>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
|
|
|
|
Indicates whether any globals are not on the `knownGlobals` list.
|
|
|
|
### localhostIPv4
|
|
* [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
|
|
IP of `localhost`.
|
|
|
|
### localIPv6Hosts
|
|
* [<Array>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
|
|
|
|
Array of IPV6 representations for `localhost`.
|
|
|
|
### mustCall([fn][, exact])
|
|
* `fn` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = () => {}
|
|
* `exact` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = 1
|
|
* return [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = () => {}
|
|
* `minimum` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = 1
|
|
* return [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) default = 'function should not have been called'
|
|
* return [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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)
|
|
* `exitCode` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)
|
|
* `signal` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
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
|
|
* [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
|
|
Indicates whether 'opensslCli' is supported.
|
|
|
|
### platformTimeout(ms)
|
|
* `ms` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)
|
|
* return [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)
|
|
|
|
Platform normalizes timeout.
|
|
|
|
### PIPE
|
|
* [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
|
|
Path to the test socket.
|
|
|
|
### PORT
|
|
* [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = `12346`
|
|
|
|
A port number for tests to use if one is needed.
|
|
|
|
### printSkipMessage(msg)
|
|
* `msg` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
|
|
Logs '1..0 # Skipped: ' + `msg`
|
|
|
|
### refreshTmpDir
|
|
* return [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
|
|
Deletes the 'tmp' dir and recreates it
|
|
|
|
### rootDir
|
|
* [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
|
|
Path to the 'root' directory. either `/` or `c:\\` (windows)
|
|
|
|
### skip(msg)
|
|
* `msg` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
|
|
Logs '1..0 # Skipped: ' + `msg` and exits with exit code `0`.
|
|
|
|
### spawnPwd(options)
|
|
* `options` [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
* return [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
|
|
Platform normalizes the `pwd` command.
|
|
|
|
### spawnSyncPwd(options)
|
|
* `options` [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
* return [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
|
|
Synchronous version of `spawnPwd`.
|
|
|
|
### tmpDir
|
|
* [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
|
|
The realpath of the 'tmp' directory.
|
|
|
|
### tmpDirName
|
|
* return [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
|
|
|
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).
|
|
|
|
<!-- eslint-disable strict, required-modules -->
|
|
```js
|
|
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](https://github.com/w3c/testharness.js) for testing the
|
|
Node.js
|
|
[WHATWG URL API](https://nodejs.org/api/url.html#url_the_whatwg_url_api)
|
|
implementation with tests from
|
|
[W3C Web Platform Tests](https://github.com/w3c/web-platform-tests).
|