使用 .ts 文件(TypeScript)配置 Jest 全局测试设置 [英] Configure Jest global tests setup with .ts file (TypeScript)

查看:78
本文介绍了使用 .ts 文件(TypeScript)配置 Jest 全局测试设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ts-jest(Jest 和 TypeScript)并且想要配置所有测试套件的一些全局设置(初始化测试数据库).

我发现jest配置中有globalSetup选项:

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

但只有 .js 文件可用于设置.我想使用 .ts 文件,因为我所有的代码包括用于在 TypeScript 中进行设置的代码.

我怎样才能做到这一点?

解决方案

我找到了解决方案.

我最初的项目结构是:

<预><代码>.|--src||--service.ts|--测试||--service.test.ts|--dist|--tsconfig.json|--package.json

package.json 中的 Jest 配置:

玩笑":{全局":{ts-jest":{tsconfig":tsconfig.json"}},moduleFileExtensions":[ts",js"],变换":{^.+\.(ts|tsx)$":./node_modules/ts-jest/preprocessor.js"},测试匹配":["**/tests/**/*.test.(ts|js)";],测试环境":节点"}

使用此配置 ts-jest 在内存中执行 .ts 文件的转换.我的 tsconfig.jsoncompilerOptions 中有 outDir 选项,但在那个 outDir 中没有写任何内容,因为我不能使用转译的笑话 -配置文件

然后我在 src 中移动测试文件夹并更改 Jest 配置.项目的新结构是:

<预><代码>.|--src||--service.ts||--测试||--service.test.ts||--jest-config.ts|--dist|--tsconfig.json|--package.json

新的 Jest 配置是:

玩笑":{全局设置":./dist/tests/jest-config.js",moduleFileExtensions":[ts",js",json"],测试匹配":[**/dist/**/*.test.js"],测试环境":节点"}

现在我可以在 Jest 中使用 jest-config.js 进行全局设置.

添加

  • Jest 设置文件(在我的示例 jest-config.js 中)必须导出一个异步函数:

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

  • 在运行测试之前,您需要编译源 .ts 文件.

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?

解决方案

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.

这篇关于使用 .ts 文件(TypeScript)配置 Jest 全局测试设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆