-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore refactored hook-resolve-type test
- Loading branch information
1 parent
2ad5a35
commit b0a6048
Showing
4 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Flags: --import ./test/fixtures/es-module-loaders/hook-resolve-type.mjs | ||
import '../common/index.mjs'; | ||
import * as fixtures from '../common/fixtures.mjs'; | ||
import { getModuleTypeStats } from '../fixtures/es-module-loaders/hook-resolve-type.mjs'; | ||
import { strict as assert } from 'assert'; | ||
import * as fs from 'fs'; | ||
|
||
const { importedESM: importedESMBefore, | ||
importedCJS: importedCJSBefore } = getModuleTypeStats(); | ||
|
||
const basePath = | ||
new URL('./node_modules/', import.meta.url); | ||
|
||
const rel = (file) => new URL(file, basePath); | ||
const createDir = (path) => { | ||
if (!fs.existsSync(path)) { | ||
fs.mkdirSync(path); | ||
} | ||
}; | ||
|
||
const moduleName = 'module-counter-by-type'; | ||
const moduleDir = rel(`${moduleName}`); | ||
|
||
try { | ||
createDir(basePath); | ||
createDir(moduleDir); | ||
fs.cpSync( | ||
fixtures.path('es-modules', moduleName), | ||
moduleDir, | ||
{ recursive: true } | ||
); | ||
|
||
await import(`${moduleName}`); | ||
} finally { | ||
fs.rmSync(basePath, { recursive: true, force: true }); | ||
} | ||
|
||
const { importedESM: importedESMAfter, | ||
importedCJS: importedCJSAfter } = getModuleTypeStats(); | ||
|
||
// Dynamic import above should increment ESM counter but not CJS counter | ||
assert.strictEqual(importedESMBefore + 1, importedESMAfter); | ||
assert.strictEqual(importedCJSBefore, importedCJSAfter); |
18 changes: 18 additions & 0 deletions
18
test/fixtures/es-module-loaders/hook-resolve-type-loader.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** @type {MessagePort} */ | ||
let port; | ||
export function initialize(data) { | ||
port = data.port; | ||
} | ||
|
||
export async function resolve(specifier, context, next) { | ||
const nextResult = await next(specifier, context); | ||
const { format } = nextResult; | ||
|
||
if (format === 'module' || specifier.endsWith('.mjs')) { | ||
port.postMessage({ type: 'module' }); | ||
} else if (format == null || format === 'commonjs') { | ||
port.postMessage({ type: 'commonjs' }); | ||
} | ||
|
||
return nextResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as fixtures from '../../common/fixtures.mjs'; | ||
import { register } from 'node:module'; | ||
import { MessageChannel } from 'node:worker_threads'; | ||
|
||
let importedESM = 0; | ||
let importedCJS = 0; | ||
export function getModuleTypeStats() { | ||
return { importedESM, importedCJS }; | ||
}; | ||
|
||
const { port1, port2 } = new MessageChannel(); | ||
|
||
register(fixtures.fileURL('es-module-loaders/hook-resolve-type-loader.mjs'), { | ||
data: { port: port2 }, | ||
transferList: [port2], | ||
}); | ||
|
||
port1.on('message', ({ type }) => { | ||
switch (type) { | ||
case 'module': | ||
importedESM++; | ||
break; | ||
case 'commonjs': | ||
importedCJS++; | ||
break; | ||
} | ||
}); | ||
|
||
port1.unref(); | ||
port2.unref(); |