My React application refers version
in the imported package.json
.
import pjson from './package.json';
// snip
<p>Version: ${pjson.version}</p>
In the example above, Jest would display an error message like this:
TypeError: Cannot read properties of undefined (reading 'version')
69 |
> 70 | <p>Version: v{pjson.version}</p>
| ^
How can I mock this up?
CodePudding user response:
I made these files. But the same.
jest.config.ts
:
const config: Config.InitialOptions = {
moduleNameMapper: {
'^. \\.json$': '<rootDir>/__mocks__/mock.json',
},
};
__mocks__/mock.json
:
{
"version": "0"
}
CodePudding user response:
Just importing with asterisks solved the problem.
Neither mock
nor moduleNameMapper
is needed.
src/App.tsx
import * as pjson from './package.json';