Skip to content

Commit

Permalink
Merge pull request #282 from iambumblehead/test-import-nodefs-promises
Browse files Browse the repository at this point in the history
Test import nodefs promises
  • Loading branch information
iambumblehead authored Jan 14, 2024
2 parents ed2522e + fb7a53d commit 44fda68
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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_
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/esmockLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
6 changes: 6 additions & 0 deletions tests/local/usesInlineBuiltinImport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
importFSPromisesReadDir: async () => {
const {readdir} = (await import('node:fs/promises'))
return await readdir('path')
}
}
3 changes: 3 additions & 0 deletions tests/local/usesInlineBuiltinImportChild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import usesInlineBuiltinImport from './usesInlineBuiltinImport.js'

export default usesInlineBuiltinImport
1 change: 0 additions & 1 deletion tests/local/usesInlineImport.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

async function writeJSConfigFile (config, filePath) {
const eslint = (await import('eslint'))

Expand Down
22 changes: 22 additions & 0 deletions tests/tests-node/esmock.node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,25 @@ 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/usesInlineBuiltinImport.js', {
'node:fs/promises': {
readdir: () => (['mock', 'local'])
}
})

assert.deepStrictEqual(
await main.importFSPromisesReadDir(), ['mock', 'local'])
})

test('mocks await import node:fs/promises (global)', async () => {
const main = await esmock.p('../local/usesInlineBuiltinImportChild.js', {}, {
'node:fs/promises': {
readdir: () => (['mock', 'global'])
}
})

assert.deepStrictEqual(
await main.importFSPromisesReadDir(), ['mock', 'global'])
})

0 comments on commit 44fda68

Please sign in to comment.