File
Blob: src/workerd/api/tests/disable-importable-exports-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 { exports, withExports } from 'cloudflare:workers'; |
| 5 | import { strictEqual } from 'node:assert'; |
| 6 | |
| 7 | // This test runs with the disallow-importable-env flag set, which should also |
| 8 | // disable importable exports. The exports imported from cloudflare:workers |
| 9 | // should not be populated. Using withExports, however, would still work. |
| 10 | |
| 11 | export class MyService { |
| 12 | greet(name) { |
| 13 | return `Hello, ${name}!`; |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | export const test = { |
| 18 | async test() { |
| 19 | // exports should be empty/undefined when disallow_importable_env is set |
| 20 | strictEqual(exports.MyService, undefined); |
| 21 | strictEqual(exports.test, undefined); |
| 22 | |
| 23 | // withExports still works as expected with an object |
| 24 | const result = await withExports({ customExport: 'test' }, async () => { |
| 25 | await scheduler.wait(0); |
| 26 | const { exports: otherExports } = await import('child'); |
| 27 | strictEqual(otherExports.customExport, 'test'); |
| 28 | strictEqual(otherExports.MyService, undefined); |
| 29 | return otherExports.customExport; |
| 30 | }); |
| 31 | strictEqual(result, 'test'); |
| 32 | |
| 33 | // withExports with non-object values should fall back to disabled behavior |
| 34 | const nonObjectValues = [null, 'string', 123, undefined]; |
| 35 | for (const value of nonObjectValues) { |
| 36 | await withExports(value, async () => { |
| 37 | await scheduler.wait(0); |
| 38 | // When withExports gets a non-object, getCurrentExports falls through to the |
| 39 | // base behavior (which is disabled by disallow_importable_env), so exports are empty |
| 40 | strictEqual(exports.MyService, undefined); |
| 41 | strictEqual(exports.customExport, undefined); |
| 42 | }); |
| 43 | } |
| 44 | }, |
| 45 | }; |