Overriding `tsconfig.json` for ts-node in mocha

前端 未结 8 2060
迷失自我
迷失自我 2020-12-13 23:38

Is it possible to override which tsconfig.json ts-node uses when called from mocha?

My main tsconfig.json contains \"module\": \"es20

8条回答
  •  庸人自扰
    2020-12-14 00:10

    --compilerOptions wont' work.

    What you need to do is customize how you register ts-node. My case was a little bit different from yours, I wanted it to use test/tsconfig.json, which contained settings needed by my test code. If I just used --require ts-node/register, it was using a default configuration that did not contain the settings needed to run my tests.

    What I did was:

    1. Create a file test/tshook.js. It contains:

      require("ts-node").register({
        project: "test/tsconfig.json",
      });
      
    2. I edited my test/mocha.opts to have:

      --require test/tshook.js
      test/**/*.ts
      

    This should will pass the desired setting to ts-node:

    require("ts-node").register({
      compilerOptions: {
        module: "commonjs",
      },
    });
    

提交回复
热议问题