GithubHelp home page GithubHelp logo

Comments (6)

iambumblehead avatar iambumblehead commented on June 4, 2024 1

@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.

iambumblehead avatar iambumblehead commented on June 4, 2024 1

@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.

iambumblehead avatar iambumblehead commented on June 4, 2024

@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.

iambumblehead avatar iambumblehead commented on June 4, 2024

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.

Jafferwaffer avatar Jafferwaffer commented on June 4, 2024

@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.

iambumblehead avatar iambumblehead commented on June 4, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.