File
Blob: src/workerd/api/node/tests/repl-nodejs-test.js
| 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 |
| 4 | import repl from 'node:repl'; |
| 5 | import assert from 'node:assert'; |
| 6 | import { EventEmitter } from 'node:events'; |
| 7 | |
| 8 | export const replWriter = { |
| 9 | test() { |
| 10 | assert.throws(() => repl.writer(), { |
| 11 | code: 'ERR_METHOD_NOT_IMPLEMENTED', |
| 12 | message: /writer/, |
| 13 | }); |
| 14 | assert.strictEqual(typeof repl.writer, 'function'); |
| 15 | }, |
| 16 | }; |
| 17 | |
| 18 | export const replStart = { |
| 19 | test() { |
| 20 | assert.throws(() => repl.start(), { |
| 21 | code: 'ERR_METHOD_NOT_IMPLEMENTED', |
| 22 | message: /start/, |
| 23 | }); |
| 24 | assert.strictEqual(typeof repl.start, 'function'); |
| 25 | }, |
| 26 | }; |
| 27 | |
| 28 | export const replRecoverable = { |
| 29 | test() { |
| 30 | const error = new Error('test error'); |
| 31 | const recoverable = new repl.Recoverable(error); |
| 32 | |
| 33 | assert.strictEqual(recoverable instanceof SyntaxError, true); |
| 34 | assert.strictEqual(recoverable instanceof repl.Recoverable, true); |
| 35 | assert.strictEqual(recoverable.err, error); |
| 36 | assert.strictEqual(recoverable.err.message, 'test error'); |
| 37 | |
| 38 | const anotherError = new TypeError('type error'); |
| 39 | const anotherRecoverable = new repl.Recoverable(anotherError); |
| 40 | assert.strictEqual(anotherRecoverable.err, anotherError); |
| 41 | }, |
| 42 | }; |
| 43 | |
| 44 | export const replREPLServer = { |
| 45 | test() { |
| 46 | assert.throws(() => new repl.REPLServer(), { |
| 47 | code: 'ERR_METHOD_NOT_IMPLEMENTED', |
| 48 | message: /REPLServer/, |
| 49 | }); |
| 50 | assert.strictEqual(typeof repl.REPLServer, 'function'); |
| 51 | }, |
| 52 | }; |
| 53 | |
| 54 | export const replREPLServerMethods = { |
| 55 | test() { |
| 56 | const REPLServerProto = repl.REPLServer.prototype; |
| 57 | |
| 58 | assert.strictEqual(typeof REPLServerProto.setupHistory, 'function'); |
| 59 | assert.strictEqual(typeof REPLServerProto.defineCommand, 'function'); |
| 60 | assert.strictEqual(typeof REPLServerProto.displayPrompt, 'function'); |
| 61 | assert.strictEqual(typeof REPLServerProto.clearBufferedCommand, 'function'); |
| 62 | |
| 63 | assert.strictEqual(typeof REPLServerProto.on, 'function'); |
| 64 | assert.strictEqual(typeof REPLServerProto.emit, 'function'); |
| 65 | assert.strictEqual(typeof REPLServerProto.once, 'function'); |
| 66 | assert.strictEqual(typeof REPLServerProto.removeListener, 'function'); |
| 67 | }, |
| 68 | }; |
| 69 | |
| 70 | export const replREPLServerInheritance = { |
| 71 | test() { |
| 72 | assert.strictEqual(repl.REPLServer.prototype instanceof EventEmitter, true); |
| 73 | }, |
| 74 | }; |
| 75 | |
| 76 | export const replModes = { |
| 77 | test() { |
| 78 | assert.strictEqual(typeof repl.REPL_MODE_SLOPPY, 'symbol'); |
| 79 | assert.strictEqual(typeof repl.REPL_MODE_STRICT, 'symbol'); |
| 80 | assert.notStrictEqual(repl.REPL_MODE_SLOPPY, repl.REPL_MODE_STRICT); |
| 81 | |
| 82 | assert.strictEqual(repl.REPL_MODE_SLOPPY.toString(), 'Symbol(repl-sloppy)'); |
| 83 | assert.strictEqual(repl.REPL_MODE_STRICT.toString(), 'Symbol(repl-strict)'); |
| 84 | }, |
| 85 | }; |
| 86 | |
| 87 | export const replBuiltinModules = { |
| 88 | test() { |
| 89 | assert.strictEqual(Array.isArray(repl.builtinModules), true); |
| 90 | assert.strictEqual(repl.builtinModules.length > 0, true); |
| 91 | |
| 92 | assert.strictEqual(repl.builtinModules.includes('fs'), true); |
| 93 | assert.strictEqual(repl.builtinModules.includes('path'), true); |
| 94 | assert.strictEqual(repl.builtinModules.includes('http'), true); |
| 95 | |
| 96 | for (const module of repl.builtinModules) { |
| 97 | assert.strictEqual(typeof module, 'string'); |
| 98 | assert.strictEqual(module[0] !== '_', true); |
| 99 | } |
| 100 | }, |
| 101 | }; |
| 102 | |
| 103 | export const replBuiltinLibs = { |
| 104 | test() { |
| 105 | assert.strictEqual(Array.isArray(repl._builtinLibs), true); |
| 106 | assert.deepStrictEqual(repl._builtinLibs, repl.builtinModules); |
| 107 | assert.strictEqual(repl._builtinLibs, repl.builtinModules); |
| 108 | }, |
| 109 | }; |
| 110 | |
| 111 | export const replDefaultExport = { |
| 112 | test() { |
| 113 | assert.strictEqual(typeof repl, 'object'); |
| 114 | assert.strictEqual(typeof repl.writer, 'function'); |
| 115 | assert.strictEqual(typeof repl.start, 'function'); |
| 116 | assert.strictEqual(typeof repl.Recoverable, 'function'); |
| 117 | assert.strictEqual(typeof repl.REPLServer, 'function'); |
| 118 | assert.strictEqual(Array.isArray(repl.builtinModules), true); |
| 119 | assert.strictEqual(Array.isArray(repl._builtinLibs), true); |
| 120 | assert.strictEqual(typeof repl.REPL_MODE_SLOPPY, 'symbol'); |
| 121 | assert.strictEqual(typeof repl.REPL_MODE_STRICT, 'symbol'); |
| 122 | }, |
| 123 | }; |
| 124 | |
| 125 | export const replModuleExports = { |
| 126 | async test() { |
| 127 | const replModule = await import('node:repl'); |
| 128 | |
| 129 | assert.strictEqual(typeof replModule.default, 'object'); |
| 130 | assert.strictEqual(typeof replModule.writer, 'function'); |
| 131 | assert.strictEqual(typeof replModule.start, 'function'); |
| 132 | assert.strictEqual(typeof replModule.Recoverable, 'function'); |
| 133 | assert.strictEqual(typeof replModule.REPLServer, 'function'); |
| 134 | assert.strictEqual(Array.isArray(replModule.builtinModules), true); |
| 135 | assert.strictEqual(Array.isArray(replModule._builtinLibs), true); |
| 136 | assert.strictEqual(typeof replModule.REPL_MODE_SLOPPY, 'symbol'); |
| 137 | assert.strictEqual(typeof replModule.REPL_MODE_STRICT, 'symbol'); |
| 138 | |
| 139 | assert.strictEqual(replModule.default.writer, replModule.writer); |
| 140 | assert.strictEqual(replModule.default.start, replModule.start); |
| 141 | assert.strictEqual(replModule.default.Recoverable, replModule.Recoverable); |
| 142 | assert.strictEqual(replModule.default.REPLServer, replModule.REPLServer); |
| 143 | assert.strictEqual( |
| 144 | replModule.default.builtinModules, |
| 145 | replModule.builtinModules |
| 146 | ); |
| 147 | assert.strictEqual( |
| 148 | replModule.default._builtinLibs, |
| 149 | replModule._builtinLibs |
| 150 | ); |
| 151 | assert.strictEqual( |
| 152 | replModule.default.REPL_MODE_SLOPPY, |
| 153 | replModule.REPL_MODE_SLOPPY |
| 154 | ); |
| 155 | assert.strictEqual( |
| 156 | replModule.default.REPL_MODE_STRICT, |
| 157 | replModule.REPL_MODE_STRICT |
| 158 | ); |
| 159 | }, |
| 160 | }; |
| 161 | |
| 162 | export const replRecoverableError = { |
| 163 | test() { |
| 164 | const errors = [ |
| 165 | new Error('standard error'), |
| 166 | new TypeError('type error'), |
| 167 | |
| 168 | new RangeError('range error'), |
| 169 | new SyntaxError('syntax error'), |
| 170 | ]; |
| 171 | |
| 172 | for (const error of errors) { |
| 173 | const recoverable = new repl.Recoverable(error); |
| 174 | assert.strictEqual(recoverable.err, error); |
| 175 | assert.strictEqual(recoverable instanceof SyntaxError, true); |
| 176 | assert.strictEqual(recoverable instanceof Error, true); |
| 177 | } |
| 178 | }, |
| 179 | }; |
| 180 | |
| 181 | export const replRecoverableConstructor = { |
| 182 | test() { |
| 183 | assert.strictEqual(typeof repl.Recoverable, 'function'); |
| 184 | assert.strictEqual(repl.Recoverable.name, 'Recoverable'); |
| 185 | |
| 186 | const recoverable = new repl.Recoverable(new Error()); |
| 187 | assert.strictEqual(recoverable.constructor, repl.Recoverable); |
| 188 | assert.strictEqual(recoverable.constructor.name, 'Recoverable'); |
| 189 | }, |
| 190 | }; |
| 191 | |
| 192 | export const replBuiltinModulesFiltered = { |
| 193 | test() { |
| 194 | for (const module of repl.builtinModules) { |
| 195 | assert.strictEqual(module.startsWith('_'), false); |
| 196 | } |
| 197 | |
| 198 | for (const module of repl._builtinLibs) { |
| 199 | assert.strictEqual(module.startsWith('_'), false); |
| 200 | } |
| 201 | }, |
| 202 | }; |