|
| 1 | +import path from 'path'; |
| 2 | +import { execaNode } from 'execa'; |
| 3 | +import { testSuite, expect } from 'manten'; |
| 4 | +import { createFixture } from 'fs-fixture'; |
| 5 | +import { tsxEsmPath, cjsApiPath, type NodeApis } from '../utils/tsx.js'; |
| 6 | + |
| 7 | +const tsFiles = { |
| 8 | + 'file.ts': ` |
| 9 | + import { foo } from './foo' |
| 10 | + export const message = foo as string |
| 11 | + `, |
| 12 | + 'foo.ts': ` |
| 13 | + import { bar } from './bar.js' |
| 14 | + export const foo = \`foo \${bar}\` as string |
| 15 | + `, |
| 16 | + 'bar.ts': 'export const bar = "bar" as string', |
| 17 | +}; |
| 18 | + |
| 19 | +export default testSuite(({ describe }, node: NodeApis) => { |
| 20 | + describe('API', ({ describe }) => { |
| 21 | + describe('CommonJS', ({ test }) => { |
| 22 | + test('register / unregister', async ({ onTestFinish }) => { |
| 23 | + const fixture = await createFixture({ |
| 24 | + 'register.cjs': ` |
| 25 | + const { register } = require(${JSON.stringify(cjsApiPath)}); |
| 26 | + try { |
| 27 | + require('./file'); |
| 28 | + } catch { |
| 29 | + console.log('Fails as expected'); |
| 30 | + } |
| 31 | +
|
| 32 | + const unregister = register(); |
| 33 | +
|
| 34 | + const loaded = require('./file'); |
| 35 | + console.log(loaded.message); |
| 36 | +
|
| 37 | + // Remove from cache |
| 38 | + const loadedPath = require.resolve('./file'); |
| 39 | + delete require.cache[loadedPath]; |
| 40 | +
|
| 41 | + unregister(); |
| 42 | +
|
| 43 | + try { |
| 44 | + require('./file'); |
| 45 | + } catch { |
| 46 | + console.log('Unregistered'); |
| 47 | + } |
| 48 | + `, |
| 49 | + ...tsFiles, |
| 50 | + }); |
| 51 | + onTestFinish(async () => await fixture.rm()); |
| 52 | + |
| 53 | + const { stdout } = await execaNode(path.join(fixture.path, 'register.cjs'), [], { |
| 54 | + nodePath: node.path, |
| 55 | + nodeOptions: [], |
| 56 | + }); |
| 57 | + |
| 58 | + expect(stdout).toBe('Fails as expected\nfoo bar\nUnregistered'); |
| 59 | + }); |
| 60 | + |
| 61 | + test('tsx.require()', async ({ onTestFinish }) => { |
| 62 | + const fixture = await createFixture({ |
| 63 | + 'require.cjs': ` |
| 64 | + const tsx = require(${JSON.stringify(cjsApiPath)}); |
| 65 | + try { |
| 66 | + require('./file'); |
| 67 | + } catch { |
| 68 | + console.log('Fails as expected'); |
| 69 | + } |
| 70 | +
|
| 71 | + const loaded = tsx.require('./file', __filename); |
| 72 | + console.log(loaded.message); |
| 73 | +
|
| 74 | + // Remove from cache |
| 75 | + const loadedPath = tsx.require.resolve('./file', __filename); |
| 76 | + delete require.cache[loadedPath]; |
| 77 | +
|
| 78 | + try { |
| 79 | + require('./file'); |
| 80 | + } catch { |
| 81 | + console.log('Unpolluted global require'); |
| 82 | + } |
| 83 | + `, |
| 84 | + ...tsFiles, |
| 85 | + }); |
| 86 | + onTestFinish(async () => await fixture.rm()); |
| 87 | + |
| 88 | + const { stdout } = await execaNode(path.join(fixture.path, 'require.cjs'), [], { |
| 89 | + nodePath: node.path, |
| 90 | + nodeOptions: [], |
| 91 | + }); |
| 92 | + |
| 93 | + expect(stdout).toBe('Fails as expected\nfoo bar\nUnpolluted global require'); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('Module', ({ test }) => { |
| 98 | + if (node.supports.moduleRegister) { |
| 99 | + test('module.register', async ({ onTestFinish }) => { |
| 100 | + const fixture = await createFixture({ |
| 101 | + 'package.json': JSON.stringify({ type: 'module' }), |
| 102 | + 'module-register.mjs': ` |
| 103 | + import { register } from 'node:module'; |
| 104 | +
|
| 105 | + await import('./file.ts').catch((error) => { |
| 106 | + console.log('Fails as expected'); |
| 107 | + }); |
| 108 | +
|
| 109 | + register(${JSON.stringify(tsxEsmPath)}, { |
| 110 | + parentURL: import.meta.url, |
| 111 | + data: true, |
| 112 | + }) |
| 113 | +
|
| 114 | + const { message } = await import('./file.ts?nocache') |
| 115 | + console.log(message) |
| 116 | + `, |
| 117 | + ...tsFiles, |
| 118 | + }); |
| 119 | + onTestFinish(async () => await fixture.rm()); |
| 120 | + |
| 121 | + const { stdout } = await execaNode(path.join(fixture.path, 'module-register.mjs'), [], { |
| 122 | + nodePath: node.path, |
| 123 | + nodeOptions: [], |
| 124 | + }); |
| 125 | + |
| 126 | + expect(stdout).toBe('Fails as expected\nfoo bar'); |
| 127 | + }); |
| 128 | + } |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments