diff options
Diffstat (limited to 'test/parallel/test-common.js')
-rw-r--r-- | test/parallel/test-common.js | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/test/parallel/test-common.js b/test/parallel/test-common.js index 2bda20acd9..0a4e1d72f0 100644 --- a/test/parallel/test-common.js +++ b/test/parallel/test-common.js @@ -22,7 +22,8 @@ 'use strict'; const common = require('../common'); const assert = require('assert'); - +const {join} = require('path'); +const {execFile} = require('child_process'); // test for leaked global detection global.gc = 42; // Not a valid global unless --expose_gc is set. @@ -33,12 +34,15 @@ delete global.gc; // common.mustCall() tests assert.throws(function() { common.mustCall(function() {}, 'foo'); -}, /^TypeError: Invalid expected value: foo$/); +}, /^TypeError: Invalid exact value: foo$/); assert.throws(function() { common.mustCall(function() {}, /foo/); -}, /^TypeError: Invalid expected value: \/foo\/$/); +}, /^TypeError: Invalid exact value: \/foo\/$/); +assert.throws(function() { + common.mustCallAtLeast(function() {}, /foo/); +}, /^TypeError: Invalid minimum value: \/foo\/$/); // assert.fail() tests assert.throws( @@ -47,3 +51,40 @@ assert.throws( code: 'ERR_ASSERTION', message: /^fhqwhgads$/ })); + +const fnOnce = common.mustCall(() => {}); +fnOnce(); +const fnTwice = common.mustCall(() => {}, 2); +fnTwice(); +fnTwice(); +const fnAtLeast1Called1 = common.mustCallAtLeast(() => {}, 1); +fnAtLeast1Called1(); +const fnAtLeast1Called2 = common.mustCallAtLeast(() => {}, 1); +fnAtLeast1Called2(); +fnAtLeast1Called2(); +const fnAtLeast2Called2 = common.mustCallAtLeast(() => {}, 2); +fnAtLeast2Called2(); +fnAtLeast2Called2(); +const fnAtLeast2Called3 = common.mustCallAtLeast(() => {}, 2); +fnAtLeast2Called3(); +fnAtLeast2Called3(); +fnAtLeast2Called3(); + +const failFixtures = [ + [ + join(common.fixturesDir, 'failmustcall1.js'), + 'Mismatched <anonymous> function calls. Expected exactly 2, actual 1.' + ], [ + join(common.fixturesDir, 'failmustcall2.js'), + 'Mismatched <anonymous> function calls. Expected at least 2, actual 1.' + ] +]; +for (const p of failFixtures) { + const [file, expected] = p; + execFile(process.argv[0], [file], common.mustCall((ex, stdout, stderr) => { + assert.ok(ex); + assert.strictEqual(stderr, ''); + const firstLine = stdout.split('\n').shift(); + assert.strictEqual(firstLine, expected); + })); +} |