This tutorial is an element 2 of two within the sequence.
Right here you’ll learn to use Jest with Babel Module Resolver for aliases which might be outlined in your .babelrc file:
{
...
"plugins": [
[
"module-resolver",
{
"root": ["./"],
"alias": {
"@elements": "./src/elements",
"@constants": "./src/constants",
}
}
],
]
}
With the intention to get the identical alias mappings to Jest, the jest.config.js file must seem like this:
module.exports = {
roots: ['<rootDir>'],
moduleFileExtensions: ['js', 'ts', 'tsx', 'json'],
testPathIgnorePatterns: ['./node_modules/'],
moduleNameMapper: {
'^@elements(.*)$': '<rootDir>/src/elements$1',
'^@constants(.*)$': '<rootDir>/src/constants$1',
},
testEnvironment: 'jsdom',
rework: tsx)$': 'babel-jest',
,
};
Now you should use import statemes with aliases in your Jest testing surroundings too.