I’m trying to learn and implement jest unit tests on a few existing projects.
We have multiple React apps with a shared utilities file that is one level up each project root folder.
import utils from '../../../utilities';
I’m getting this message:
Cannot find module ‘../../../utilities’ from ‘src/features/component.jsx’
I have tried to add “../” to the roots parameter in config. I’ve tried a few other entries in the roots array but can’t get this to work.
const config = {
roots:[
"<rootDir>",
"../"
],
moduleNameMapper: {
'\.(png)$': 'identity-obj-proxy', //'<rootDir>/__mocks__/fileMock.js',
'\.(css|less)$': 'identity-obj-proxy',
},
moduleDirectories:["node_modules", "src"],
transform: {
'^.+\.(js|jsx)$': 'babel-jest'
},
testEnvironment: 'node',
};
module.exports = config;
I have tried mocking the utils; but I might be doing this wrong.
jest.mock('utilities', () => ({}));
This gives me this message:
Cannot find module ‘utilities’ from ‘src/features/component.spec.js’
The utilities is just a few shared functions, has been carried over for years and is formatted as:
let utils = {
...
};
export default utils;
- Would we need to copy utilities for each react app?
- Or is there a way to keep a single shared utilities file and include it in our jest tests?
- Or is there a different work around to get some unit tests with a single utilities file?