|
1 | 1 | import fs from 'node:fs'; |
| 2 | +import { createRequire } from 'node:module'; |
2 | 3 | import path, { join } from 'node:path'; |
3 | 4 | import { pathToFileURL } from 'node:url'; |
4 | 5 | import vm from 'node:vm'; |
5 | 6 | import { describe, expect, test } from '@rstest/core'; |
6 | | -import { buildAndGetResults } from 'test-helper'; |
| 7 | +import { buildAndGetResults, queryContent } from 'test-helper'; |
7 | 8 |
|
8 | 9 | describe('ESM shims', async () => { |
9 | 10 | const fixturePath = join(__dirname, 'esm'); |
@@ -107,42 +108,52 @@ describe('ESM shims disabled', async () => { |
107 | 108 | describe('CJS shims', () => { |
108 | 109 | test('import.meta.url', async () => { |
109 | 110 | const fixturePath = join(__dirname, 'cjs'); |
110 | | - const { entryFiles, entries } = await buildAndGetResults({ fixturePath }); |
| 111 | + const { entryFiles, entries, contents } = await buildAndGetResults({ |
| 112 | + fixturePath, |
| 113 | + }); |
111 | 114 | // `module.require` is not available in Rstest runner context. Manually create a context to run the CJS code. |
112 | 115 | // As a temporary solution, we use `module.require` to avoid potential collision with module scope variable `require`. |
113 | 116 | const cjsCode = entries.cjs; |
| 117 | + const req = createRequire(entryFiles.cjs); |
114 | 118 | const context = vm.createContext({ |
115 | | - require, |
| 119 | + require: req, |
116 | 120 | exports, |
117 | | - module: { require }, |
| 121 | + module: { require: req }, |
118 | 122 | __filename: entryFiles.cjs, |
119 | 123 | }); |
120 | | - const { importMetaUrl, requiredModule } = vm.runInContext(cjsCode, context); |
| 124 | + const { importMetaUrl, dynamicImportMetaUrl, requiredModule } = |
| 125 | + vm.runInContext(cjsCode, context); |
121 | 126 | const fileUrl = pathToFileURL(entryFiles.cjs).href; |
| 127 | + const dynamicUrl = await dynamicImportMetaUrl(); |
| 128 | + const { content: dynamicContent } = queryContent( |
| 129 | + contents.cjs!, |
| 130 | + /cjs\/1~7\.cjs/, |
| 131 | + ); |
| 132 | + |
122 | 133 | expect(importMetaUrl).toBe(fileUrl); |
| 134 | + expect(dynamicUrl).toBe(fileUrl.replace('index.cjs', '1~7.cjs')); |
123 | 135 | expect(requiredModule).toBe('ok'); |
124 | | - expect( |
125 | | - cjsCode.startsWith( |
126 | | - `"use strict";\nconst __rslib_import_meta_url__ = /*#__PURE__*/ function() {`, |
127 | | - ), |
128 | | - ).toBe(true); |
| 136 | + |
| 137 | + for (const code of [cjsCode, dynamicContent]) { |
| 138 | + expect( |
| 139 | + code.startsWith( |
| 140 | + `"use strict";\nconst __rslib_import_meta_url__ = /*#__PURE__*/ function() {`, |
| 141 | + ), |
| 142 | + ).toBe(true); |
| 143 | + } |
129 | 144 | }); |
130 | 145 |
|
131 | 146 | test('ESM should not be affected by CJS shims configuration', async () => { |
132 | 147 | const fixturePath = join(__dirname, 'cjs'); |
133 | 148 | const { entries } = await buildAndGetResults({ fixturePath }); |
134 | | - expect(entries.esm).toMatchInlineSnapshot(` |
135 | | - "import { createRequire } from "node:module"; |
136 | | - import { fileURLToPath } from "url"; |
137 | | - const importMetaUrl = import.meta.url; |
138 | | - const src_require = createRequire(import.meta.url); |
139 | | - const requiredModule = src_require('./ok.cjs'); |
140 | | - const src_filename = fileURLToPath(import.meta.url); |
141 | | - console.log(src_filename); |
142 | | - const src_module = null; |
143 | | - export { src_filename as __filename, importMetaUrl, src_module as module, requiredModule }; |
144 | | - " |
145 | | - `); |
| 149 | + |
| 150 | + for (const code of [ |
| 151 | + 'const importMetaUrl = import.meta.url;', |
| 152 | + 'const src_require = createRequire(import.meta.url);', |
| 153 | + 'const src_filename = fileURLToPath(import.meta.url);', |
| 154 | + ]) { |
| 155 | + expect(entries.esm).toContain(code); |
| 156 | + } |
146 | 157 | }); |
147 | 158 | }); |
148 | 159 |
|
|
0 commit comments