Skip to content

Commit

Permalink
Add more node:util tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Mar 21, 2024
1 parent 9851ddb commit 58e1be2
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/workerd/api/node/util-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

import assert from 'node:assert';
import { mock } from 'node:test';
import util, { inspect } from 'node:util';

const remainingMustCallErrors = new Set();
Expand Down Expand Up @@ -3777,3 +3778,49 @@ export const utilInspectError = {
);
}
};

export const logTest = {
test() {
const original = console.log;
console.log = mock.fn();

util.log('test');

assert.strictEqual(console.log.mock.callCount(), 1);
const args = console.log.mock.calls[0].arguments;
assert.strictEqual(args.length, 3);
assert.strictEqual(args[0], '%s - %s');
// skipping the check on args[1] since it'll be a timestamp that changes
assert.strictEqual(args[2], 'test');

console.log = original;
}
};

export const aborted = {
async test() {
const signal = AbortSignal.timeout(10);
await util.aborted(signal, {});

await assert.rejects(util.aborted({}, {}), {
message: 'The "signal" argument must be an instance of AbortSignal. ' +
'Received an instance of Object'
});
}
};

export const debuglog = {
test() {
const original = console.log;
console.log = mock.fn();

util.debuglog('test')('hello');

assert.strictEqual(console.log.mock.callCount(), 1);
const args = console.log.mock.calls[0].arguments;
assert.strictEqual(args.length, 1);
assert.strictEqual(args[0], 'TEST: hello\n');

console.log = original;
}
};

0 comments on commit 58e1be2

Please sign in to comment.