16

I'm trying to run some tests with Jest but I keep getting an error. It's something to do with a TypeScript config file, I already searched but didn't find much. I'm using NodeJS, TypeORM with TypeScript.

I'm getting this error when running yarn test:

Error: Jest: Failed to parse the TypeScript config file

C:\Users\demis\Documents\Projects\Personal\nlw_node\jest.config.ts TypeError: registerer.enabled is not a function*** at readConfigFileAndSetRootDir (C:\Users\demis\Documents\Projects\Personal\nlw_node\node_modules\jest-config\build\readConfigFileAndSetRootDir.js:150:13) at readConfig (C:\Users\demis\Documents\Projects\Personal\nlw_node\node_modules\jest-config\build\index.js:217:18) at readConfigs (C:\Users\demis\Documents\Projects\Personal\nlw_node\node_modules\jest-config\build\index.js:406:26) at runCLI (C:\Users\demis\Documents\Projects\Personal\nlw_node\node_modules@jest\core\build\cli\index.js:230:59) at Object.run (C:\Users\demis\Documents\Projects\Personal\nlw_node\node_modules\jest-cli\build\cli\index.js:163:37)

First.test.ts:

describe("First", () => {
  it("should", () => {
    expect(2 + 2).toBe(4);
  });
});

jest.config.ts:

export default {
  bail: true,
  clearMocks: true,
  coverageProvider: "v8",
  preset: "ts-jest",
  testEnvironment: "node",
  testMatch: ["**/tests/*.test.ts"]
};

tsconfig.json:

{
   "compilerOptions": {
      "lib": [
         "es5",
         "es6"
      ],
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "node",
      "outDir": "./build",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "sourceMap": true
   }
}

package.json:

{
  "name": "nlw_node",
  "version": "0.0.1",
  "description": "Awesome project developed with TypeORM.",
  "devDependencies": {
    "@types/express": "^4.17.11",
    "@types/jest": "^26.0.20",
    "@types/node": "^8.0.29",
    "@types/uuid": "^8.3.0",
    "jest": "^26.6.3",
    "nodemon": "^2.0.7",
    "ts-jest": "^26.5.3",
    "ts-node": "3.3.0",
    "typescript": "^4.2.3"
  },
  "dependencies": {
    "body-parser": "^1.18.1",
    "express": "^4.15.4",
    "mysql": "^2.14.1",
    "reflect-metadata": "^0.1.10",
    "typeorm": "0.2.31",
    "uuid": "^8.3.2"
  },
  "scripts": {
    "start": "nodemon src/index.ts",
    "typeorm": "ts-node node_modules/typeorm/cli.js",
    "test": "jest"
  }
}

ormconfig.ts:

{
  "type": "mysql",
  "host": "localhost",
  "port": 3306,
  "username": "root",
  "password": "root",
  "database": "nlw_node",
  "synchronize": true,
  "logging": false,
  "entities": ["src/api/models/**.ts"],
  "migrations": ["src/database/migrations/**.ts"],
 
  "cli": {
    "entitiesDir": "src/entity",
    "migrationsDir": "./src/database/migrations",
    "subscribersDir": "src/subscriber"
  }
}
1
  • The problem is you have type="module" in your package.json (which is required by sveltekit in my case). I have no idea how to fix though. Commented May 4, 2022 at 7:23

8 Answers 8

11

Remove the ts-node package and install ts-node-dev instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Upgrading ts-node version to latest (^10.9.1 in my case) worked for me.
10

I had the silliest of issues. There was a comma at the end of my tsconfig file like the following:-

{
  ...
  "ts-node": {
    "compilerOptions": {
      "jsx": "react-jsx",
      "module": "commonjs",
      "jsxImportSource": "",
      "paths": {
        "~/*": [
          "./src/*"
        ]
      }
    },
    "require": [
      "tsconfig-paths/register"
    ],
    "transpileOnly": true
  }, // <----- This was the offending comma
}

Removing this comma fixed the issue for me.

3 Comments

This led me to the correct solution for my case. I had commented out a compilerOption which normally isn't an issue and went unnoticed until I ran tests. In my jest.config I import tsconfig.json for paths. At that point tsconfig needs to be properly formed JSON.
I would have been stuck on this issue for hours if I didn't see this answer. This worked for me, I had an extra comma at the end of my paths. Thank you so much
check you also have no comments in the tsconfig.json
4

I had exactly the same error this am, trying to run tests in jest with Angular and Typescript.

My problem was solved by re-installing jest:

npm install --save-dev jest

This then showed an underlying configuration issue with my set up - but at least I don't see the failed error.

Comments

2

Installing ts-jest should solve this problem if you have ts-node installed.

Comments

1

Check if the babel.config.js file is in the root / folder of the project, if is not the problem follow these instructions

Comments

1

Wanting to leave this here just in case it helps someone.

TLDR; run your IDE's "format this file" functionality on your tsconfig.json.

This is the error I was getting:

SyntaxError: /Users/foo/bar/baz/project/tsconfig.json: Expected double-quoted property name in JSON at position 495 (line 20 column 5)

I couldn't find the offending (assumed) single quote, or, well, anything wrong with it all. I wasted so much time on this lol..

The fix was to literally just right click inside my tsconfig.json file and format it (I'm using VSCode FYI).

Comments

0

I added exclude and include on my tsconfig.json file and it worked.

{
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "rootDir": "./",
    "baseUrl": "./src",
    "outDir": "./dist",
    "allowJs": true,
    "noEmit": false,
    "noEmitOnError": true,
    "noUnusedLocals": true,
    "strict": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "forceConsistentCasingInFileNames": true,
    "removeComments": true,
    "skipLibCheck": true,
    "typeRoots": [
      "@types",
      "./node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules",
    "dist"
  ],
  "include": [
    "src/**/*",
    "src/__tests__",
    "jest.config.js"
  ]
}

Comments

0

I had the same problem, but the tests were running once.

It turned out I needed to delete node_modules and yarn.lock and reinstall everything again.

Comments

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.