How can I import the Chai 'expect()' function globally in TypeScript?
Asked Answered
E

1

6

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

I 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.

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?

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.

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

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 is not working either.

I was testing:

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

Here is my tsconfig.json file.

{
    "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"
    ]
}
Edora answered 5/5, 2020 at 21:20 Comment(0)
N
8

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

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

Create a directory structure:

typings
    global
       index.d.ts

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

declare const expect: Chai.ExpectStatic

Now your tsconfig.json should be something like this:

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

Note the typings directory and importing of the global module.

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

Netti answered 22/8, 2020 at 6:22 Comment(1)
Oh I'm realized the working of typescript type matching custom setting !! I OWE IT ALL TO YOUR HELPS. Holy Thank you your teaching !Edora

© 2022 - 2024 — McMap. All rights reserved.