如何在 TypeScript 中全局导入 Chai 'expect()' 函数?
How can I import the Chai 'expect()' function globally in TypeScript?
其他相关问题只是问了JavaScript,但我知道Chai团队已经提供了'chai/register-expect',等等
我正在从 Jest 迁移到 Chai,当我使用 Jest 时,只需在文件 [=52= 的“类型”字段中键入 'jest' 即可完成]。然后 expect 函数被自动引用为 @types/jest index.d.ts
.
但是@types/chai或者Chai不支持这个。他们建议,在报告问题之前,到 Stack Overflow 上的 post。到底是什么,对吧?
浏览完后,我意识到每个文件都导入了 'expect' 函数,例如 TypeORM 或其他 TypeScript 项目...天哪,真是太棒了。
为什么我应该为每个文件导入 expect()?有没有办法避免这种情况?
我可以return开玩笑,但那种表现实在是太可怕了。最好为所有文件导入 expects。
mocha -r chai/register-expect
也不行。
我正在测试:
npx mocha -r node_modules/ts-node/register/transpile-only -r chai/register-expect -r ts-node/register -r tsconfig-paths/register some.test.ts
这是我的 tsconfig.json 文件。
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es5",
"noImplicitAny": false,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": "src",
"skipLibCheck": true,
"downlevelIteration" : true,
"paths": {
... bla bla
"main/*" : [
"main/*"
],
"controllers/*" : [
"main/controllers/*"
],
"middleware/*" : [
"main/middleware/*"
],
"*": [
"node_modules/*"
]
},
"types" : [
"node",
"mocha",
"chai"
],
"typeRoots": [
"node_modules/@types",
"types"
],
"lib": [
"es2017",
"dom"
],
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule" : true
},
"exclude": [
"node_modules"
],
"include": [
"src/**/*.ts",
"**/*.test.ts"
]
}
使用 Linux 命令部分解决问题...
for file in $(find src/test); do `sed -i '' "1i\
import { assert } from 'chai'; \
" $file;
done;
chai/register-expect
将注册一个全局 Chai 函数 expect
.
您需要通过创建自定义定义文件向您的 TypeScript 编译器解释您拥有它。
创建目录结构:
typings
global
index.d.ts
在您的 index.d.ts 文件中,添加以下内容:
declare const expect: Chai.ExpectStatic
现在你的 tsconfig.json 应该是这样的:
"typeRoots": [
"node_modules/@types", "./typings"
],
"types": [
"mocha",
"chai",
"node",
"global"
]
注意 typings
目录和 global
模块的导入。
这是必需的,因为 ts-node 通常 doesn't care about your custom typings。
其他相关问题只是问了JavaScript,但我知道Chai团队已经提供了'chai/register-expect',等等
我正在从 Jest 迁移到 Chai,当我使用 Jest 时,只需在文件 [=52= 的“类型”字段中键入 'jest' 即可完成]。然后 expect 函数被自动引用为 @types/jest index.d.ts
.
但是@types/chai或者Chai不支持这个。他们建议,在报告问题之前,到 Stack Overflow 上的 post。到底是什么,对吧?
浏览完后,我意识到每个文件都导入了 'expect' 函数,例如 TypeORM 或其他 TypeScript 项目...天哪,真是太棒了。
为什么我应该为每个文件导入 expect()?有没有办法避免这种情况?
我可以return开玩笑,但那种表现实在是太可怕了。最好为所有文件导入 expects。
mocha -r chai/register-expect
也不行。
我正在测试:
npx mocha -r node_modules/ts-node/register/transpile-only -r chai/register-expect -r ts-node/register -r tsconfig-paths/register some.test.ts
这是我的 tsconfig.json 文件。
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es5",
"noImplicitAny": false,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": "src",
"skipLibCheck": true,
"downlevelIteration" : true,
"paths": {
... bla bla
"main/*" : [
"main/*"
],
"controllers/*" : [
"main/controllers/*"
],
"middleware/*" : [
"main/middleware/*"
],
"*": [
"node_modules/*"
]
},
"types" : [
"node",
"mocha",
"chai"
],
"typeRoots": [
"node_modules/@types",
"types"
],
"lib": [
"es2017",
"dom"
],
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule" : true
},
"exclude": [
"node_modules"
],
"include": [
"src/**/*.ts",
"**/*.test.ts"
]
}
使用 Linux 命令部分解决问题...
for file in $(find src/test); do `sed -i '' "1i\
import { assert } from 'chai'; \
" $file;
done;
chai/register-expect
将注册一个全局 Chai 函数 expect
.
您需要通过创建自定义定义文件向您的 TypeScript 编译器解释您拥有它。
创建目录结构:
typings
global
index.d.ts
在您的 index.d.ts 文件中,添加以下内容:
declare const expect: Chai.ExpectStatic
现在你的 tsconfig.json 应该是这样的:
"typeRoots": [
"node_modules/@types", "./typings"
],
"types": [
"mocha",
"chai",
"node",
"global"
]
注意 typings
目录和 global
模块的导入。
这是必需的,因为 ts-node 通常 doesn't care about your custom typings。