Sandaru
Sandaru

Reputation: 81

How to mock JSON import Jest TypeScript

I want to mock a .json file using Jest for unit testing in Typescript.

I'm currently using this global mock inside jest.config.js file. And this is working fine:

    'package.json': '<rootDir>/__tests__/tasks/__mocks__/data.json'

But I want to mock it locally, inside my test class.

This didn't work for me:

jest.mock('../../package.json', () => ({
    package : { name: '__name__', 'version': '__version__'};
}), { virtual: true })

Upvotes: 2

Views: 4827

Answers (1)

Boobalan
Boobalan

Reputation: 855

have you try the following?

const packageJson = require('../../package.json);
 jest.mock(packageJson, () => ({
     package : { name: '__name__', 'version': '__version__'};
 }), { virtual: true })

Upvotes: 0

Related Questions