I'm trying to install Jest for use with Babel and Typescript. I've followed the instructions shown here to the letter but I'm getting:
Error: Jest: Failed to parse the TypeScript config file C:...jest.config.js`
...when I run npm run test.
The contents of jest.config.ts is:
export default {
//lots of commented-out stuff in here - nothing at all uncommented
}
Here's the exact steps I did:
- init project -
npm init -y - install Jest -
npm -i --save-dev jest - make a script (sum.js) and a test (sum.test.js) file:
sum.js:
function sum(a, b) {
return a + b;
}
module.exports = sum;
sum.test.js
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
- add
testcommand topackage.json:
package.json:
{
"name": "jest-learn",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.17.5",
"@babel/preset-env": "^7.16.11",
"@babel/preset-typescript": "^7.16.7",
"babel-jest": "^27.5.1",
"jest": "^27.5.1"
}
}
At this point, if I run yarn test, my test runs and passes just fine. The problem is when I try to introduce Babel and Typescript. Steps continue:
- install Babel deps:
npm -i --dev babel-jest @babel/core @babel/preset-env - create
babel.config.js - install Typescript:
npm -i --dev @babel/preset-typescript - add
@babel/preset-typescripttobabel.config.js:
babel.config.js:
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
};
Now when I run npm run test I get the above error. What did I do wrong?
Error: Jest: Failed to parse the TypeScript config file C:...jest.config.js