Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test import nodefs promises #282

Merged
merged 7 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'])
})
Loading