Skip to content

Commit 17823b3

Browse files
GeoffreyBoothruyadorno
authored andcommitted
esm: refactor test-esm-named-exports
PR-URL: #49493 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 9f8bea3 commit 17823b3

5 files changed

Lines changed: 60 additions & 50 deletions

File tree

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
// Flags: --loader ./test/fixtures/es-module-loaders/hook-resolve-type.mjs
2-
import{allowGlobals}from'../common/index.mjs';
1+
import{spawnPromisified}from'../common/index.mjs';
2+
import*astmpdirfrom'../common/tmpdir.js';
33
import*asfixturesfrom'../common/fixtures.mjs';
4-
import{strictasassert}from'assert';
5-
import*asfsfrom'fs';
6-
7-
allowGlobals(global.getModuleTypeStats);
8-
9-
const{importedESM: importedESMBefore,
10-
importedCJS: importedCJSBefore}=awaitglobal.getModuleTypeStats();
11-
12-
constbasePath=
13-
newURL('./node_modules/',import.meta.url);
14-
15-
constrel=(file)=>newURL(file,basePath);
16-
constcreateDir=(path)=>{
17-
if(!fs.existsSync(path)){
18-
fs.mkdirSync(path);
19-
}
20-
};
4+
import{deepStrictEqual}from'node:assert';
5+
import{mkdir,rm,cp}from'node:fs/promises';
6+
import{execPath}from'node:process';
217

8+
constbase=tmpdir.fileURL(`test-esm-loader-resolve-type-${(Math.random()*Date.now()).toFixed(0)}`);
229
constmoduleName='module-counter-by-type';
23-
constmoduleDir=rel(`${moduleName}`);
10+
constmoduleURL=newURL(`${base}/node_modules/${moduleName}`);
2411
try{
25-
createDir(basePath);
26-
createDir(moduleDir);
27-
fs.cpSync(
28-
fixtures.path('es-modules',moduleName),
29-
moduleDir,
12+
awaitmkdir(moduleURL,{recursive: true});
13+
awaitcp(
14+
fixtures.path('es-modules','module-counter-by-type'),
15+
moduleURL,
3016
{recursive: true}
3117
);
3218

33-
34-
awaitimport(`${moduleName}`);
19+
deepStrictEqual(awaitspawnPromisified(
20+
execPath,
21+
[
22+
'--no-warnings',
23+
'--input-type=module',
24+
'--eval',
25+
`import { getModuleTypeStats } from ${JSON.stringify(fixtures.fileURL('es-module-loaders','hook-resolve-type.mjs'))};
26+
const before = getModuleTypeStats();
27+
await import(${JSON.stringify(moduleName)});
28+
const after = getModuleTypeStats();
29+
console.log(JSON.stringify({ before, after }));`,
30+
],
31+
{cwd: base},
32+
),{
33+
stderr: '',
34+
stdout: JSON.stringify({
35+
before: {importedESM: 0,importedCJS: 0},
36+
// Dynamic import in the eval script should increment ESM counter but not CJS counter
37+
after: {importedESM: 1,importedCJS: 0},
38+
})+'\n',
39+
code: 0,
40+
signal: null,
41+
});
3542
}finally{
36-
fs.rmSync(basePath,{recursive: true,force: true});
43+
awaitrm(base,{recursive: true,force: true});
3744
}
38-
39-
const{importedESM: importedESMAfter,
40-
importedCJS: importedCJSAfter}=awaitglobal.getModuleTypeStats();
41-
42-
// Dynamic import above should increment ESM counter but not CJS counter
43-
assert.strictEqual(importedESMBefore+1,importedESMAfter);
44-
assert.strictEqual(importedCJSBefore,importedCJSAfter);

‎test/es-module/test-esm-named-exports.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Flags: --experimental-loader ./test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs
1+
// Flags: --import ./test/fixtures/es-module-loaders/builtin-named-exports.mjs
22
'use strict';
33

44
require('../common');

‎test/es-module/test-esm-named-exports.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Flags: --experimental-loader ./test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs
1+
// Flags: --import ./test/fixtures/es-module-loaders/builtin-named-exports.mjs
22
import'../common/index.mjs';
33
import{readFile,__fromLoader}from'fs';
44
importassertfrom'assert';

‎test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
importmodulefrom'module';
1+
importmodulefrom'node:module';
22
import{readFileSync}from'node:fs';
33

4-
constGET_BUILTIN=`$__get_builtin_hole_${Date.now()}`;
5-
6-
exportfunctionglobalPreload(){
7-
return`Object.defineProperty(globalThis, ${JSON.stringify(GET_BUILTIN)}, {
8-
value: (builtinName) => {
9-
return getBuiltin(builtinName);
10-
},
11-
enumerable: false,
12-
configurable: false,
13-
});
14-
`;
4+
/** @type {string} */
5+
letGET_BUILTIN;
6+
exportfunctioninitialize(data){
7+
GET_BUILTIN=data.GET_BUILTIN;
158
}
169

1710
exportasyncfunctionresolve(specifier,context,next){
@@ -56,7 +49,7 @@ const $builtinInstance = ${GET_BUILTIN}(${JSON.stringify(builtinName)});
5649
module.exports = $builtinInstance;
5750
module.exports.__fromLoader = true;
5851
59-
// We need this for CJS-module-lexer can parse the exported names.
52+
// We need this for CJS-module-lexer can parse the exported names.
6053
${
6154
builtinExports
6255
.map(name=>`exports.${name} = $builtinInstance.${name};`)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import*asfixturesfrom'../../common/fixtures.mjs';
2+
import{createRequire,register}from'node:module';
3+
4+
constrequire=createRequire(import.meta.url);
5+
6+
constGET_BUILTIN=`$__get_builtin_hole_${Date.now()}`;
7+
Object.defineProperty(globalThis,GET_BUILTIN,{
8+
value: builtinName=>require(builtinName),
9+
enumerable: false,
10+
configurable: false,
11+
});
12+
13+
register(fixtures.fileURL('es-module-loaders/builtin-named-exports-loader.mjs'),{
14+
data: {
15+
GET_BUILTIN,
16+
},
17+
});

0 commit comments

Comments
 (0)