其他相关问题只是在js上问,但我知道Chai团队已经提供了“ chai / register-expect”等。
我正在从玩笑迁移到柴,当我使用玩笑完成时,只需在tscofnig.json中的“类型”字段中键入“玩笑”,然后使用@ types / jest index.d.ts自动引用期望函数文件。
但@ types / chai或chai不支持此功能。并且他们建议在编写问题之前,在Stackoverflow上发布。 WTF对吗?
在浏览完此内容后,我意识到每个人都像typeorm或其他打字稿项目一样,每个文件都导入了“ expect”功能。真是太好了。
每个文件我应该导入什么?没有办法吗?
我可以开玩笑,但那种表现太糟糕了。最好导入所有文件的期望值。
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"
]
}
答案 0 :(得分:1)
chai/register-expect
将注册全局chai函数expect
。
您需要通过创建自定义定义文件来向打字稿编译器说明已拥有该文件。
创建目录结构:
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节点通常为doesn't care about your custom typings
答案 1 :(得分:0)
部分解决Linux命令问题。
for file in $(find src/test); do `sed -i '' "1i\\
import { assert } from 'chai'; \\
" $file;
done;