4

I have the following project structure

|_typetests
|          |_type.test.ts
|
|
myproj.d.ts
tsconfig.json

My tsconfig.json looks like this:

{
  "compilerOptions": {
      "module": "commonjs",
      "moduleResolution": "node",
      "lib": [
          "es6"
      ],
      "target": "es6",
      "noImplicitAny": true,
      "noImplicitThis": true,
      "strictNullChecks": true,
      "types": [
          "node",
          "mocha"
      ],
      "noEmit": true,
      "forceConsistentCasingInFileNames": true,
      "baseUrl": "./"
  },
  "include": [
      "types/*.test.ts"
  ],
  "exclude": ["node_modules"]
}

If I run ./node_modules/.bin/tsc -p . --traceResolution

Then I can see:

Module name 'myproj' was successfully resolved to '/Users/paulcowan/projects/myproj/myproj.d.ts'. ========

But when I run the following through mocha

./node_modules/.bin/mocha -r ts-node/register types/*.test.ts

Error: Cannot find module 'myproj'

dagda1
  • 26,856
  • 59
  • 237
  • 450

1 Answers1

15

Try use --files flag to solve your issue.

Or set TS_NODE_FILES environment variable to true and try again

TS_NODE_FILES=true ./node_modules/.bin/mocha -r ts-node/register types/*.test.ts

hoangdv
  • 15,138
  • 4
  • 27
  • 48
  • still no cigar, is there anything I can do debug this better, I don't think it is picking up the tsconfig.json – dagda1 May 04 '19 at 17:34
  • @dagda1 try add `typeRoots` option to you tsconfig file. `"typeRoots": ["./node_modules/@types", "./myproj.d.ts"]` and try again with, without my option. – hoangdv May 04 '19 at 17:41
  • same deal, I don't think it is picking up the tsconfig.json – dagda1 May 04 '19 at 17:43
  • ts-node loads tsconfig.json automatically. – hoangdv May 04 '19 at 17:51
  • 1
    https://stackoverflow.com/questions/51610583/ts-node-ignores-d-ts-files-while-tsc-successfully-compiles-the-project – hoangdv May 04 '19 at 17:53
  • aha, looks promising – dagda1 May 04 '19 at 17:54
  • 1
    `TS_NODE_FILES` worked for me; I put it right in a script command in `package.json`: `"scripts": { "test": "TS_NODE_FILES=true mocha -r ts-node/register tests/**/*-test.ts -r ts-node/register tests/*-test.ts"}` – CONTRACT SAYS I'M RIGHT Jul 09 '19 at 18:07
  • This worked for me: `./node_modules/.bin/mocha --require ts-node/register --require tsconfig-paths/register --preserve-symlinks --max-old-space-size=4000 wiski-libs/types/global.d.ts` – The Dembinski Apr 13 '20 at 05:50
  • Note: instead of setting the environment variable in the "scripts" section of package.json, you might prefer to set `process.env.TS_NODE_FILES = true;` in your file .mocharc.js. – Sylvain Lesage Jun 03 '20 at 16:35