File
Blob: src/workerd/api/tests/new-module-registry-test.wd-test
| 1 | using Workerd = import "/workerd/workerd.capnp"; |
| 2 | |
| 3 | const unitTests :Workerd.Config = ( |
| 4 | services = [ |
| 5 | ( name = "new-module-registry-test", |
| 6 | worker = ( |
| 7 | modules = [ |
| 8 | (name = "worker", esModule = embed "new-module-registry-test.js"), |
| 9 | (name = "foo", esModule = "export const foo = 1; export default 2; export const bar = import.meta.url"), |
| 10 | (name = "bar", esModule = "export const foo = 1; export default 2;"), |
| 11 | (name = "node:fs", esModule = "export default 'abc'"), |
| 12 | |
| 13 | # Intentionally bad module to test error handling. |
| 14 | # Evaluation will error because i/o is not permitted at top-level scope. |
| 15 | (name = "bad", esModule = "export default 1; setTimeout(() => {}, 10)"), |
| 16 | |
| 17 | # Ensure that async context is propagated into a dynamic import. |
| 18 | (name = "als", esModule = "export default globalThis.als.getStore()"), |
| 19 | |
| 20 | # Import attributes are not supported currently |
| 21 | (name = "ia", esModule = "import * as def from 'foo' with { a: 'test' }"), |
| 22 | |
| 23 | # Errors on ESM eval should be reported properly in both static and |
| 24 | # dynamic imports. |
| 25 | (name = "esm-error", esModule = "export default 1; throw new Error('boom');"), |
| 26 | (name = "esm-error-dynamic", esModule = "export * as d from 'esm-error'"), |
| 27 | |
| 28 | # CommonJS modules work |
| 29 | (name = "cjs1", |
| 30 | namedExports = ["foo", "bar"], |
| 31 | commonJsModule = "module.exports = { foo: 1, bar: 2, filename: __filename, dirname: __dirname, assert: require('assert') }"), |
| 32 | (name = "cjs2", commonJsModule = "module.exports = require('cjs1')"), |
| 33 | (name = "cjs3", commonJsModule = "throw new Error('boom')"), |
| 34 | # Intentional circular dependency |
| 35 | (name = "cjs4", commonJsModule = "module.exports = require('cjs5')"), |
| 36 | (name = "cjs5", commonJsModule = "module.exports = require('cjs4')"), |
| 37 | (name = "cjs6", commonJsModule = "module.exports = require('tla')"), |
| 38 | |
| 39 | # Other module types work |
| 40 | (name = "text", text = "abc"), |
| 41 | (name = "data", data = "abcdef"), |
| 42 | (name = "json", json = "{ \"foo\": 1 }"), |
| 43 | (name = "invalid-json", json = "1n"), |
| 44 | |
| 45 | (name = "wasm", wasm = embed "test.wasm"), |
| 46 | |
| 47 | (name = "complex", esModule = "import * as abc from '././././././complex/complex2'; export { abc };"), |
| 48 | (name = "complex/complex2", esModule = "import * as def from '/bundle/complex/complex3'; export { def }; export const foo = 1;"), |
| 49 | (name = "complex/complex3", esModule = "export const bar = 2;"), |
| 50 | |
| 51 | # When the user bundle name begins with a slash, it is stripped before |
| 52 | # processing, ensuring that the module is still processed relative to |
| 53 | # the bundle base URL. Query strings and fragments are ignored. |
| 54 | (name = "/%61bc?abc#123", esModule = "export default import.meta.url"), |
| 55 | |
| 56 | # A module name should not be able to escape the bundle base URL, |
| 57 | # and leading/trailing whitespace should be trimmed. |
| 58 | (name = " ..//////outside ", esModule = "export default 1;"), |
| 59 | |
| 60 | # Should resolve to "abc123" after normalization. |
| 61 | (name = "/foo/../bar/../../../../////abc123", esModule = "export default 1;"), |
| 62 | |
| 63 | # It should be possible to have a module whose name is a valid URL. |
| 64 | (name = "https://example.com/mod", esModule = "export default 'example';"), |
| 65 | |
| 66 | # Percent-encoded characters in the path should be normalized, and absolute |
| 67 | # file URLs are accepted as long as they are under the bundle base URL. |
| 68 | (name = "file:///bundl%65/should/b%65/ok", esModule = "export default 1;"), |
| 69 | |
| 70 | # Unicode characters in the path should be handled as UTF-8 and supported. |
| 71 | (name = "部品", esModule = "export default 1;"), |
| 72 | |
| 73 | (name = "tla", esModule = "export default await import('text')"), |
| 74 | ], |
| 75 | compatibilityFlags = [ |
| 76 | "nodejs_compat_v2", |
| 77 | "new_module_registry", |
| 78 | "experimental", |
| 79 | ], |
| 80 | bindings = [ |
| 81 | (name = "unsafe", unsafeEval = void), |
| 82 | ], |
| 83 | ), |
| 84 | ), |
| 85 | ], |
| 86 | ); |