From 29a9719fa637f84f50273f29822033a0a6f96df7 Mon Sep 17 00:00:00 2001 From: Kevin Deng Date: Sun, 30 Nov 2025 07:54:45 +0800 Subject: [PATCH] test: add more import ways --- README.md | 5 ++++ tests/fixtures/cjs-dynamic-import.cjs | 1 + tests/fixtures/cjs-import.cjs | 1 + tests/fixtures/mod.js | 15 +++++++++- tests/index.test.js | 42 +++++++++++++++++++++------ 5 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 tests/fixtures/cjs-dynamic-import.cjs create mode 100644 tests/fixtures/cjs-import.cjs diff --git a/README.md b/README.md index 876d0a6..74db733 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,11 @@ Import ES modules without cache. +## Features + +- Import ES modules without cache +- All dependencies are also imported without cache + ## Install ```bash diff --git a/tests/fixtures/cjs-dynamic-import.cjs b/tests/fixtures/cjs-dynamic-import.cjs new file mode 100644 index 0000000..9127885 --- /dev/null +++ b/tests/fixtures/cjs-dynamic-import.cjs @@ -0,0 +1 @@ +module.exports = crypto.randomUUID() diff --git a/tests/fixtures/cjs-import.cjs b/tests/fixtures/cjs-import.cjs new file mode 100644 index 0000000..9127885 --- /dev/null +++ b/tests/fixtures/cjs-import.cjs @@ -0,0 +1 @@ +module.exports = crypto.randomUUID() diff --git a/tests/fixtures/mod.js b/tests/fixtures/mod.js index 8141d32..1342c0f 100644 --- a/tests/fixtures/mod.js +++ b/tests/fixtures/mod.js @@ -1,8 +1,21 @@ import { createRequire } from 'module' + const require = createRequire(import.meta.url) +export const url = import.meta.url +// import + ESM export * from './rand.js' + +// require + CJS export const cjs = require('./cjs.cjs') + +// require + ESM export const requireESM = require('./require-esm.js') -export const url = import.meta.url +// import + CJS +export { default as importCJS } from './cjs-import.cjs' +// dynamic import + CJS +export const dynamicImportCJS = (await import('./cjs-dynamic-import.cjs')) + .default + +// circular export * from './circular.js' diff --git a/tests/index.test.js b/tests/index.test.js index 9ca0178..f26ad24 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -15,31 +15,55 @@ function clearCJSCache() { test('import attributes', async () => { const deregister = init() - const { uuid, url, cjs, requireESM } = await import('./fixtures/mod.js', { - with: { cache: 'no' }, - }) - assert.match(url, /\?no-cache=[0-9a-f-]{36}$/) - + const { uuid, url, cjs, requireESM, importCJS, dynamicImportCJS } = + await import('./fixtures/mod.js', { + with: { cache: 'no' }, + }) const { uuid: uuid2, cjs: cjs2, requireESM: requireESM2, + importCJS: importCJS2, + dynamicImportCJS: dynamicImportCJS2, } = await import('./fixtures/mod.js', { with: { cache: 'no' }, }) - assert.notEqual(uuid, uuid2) - assert.equal(cjs, cjs2) - clearCJSCache() - const { uuid: uuid3, requireESM: requireESM3 } = await import( + const { + uuid: uuid3, + cjs: cjs3, + requireESM: requireESM3, + importCJS: importCJS3, + dynamicImportCJS: dynamicImportCJS3, + } = await import( // absolute URL new URL('fixtures/mod.js', import.meta.url).href, { with: { cache: 'no' }, } ) + + assert.match(url, /\?no-cache=[0-9a-f-]{36}$/) + + // import + ESM + assert.notEqual(uuid, uuid2) assert.notEqual(uuid2, uuid3) + // require + CJS + assert.equal(cjs, cjs2) + // require + CJS + clearCJSCache + assert.notEqual(cjs2, cjs3) + + // import + CJS + assert.equal(importCJS, importCJS2) + // import + CJS + clearCJSCache + assert.notEqual(importCJS2, importCJS3) + + // dynamic import + CJS + assert.equal(dynamicImportCJS, dynamicImportCJS2) + // dynamic import + CJS + clearCJSCache + assert.notEqual(dynamicImportCJS2, dynamicImportCJS3) + // known limitation: require cache can't be fully cleared assert.equal(requireESM, requireESM2) assert.equal(requireESM2, requireESM3) -- 2.51.2