如何在 TypeScript 中全局导入 Chai 'expect()' 函数? [英] How can I import the Chai 'expect()' function globally in TypeScript?

查看:42
本文介绍了如何在 TypeScript 中全局导入 Chai 'expect()' 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其他相关问题只是关于 JavaScript,但我知道 Chai 团队已经提供了'chai/register-expect'等.

Other related questions are just asked about JavaScript, but I know the Chai team already provided 'chai/register-expect', etc..

我正在从 Jest 迁移到 Chai,当我使用开玩笑它只是通过在类型"中输入开玩笑"来完成的.文件 tscofnig.json 中的字段.然后 expect 函数自动被 @types/jest index.d.ts 引用.

I'm was migrating from Jest to Chai, and when I used Jest it was done just by typing 'jest' to the "types" field in file tscofnig.json. Then the expect function was automatically referred to with @types/jest index.d.ts.

但是@types/chai 或 Chai 不支持这个.他们建议在报告问题之前在 Stack Overflow 上发帖.到底是什么,对吧?

But @types/chai or Chai do not support this. And they recommend, before reporting an issue, to post on Stack Overflow. What on Earth, right?

冲浪之后,我意识到每个人都为每个文件导入了期望"函数,例如 TypeORM或其他 TypeScript 项目......天啊,太棒了.

After surfing about this, I realize everyone imports the 'expect' function per file, like TypeORM or other TypeScript projects... Holy bleep, it is so awwwwwwwful.

我到底为什么要为每个文件导入 expect() ?没有办法避免吗?

Why on Earth should I import expect() per file? Isn't there a way to avoid that?

我可以回到 Jest,但那种表现太可怕了.最好为所有文件导入 expects.

I can return to Jest, but that performance is so horrible fecal matter. It is better importing expects for all files.

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"
    ]
}

推荐答案

chai/register-expect 将注册一个全局的 Chai 函数 expect.

chai/register-expect will register a global Chai function expect.

您需要通过创建自定义定义文件向您的 TypeScript 编译器解释您拥有它.

You need to explain to your TypeScript compiler that you have it by creating a custom definition file.

创建目录结构:

typings
    global
       index.d.ts

在您的 index.d.ts 文件中,添加以下内容:

In your index.d.ts file, add the following:

declare const expect: Chai.ExpectStatic

现在你的 tsconfig.json 应该是这样的:

Now your tsconfig.json should be something like this:

    "typeRoots": [
      "node_modules/@types", "./typings"
    ],
    "types": [
      "mocha",
      "chai",
      "node",
      "global"
    ]

注意 typings 目录和 global 模块的导入.

Note the typings directory and importing of the global module.

这是必需的,因为 ts-node 通常 不需要关心您的自定义类型.

This is needed because ts-node generally doesn't care about your custom typings.

这篇关于如何在 TypeScript 中全局导入 Chai 'expect()' 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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