8

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 test command to package.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-typescript to babel.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?

2
  • same issue... Any fix? Commented Mar 9, 2022 at 19:59
  • I have same similar issue when I update Jest from 27.5.1 to >=28. Are you sure you have error about .js file, not .ts ? Error: Jest: Failed to parse the TypeScript config file C:...jest.config.js Commented Aug 30, 2022 at 23:23

2 Answers 2

2

This worked for me from Jest docs:

  • create and add file jest.config.ts to your source directory with content like:
    /** @type {import('jest').Config} */
        const config = {
            verbose: true,
        };
      
    module.exports = config;
  • Then, make sure to include the file in your tsconfig.json file:
    ...
    "include": ["./src/**/*.tsx", "./src/**/*.ts", "src/jest.config.ts"]
  • in your packages.json: Ensure also that you have installed jest, ts-jest, and ts-node-dev modules:
    ...
    },
      "devDependencies": {
        "@faker-js/faker": "^7.5.0",
        "@types/express": "^4.17.14",
        "@types/jest": "^29.0.3",
        "@types/uuid": "^8.3.4",
        "jest": "^29.0.3",
        "nodemon": "^2.0.20",
        "ts-jest": "^29.0.2",
        "ts-node-dev": "^2.0.0",
        "typescript": "^4.8.3"
      }

and that the jest transform configuration script looks like:

"jest": {
    "transform": {
      ".(ts|tsx)": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ]
  },
Sign up to request clarification or add additional context in comments.

2 Comments

why do I need a jest transform config in the package.json, when I have a jest.config.ts file?
in latest jest you do not need ts-jest and ts-node-dev, you also do not have to include jest.config.ts to tsconfig.json
1

This issue was fixed for me by just installing TS Node.

1 Comment

I already had installed and old version of ts-node library, then I updated it, it's working now. You point me to the right direction. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.