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

Is it possible to mock json imports with assert? #246

Closed
Jafferwaffer opened this issue Sep 18, 2023 · 6 comments
Closed

Is it possible to mock json imports with assert? #246

Jafferwaffer opened this issue Sep 18, 2023 · 6 comments

Comments

@Jafferwaffer
Copy link

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

import someJson from './some.json' assert { type: 'json' };
...

export default App;

./test/App.test.js

  const fakeJson = { foo: 1 }; 
  const myApp = await esmock('../src/App.js',
  {
    '../src/some.json': fakeJson
  });

So far I'm running into the following error on Node v18 running with Ava:

  TypeError {
    code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING',
    message: 'Module "file:///src/some.json" needs an import assertion of type "json"',
  }

Any guidance would be appreciated, thanks in advance

@iambumblehead
Copy link
Owner

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

@iambumblehead
Copy link
Owner

iambumblehead commented Sep 18, 2023

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,

@iambumblehead
Copy link
Owner

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

@Jafferwaffer
Copy link
Author

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

@iambumblehead
Copy link
Owner

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

@iambumblehead
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants