如何在typescript上全局导入chai expect函数?

问题描述 投票:0回答:1

其他相关的问题就在js上问吧,但我知道Chai团队已经提供了'chairegister-expect'等等。

我正在从jest迁移到chai,当我使用jest时,只需在tscofnig.json中的" types "字段中输入 "jest",然后期望函数就会自动被@typesjest index.d.ts文件引用。

但 @typeschai 或 chai 不支持这个。而且他们建议在写问题之前,先在Stackoverflow上发帖。WTF 是吧?

在浏览了这个之后,我意识到每个人都导入'期望'功能,每个文件像typeorm或其他typescript项目...。我的天啊,这真是太可怕了。

什么是地狱,我应该导入期望每一个文件? 有没有办法?

我可以返回jest,但性能是如此可怕的狗屎。它是更好的导入期望的所有文件。

它是不工作的 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"
    ]
}
typescript types chai tsconfig
1个回答
0
投票

用linux命令解决部分问题。

for file in $(find src/test); do `sed -i '' "1i\\
import { assert } from 'chai'; \\
"  $file;
done;
© www.soinside.com 2019 - 2024. All rights reserved.