Comments (6)
@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?
from esmock.
@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.
from esmock.
@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.
from esmock.
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,
from esmock.
@iambumblehead thanks for the quick response!
Yes, the fs
way of doing things works for me, nice suggestion 👍 very much looking forward to when this json import proposal is resolved.
As I have multiple json imports in the same file I have to replace I used sinon
to stub & return different json based on the args, example:
./src/App.js
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
./test/App.test.js
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 assert
syntax? Or will you wait until the proposal is accepted?
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.
from esmock.
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
from esmock.
Related Issues (20)
- Mocking a file with a hashbang using the `import` key fails HOT 7
- Error when using `import` key with modules that have CJS imports HOT 9
- 2.3.4 - 'RangeError [ERR_UNKNOWN_MODULE_FORMAT]: Unknown module format: undefined' HOT 8
- Breakage in Node.js v20.6.0 HOT 18
- `invalid moduleId` in a TypeScript monorepo HOT 35
- yarn pnp issues HOT 14
- Support Yarn PnP resolver out of the box HOT 11
- Get rid of old Node versions support HOT 8
- resolve error introduced with 2.5.2 HOT 10
- Error when mocking a file that exports an array. HOT 1
- c8 incorrect branch coverage; modules and `import` globals HOT 11
- how to mock globalThis HOT 4
- import('node:fs/promises') called within a library does not seem to be replaced HOT 30
- Path alias support HOT 1
- Unexpected strict mode reserved word syntax error when mocking express HOT 2
- incompatible with eslint-plugin-import HOT 3
- No support for subpath patterns? HOT 14
- Version 2.6.3 changed the way to mock libraries HOT 7
- ESMock seems to wrongfully alter Sinon stubs. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from esmock.