-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test_runner: add assert.register() API
This commit adds a top level assert.register() API to the test runner. This function allows users to define their own custom assertion functions on the TestContext. Fixes: #52033 PR-URL: #56434 Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Pietro Marchini <[email protected]>
- Loading branch information
Showing
5 changed files
with
166 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
const { | ||
SafeMap, | ||
} = primordials; | ||
const { | ||
validateFunction, | ||
validateString, | ||
} = require('internal/validators'); | ||
const assert = require('assert'); | ||
const methodsToCopy = [ | ||
'deepEqual', | ||
'deepStrictEqual', | ||
'doesNotMatch', | ||
'doesNotReject', | ||
'doesNotThrow', | ||
'equal', | ||
'fail', | ||
'ifError', | ||
'match', | ||
'notDeepEqual', | ||
'notDeepStrictEqual', | ||
'notEqual', | ||
'notStrictEqual', | ||
'partialDeepStrictEqual', | ||
'rejects', | ||
'strictEqual', | ||
'throws', | ||
]; | ||
let assertMap; | ||
|
||
function getAssertionMap() { | ||
if (assertMap === undefined) { | ||
assertMap = new SafeMap(); | ||
|
||
for (let i = 0; i < methodsToCopy.length; i++) { | ||
assertMap.set(methodsToCopy[i], assert[methodsToCopy[i]]); | ||
} | ||
} | ||
|
||
return assertMap; | ||
} | ||
|
||
function register(name, fn) { | ||
validateString(name, 'name'); | ||
validateFunction(fn, 'fn'); | ||
const map = getAssertionMap(); | ||
map.set(name, fn); | ||
} | ||
|
||
module.exports = { getAssertionMap, register }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('node:assert'); | ||
const { test, assert: testAssertions } = require('node:test'); | ||
|
||
testAssertions.register('isOdd', (n) => { | ||
assert.strictEqual(n % 2, 1); | ||
}); | ||
|
||
testAssertions.register('ok', () => { | ||
return 'ok'; | ||
}); | ||
|
||
testAssertions.register('snapshot', () => { | ||
return 'snapshot'; | ||
}); | ||
|
||
testAssertions.register('deepStrictEqual', () => { | ||
return 'deepStrictEqual'; | ||
}); | ||
|
||
testAssertions.register('context', function() { | ||
return this; | ||
}); | ||
|
||
test('throws if name is not a string', () => { | ||
assert.throws(() => { | ||
testAssertions.register(5); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
message: 'The "name" argument must be of type string. Received type number (5)' | ||
}); | ||
}); | ||
|
||
test('throws if fn is not a function', () => { | ||
assert.throws(() => { | ||
testAssertions.register('foo', 5); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
message: 'The "fn" argument must be of type function. Received type number (5)' | ||
}); | ||
}); | ||
|
||
test('invokes a custom assertion as part of the test plan', (t) => { | ||
t.plan(2); | ||
t.assert.isOdd(5); | ||
assert.throws(() => { | ||
t.assert.isOdd(4); | ||
}, { | ||
code: 'ERR_ASSERTION', | ||
message: /Expected values to be strictly equal/ | ||
}); | ||
}); | ||
|
||
test('can override existing assertions', (t) => { | ||
assert.strictEqual(t.assert.ok(), 'ok'); | ||
assert.strictEqual(t.assert.snapshot(), 'snapshot'); | ||
assert.strictEqual(t.assert.deepStrictEqual(), 'deepStrictEqual'); | ||
}); | ||
|
||
test('"this" is set to the TestContext', (t) => { | ||
assert.strictEqual(t.assert.context(), t); | ||
}); |