Search code examples
typescripttypeschaitsconfig

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


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

Solution

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