From 749cd728df4e1bf3fed106da9b6d6990e4bb41e7 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 21 Dec 2023 22:32:13 -0800 Subject: [PATCH 1/7] add node:fs/promises test --- tests/local/usesInlineImport.mjs | 6 +++++- tests/tests-node/esmock.node.test.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/local/usesInlineImport.mjs b/tests/local/usesInlineImport.mjs index 9d99145b..268b76be 100644 --- a/tests/local/usesInlineImport.mjs +++ b/tests/local/usesInlineImport.mjs @@ -1,4 +1,3 @@ - async function writeJSConfigFile (config, filePath) { const eslint = (await import('eslint')) @@ -10,4 +9,9 @@ async function writeJSConfigFile (config, filePath) { }) } +writeJSConfigFile.importFSPromisesReadDir = async () => { + const {readdir} = (await import('node:fs/promises')) + return await readdir('path') +} + export default writeJSConfigFile diff --git a/tests/tests-node/esmock.node.test.js b/tests/tests-node/esmock.node.test.js index 60952c1b..4400ba1a 100644 --- a/tests/tests-node/esmock.node.test.js +++ b/tests/tests-node/esmock.node.test.js @@ -581,3 +581,14 @@ test('should mock imported json (strict)', async () => { Object.keys(importsJSON.JSONobj).sort().join(), 'test-example') assert.strictEqual(importsJSON.JSONobj['test-example'], 'test-json-b') }) + +test('mocks await import node:fs/promises', async () => { + const main = await esmock.p('../local/usesInlineImport.mjs', { + 'node:fs/promises': { + readdir: () => (['foo', 'bar']) + } + }) + + assert.deepStrictEqual( + await main.importFSPromisesReadDir(), ['foo', 'bar']) +}) From c8a055e52972ee28f36b5f29da65af3644ac75a7 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 21 Dec 2023 22:36:03 -0800 Subject: [PATCH 2/7] added global variant of test --- tests/tests-node/esmock.node.test.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/tests-node/esmock.node.test.js b/tests/tests-node/esmock.node.test.js index 4400ba1a..0cbe9402 100644 --- a/tests/tests-node/esmock.node.test.js +++ b/tests/tests-node/esmock.node.test.js @@ -583,12 +583,23 @@ test('should mock imported json (strict)', async () => { }) test('mocks await import node:fs/promises', async () => { - const main = await esmock.p('../local/usesInlineImport.mjs', { + const mainlocal = await esmock.p('../local/usesInlineImport.mjs', { 'node:fs/promises': { - readdir: () => (['foo', 'bar']) + readdir: () => (['mock', 'local']) } }) assert.deepStrictEqual( - await main.importFSPromisesReadDir(), ['foo', 'bar']) + await mainlocal.importFSPromisesReadDir(), ['mock', 'local']) +}) + +test('mocks await import node:fs/promises (global)', async () => { + const main = await esmock.p('../local/usesInlineImport.mjs', {}, { + 'node:fs/promises': { + readdir: () => (['mock', 'global']) + } + }) + + assert.deepStrictEqual( + await main.importFSPromisesReadDir(), ['mock', 'global']) }) From 4228154b6b925852b86745d55ac770548efc6ed3 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 21 Dec 2023 22:37:36 -0800 Subject: [PATCH 3/7] rename mocklocal variable as mock --- tests/tests-node/esmock.node.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tests-node/esmock.node.test.js b/tests/tests-node/esmock.node.test.js index 0cbe9402..d8ffeed0 100644 --- a/tests/tests-node/esmock.node.test.js +++ b/tests/tests-node/esmock.node.test.js @@ -583,14 +583,14 @@ test('should mock imported json (strict)', async () => { }) test('mocks await import node:fs/promises', async () => { - const mainlocal = await esmock.p('../local/usesInlineImport.mjs', { + const main = await esmock.p('../local/usesInlineImport.mjs', { 'node:fs/promises': { readdir: () => (['mock', 'local']) } }) assert.deepStrictEqual( - await mainlocal.importFSPromisesReadDir(), ['mock', 'local']) + await main.importFSPromisesReadDir(), ['mock', 'local']) }) test('mocks await import node:fs/promises (global)', async () => { From 49303a868b82e8c4ddc55bbf089c199f91632682 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 28 Dec 2023 11:44:55 -0800 Subject: [PATCH 4/7] update tests, use test files that target specifically builtin import trees --- tests/local/usesInlineBuiltinImport.js | 6 ++++++ tests/local/usesInlineBuiltinImportChild.js | 3 +++ tests/local/usesInlineImport.mjs | 5 ----- tests/tests-node/esmock.node.test.js | 4 ++-- 4 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 tests/local/usesInlineBuiltinImport.js create mode 100644 tests/local/usesInlineBuiltinImportChild.js diff --git a/tests/local/usesInlineBuiltinImport.js b/tests/local/usesInlineBuiltinImport.js new file mode 100644 index 00000000..ecfa8758 --- /dev/null +++ b/tests/local/usesInlineBuiltinImport.js @@ -0,0 +1,6 @@ +export default { + importFSPromisesReadDir: async () => { + const {readdir} = (await import('node:fs/promises')) + return await readdir('path') + } +} diff --git a/tests/local/usesInlineBuiltinImportChild.js b/tests/local/usesInlineBuiltinImportChild.js new file mode 100644 index 00000000..ef2b8dd3 --- /dev/null +++ b/tests/local/usesInlineBuiltinImportChild.js @@ -0,0 +1,3 @@ +import usesInlineBuiltinImport from './usesInlineBuiltinImport.js' + +export default usesInlineBuiltinImport diff --git a/tests/local/usesInlineImport.mjs b/tests/local/usesInlineImport.mjs index 268b76be..019584f1 100644 --- a/tests/local/usesInlineImport.mjs +++ b/tests/local/usesInlineImport.mjs @@ -9,9 +9,4 @@ async function writeJSConfigFile (config, filePath) { }) } -writeJSConfigFile.importFSPromisesReadDir = async () => { - const {readdir} = (await import('node:fs/promises')) - return await readdir('path') -} - export default writeJSConfigFile diff --git a/tests/tests-node/esmock.node.test.js b/tests/tests-node/esmock.node.test.js index d8ffeed0..c43cac15 100644 --- a/tests/tests-node/esmock.node.test.js +++ b/tests/tests-node/esmock.node.test.js @@ -583,7 +583,7 @@ test('should mock imported json (strict)', async () => { }) test('mocks await import node:fs/promises', async () => { - const main = await esmock.p('../local/usesInlineImport.mjs', { + const main = await esmock.p('../local/usesInlineBuiltinImport.js', { 'node:fs/promises': { readdir: () => (['mock', 'local']) } @@ -594,7 +594,7 @@ test('mocks await import node:fs/promises', async () => { }) test('mocks await import node:fs/promises (global)', async () => { - const main = await esmock.p('../local/usesInlineImport.mjs', {}, { + const main = await esmock.p('../local/usesInlineBuiltinImportChild.js', {}, { 'node:fs/promises': { readdir: () => (['mock', 'global']) } From 6596907841762dd7207790a83e78aeff9de87b39 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 28 Dec 2023 18:04:44 -0800 Subject: [PATCH 5/7] re-add querystring to url retured by load hook --- src/esmockLoader.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/esmockLoader.js b/src/esmockLoader.js index 2c049230..9770fc81 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -224,6 +224,12 @@ const load = async (url, context, nextLoad) => { } } + if (treeid && !url.includes(treeid)) { + // long querystring reduces readability of runtime error stacktrace + // smaller querystring `esmk=$id` does not clutter stacktrace + url = url + '?esmk=' + treeid.split('=')[1] + } + return nextLoad(url, context) } From 6f06b9c4dd598964119df7ba5bf5d3e879b30c6c Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 28 Dec 2023 18:52:50 -0800 Subject: [PATCH 6/7] update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e0947fc..5b85af91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,11 @@ # changelog - * 2.6.1 _???.??.2023_ + * 2.6.1 _Dec.30.2023_ * [update ci job to use checkout v4](https://github.com/iambumblehead/esmock/pull/279) * [update README to work w/ eslint-plugin-markdown](https://github.com/iambumblehead/esmock/pull/275) * [update eslint](https://github.com/iambumblehead/esmock/pull/276) disallow trailing whitespace * [add typescript example](https://github.com/iambumblehead/esmock/pull/278) to README + * [update esmock to correctly](https://github.com/iambumblehead/esmock/pull/282) mock builtins imported with `await import` thanks @btakita * 2.6.0 _Nov.07.2023_ * [typings: make MockFunction generic,](https://github.com/iambumblehead/esmock/pull/267) thanks @uwinkelvos * 2.5.9 _Nov.01.2023_ From fb7a53d94054ee2710dd1831e493e79d236e506f Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 28 Dec 2023 18:53:11 -0800 Subject: [PATCH 7/7] increment package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ea30c4ba..07c5ea4b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "esmock", "type": "module", - "version": "2.6.0", + "version": "2.6.1", "license": "ISC", "readmeFilename": "README.md", "description": "provides native ESM import and globals mocking for unit tests",