-
Notifications
You must be signed in to change notification settings - Fork 19
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
Is it possible to mock json imports with assert? #246
Comments
@Jafferwaffer thanks for opening this issue. This json-importing scenario is not supported, but I'll prepare a PR and message you when that's ready. |
The json import syntax is a stage 3 proposal, with syntax currently looking like this import { x } from "./mod" with { type: "json" } The eslint pipeline at the PR fails because eslint does not recognize the import syntax. Also, there seems to be a nodejs upstream error related to "assert" and "with" attributes; an exact reproduction made here: https://github.com/iambumblehead/nodejs-import-attributes-repro related links, |
@Jafferwaffer another way to mock the json is to do something like this; load the json with the 'node:fs' module, then mock node:fs to return json needed for the test. ./src/App.js import fs from 'node:fs'
const someJson = JSON.parse(await fs.readFileSync('/path/to/some.json', 'utf-8'))
// ...
export default App ./test/App.test.js const fakeJson = { foo: 1 }
const myApp = await esmock('../src/App.js', {
'node:fs': {
readFileSync: () => JSON.stringify(fakeJson)
}
}) @Jafferwaffer what do you think of the "node:fs" solution above? Would it be okay to close the PR and update the wiki with that suggestion? |
@iambumblehead thanks for the quick response! Yes, the As I have multiple json imports in the same file I have to replace I used
import fs from 'node:fs'
const someJson = JSON.parse(fs.readFileSync('/path/to/some.json', 'utf-8'));
const moreJson = JSON.parse(fs.readFileSync('/path/to/more.json', 'utf-8'));
// ...
export default App
const readFileStub = sinon.stub();
readFileStub.withArgs('/path/to/some.json').returns(JSON.stringify({ foo: 1 }));
readFileStub.withArgs('/path/to/more.json').returns(JSON.stringify({ bar: 1 }));
const myApp = await esmock('../src/App.js', {
'node:fs': {
readFileSync: readFileStub
}
}) Are there any plans to support this Yes this issue can be closed, would be nice to add this to wiki as I see others likely to run into same issue. Thanks again. |
@Jafferwaffer an upstream issue is created for the loader-related issue blocking this feature nodejs/node#49724 if/when that is resolved, we could update the PR to fix the failing lint pipeline ("ignore" that failing file) and support could be added. |
its been a few weeks since the upstream issue was resolved, but still the json-importing test only passes for node 21 #266 it is merged, but note, it does not work for node 18 or node 20 |
Hi there!
Firstly, thanks for this package it's helped a bunch.
I'm wondering if its possible to mock json imports which require assertions. Otherwise, are there any suggested workarounds please?
Example:
./src/App.js
./test/App.test.js
So far I'm running into the following error on Node v18 running with Ava:
Any guidance would be appreciated, thanks in advance
The text was updated successfully, but these errors were encountered: