Skip to content
File

Blob: src/workerd/api/node/tests/console-nodejs-test.js

javascript71 lines
1// Copyright (c) 2025 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4import Console from 'node:console';
5import { strictEqual, doesNotThrow, throws } from 'node:assert';
6 
7// These methods are retrieved using Object.keys(require('node:console'))
8// on Node.js v22.17.1
9const methods = [
10 'log',
11 'info',
12 'debug',
13 'warn',
14 'error',
15 'dir',
16 'time',
17 'timeEnd',
18 'timeLog',
19 'trace',
20 'assert',
21 'clear',
22 'count',
23 'countReset',
24 'group',
25 'groupEnd',
26 'table',
27 'dirxml',
28 'groupCollapsed',
29 'Console',
30 'profile',
31 'profileEnd',
32 'timeStamp',
33 'context',
34 'createTask',
35];
36 
37const notImplementedMethods = ['context', 'createTask', 'Console'];
38 
39export const testMethodNames = {
40 async test() {
41 for (const method of methods) {
42 strictEqual(
43 typeof Console[method],
44 'function',
45 `Method Console.${method} is not a function`
46 );
47 }
48 },
49};
50 
51export const testNotImplemented = {
52 async test() {
53 for (const method of notImplementedMethods) {
54 throws(() => Console[method](), {
55 code: 'ERR_METHOD_NOT_IMPLEMENTED',
56 });
57 }
58 },
59};
60 
61export const testMethods = {
62 async test() {
63 const implementedMethods = methods.filter(
64 (m) => !notImplementedMethods.includes(m)
65 );
66 for (const method of implementedMethods) {
67 doesNotThrow(() => Console[method](`Calling Console.${method}()`));
68 }
69 },
70};