File
Blob: src/workerd/api/node/tests/module-create-require-test.js
| 1 | // Copyright (c) 2017-2022 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 { createRequire, isBuiltin, builtinModules } from 'node:module'; |
| 5 | import { ok, strictEqual, throws } from 'node:assert'; |
| 6 | |
| 7 | export const doTheTest = { |
| 8 | async test() { |
| 9 | const require = createRequire('/'); |
| 10 | ok(typeof require === 'function'); |
| 11 | |
| 12 | const foo = require('foo'); |
| 13 | const bar = require('bar'); |
| 14 | const baz = require('baz'); |
| 15 | const qux = require('worker/qux'); |
| 16 | |
| 17 | // When require_returns_default_export flag is enabled, require() returns the |
| 18 | // default export directly. Otherwise it returns the namespace object. |
| 19 | if (Cloudflare.compatibilityFlags.require_returns_default_export) { |
| 20 | strictEqual(foo, 1); |
| 21 | } else { |
| 22 | strictEqual(foo.default, 1); |
| 23 | } |
| 24 | strictEqual(bar, 2); |
| 25 | strictEqual(baz, 3); |
| 26 | strictEqual(qux, '4'); |
| 27 | |
| 28 | const assert = await import('node:assert'); |
| 29 | const required = require('node:assert'); |
| 30 | |
| 31 | // When require_returns_default_export flag is enabled, require() returns the |
| 32 | // default export directly (assert.default === required). |
| 33 | // When the flag is disabled, require() returns the namespace (assert === required). |
| 34 | if (Cloudflare.compatibilityFlags.require_returns_default_export) { |
| 35 | strictEqual(assert.default, required); |
| 36 | } else { |
| 37 | strictEqual(assert, required); |
| 38 | } |
| 39 | |
| 40 | throws(() => require('invalid'), { |
| 41 | message: 'Top-level await in module is not permitted at this time.', |
| 42 | }); |
| 43 | // Trying to require the module again should throw the same error. |
| 44 | throws(() => require('invalid'), { |
| 45 | message: 'Top-level await in module is not permitted at this time.', |
| 46 | }); |
| 47 | throws(() => require('invalid2'), { |
| 48 | message: 'Top-level await in module is not permitted at this time.', |
| 49 | }); |
| 50 | |
| 51 | throws(() => require('does not exist')); |
| 52 | throws(() => createRequire('not a valid path'), { |
| 53 | message: /The argument must be a file URL object/, |
| 54 | }); |
| 55 | throws(() => createRequire(new URL('http://example.org')), { |
| 56 | message: /The argument must be a file URL object/, |
| 57 | }); |
| 58 | |
| 59 | // TODO(soon): Later when we when complete the new module registry, query strings |
| 60 | // and hash fragments will be allowed when the new registry is being used. |
| 61 | throws(() => createRequire('file://test?abc'), { |
| 62 | message: |
| 63 | 'The specifier must not have query string parameters or hash fragments.', |
| 64 | }); |
| 65 | throws(() => createRequire('file://test#123'), { |
| 66 | message: |
| 67 | 'The specifier must not have query string parameters or hash fragments.', |
| 68 | }); |
| 69 | |
| 70 | // These should not throw... |
| 71 | createRequire('file:///'); |
| 72 | createRequire('file:///tmp'); |
| 73 | createRequire(new URL('file:///')); |
| 74 | }, |
| 75 | }; |
| 76 | |
| 77 | export const isBuiltinTest = { |
| 78 | test() { |
| 79 | ok(isBuiltin('fs')); |
| 80 | ok(isBuiltin('http')); |
| 81 | ok(isBuiltin('https')); |
| 82 | ok(isBuiltin('path')); |
| 83 | ok(isBuiltin('node:fs')); |
| 84 | ok(isBuiltin('node:http')); |
| 85 | ok(isBuiltin('node:https')); |
| 86 | ok(isBuiltin('node:path')); |
| 87 | ok(isBuiltin('node:test')); |
| 88 | ok(!isBuiltin('test')); |
| 89 | ok(!isBuiltin('worker')); |
| 90 | ok(!isBuiltin('worker/qux')); |
| 91 | |
| 92 | builtinModules.forEach((module) => { |
| 93 | ok(isBuiltin(module)); |
| 94 | }); |
| 95 | }, |
| 96 | }; |