Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Jest global tests setup with .ts file (TypeScript)

I am using ts-jest (Jest and TypeScript) and want to configure some global setup for all test suites (initialize test database).

I found that there is globalSetup options in jest configuration:

"jest": {
    "globalSetup": "./jest-config.js"
}

but only .js file can be used for setup. I want to use .ts file because all my code including code for setup in TypeScript.

How can I achieve this?

like image 918
Aleksei Semidotskii Avatar asked Jan 18 '18 09:01

Aleksei Semidotskii


People also ask

Does Jest work with ts?

TypeScript Unit Testing jest (ts-jest) jest is painless JavaScript testing framework by Facebook, with ts-jest can be used to test TypeScript code. PASS ./fizzBuzz. test.

Does Jest need ts node?

When you run jest with a jest. config. ts file it will use ts-node to compile that file, then it will pass it to ts-jest which will compile your tests, then it will pass those . js tests to jest to run it.

Does Jest compile TypeScript?

Configure Jest ts-jest is a TypeScript preprocessor for jest , that lets you use jest to test projects written in TypeScript. This will create a file named jest. config. js with a setting for jest to use the preprocessor js-test .


3 Answers

I found a solution.

My initial project structure was:

.
|--src
|  |--service.ts
|--tests
|  |--service.test.ts
|--dist
|--tsconfig.json 
|--package.json

Jest configuration in package.json:

"jest": {
  "globals": {
    "ts-jest": {
      "tsconfig": "tsconfig.json"
    }
  },
  "moduleFileExtensions": ["ts","js"],
  "transform": {
    "^.+\\.(ts|tsx)$": "./node_modules/ts-jest/preprocessor.js"
  },
  "testMatch": [
    "**/tests/**/*.test.(ts|js)"
  ],
  "testEnvironment": "node"
}

With this configuration ts-jest perform in memory transpiling of the .ts files. I have outDir option in compilerOptions of my tsconfig.json, but nothing is written in that outDir and because I cann't use transpiled jest-config.js

Then I move tests folder in src and change Jest config. New structure of the project is:

.
|--src
|  |--service.ts
|  |--tests
|     |--service.test.ts
|     |--jest-config.ts
|--dist
|--tsconfig.json 
|--package.json

New Jest config is:

"jest": {
  "globalSetup": "./dist/tests/jest-config.js",
  "moduleFileExtensions": ["ts", "js", "json"],
  "testMatch": [
    "**/dist/**/*.test.js"
  ],
  "testEnvironment": "node"
}

Now I can use jest-config.js for global setup in Jest.

Addition

  • Jest setup file (in my example jest-config.js) must export one async function:

    module.exports = async function() { ... }

  • Before running the tests, you need to compile source .ts files.

like image 60
Aleksei Semidotskii Avatar answered Oct 25 '22 23:10

Aleksei Semidotskii


Global setup is performed before a ts environment is made available, so a workaround is to create the environment manually by requiring ts-node at the beginning of the file.

In your jest configuration (package.json or jest.config.js):

"globalSetup": "./globalSetup.ts"

Then in globalSetup.ts:

require('ts-node/register');

const setup = (): void => {
  // whatever you need to setup globally
};

export default setup;

You can read more about ts-node on github.

like image 41
remeus Avatar answered Oct 25 '22 23:10

remeus


For anyone with TS error with @remeus's answer, the following change will do the trick:

require('ts-node').register({
  transpileOnly: true,
});

If you're using absolute imports, use this:

require('tsconfig-paths').register();
like image 28
glinda93 Avatar answered Oct 25 '22 22:10

glinda93