question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing with mocha and defining project with TS_NODE_PROJECT fails

See original GitHub issue

Hi

I have a project, a library, of small convenience methods for JS and I use Mocha for my unit testing with the following command:

cross-env TS_NODE_PROJECT=./test nyc mocha --compilers ts:ts-node/register ./test/*.spec.ts

Which has worked well until I updated to ts-node version 5.0.1.

Now it it has started failing with the error message that it cannot find my test folder.

So I wanted to know if you could tell me why it is failing?

Note
I use NYC (Istanbul) for code coverage, but the compilation fails even if I remove that from the command.

My Library set up

MyLibrary
|- test
|  |- <*.spec.ts>
|  |- tsconfig.json
|- <*.ts>
|- tsconfig.json

All my main files are in the root of the project and all the test files are in a test folder.

As I am compiling the tests a bit differently than the main files I have added a tsconfig.json in the root of the test folder and I am (trying to) instructing the TS compiler to use the test folder tsconfig.json for the compilation.

test/tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitAny": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "sourceMap": true,
    "target": "es2017",
    "lib": ["es5", "es6", "dom"],
    "moduleResolution": "node",
    "allowJs": false,
    "outDir": "."
  },

  "include": [
    "../*.ts"
  ],

  "exclude": [
    "node_modules",
    "../test/**/*.ts"
  ]
}

Error Message thrown

C:\projects\MyLibrary\node_modules\ts-node\src\index.ts:453
        throw new TSError([formatDiagnostic(result.error, cwd, ts, 0)])
              ^
TSError: Γ¿» Unable to compile TypeScript
The specified path does not exist: 'C:\projects\MyLibrary\test'. (5058)
    at readConfig (C:\projects\MyLibrary\node_modules\ts-node\src\index.ts:453:15)
    at Object.register (C:\projects\MyLibrary\node_modules\ts-node\src\index.ts:209:18)
    at Object.<anonymous> (C:\projects\MyLibrary\node_modules\ts-node\register\index.js:1:16)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at program.compilers.forEach.c (C:\projects\MyLibrary\node_modules\mocha\bin\_mocha:459:3)
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (C:\projects\MyLibrary\node_modules\mocha\bin\_mocha:451:19)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
duvet86commented, Apr 30, 2018

You are definitely right, removing the space fixes it. SET TS_NODE_PROJECT=./test/tsconfig.json&& mocha -r ts-node/register -r tsconfig-paths/register test/**/*.ts

Last question why setting the inline option doesn’t work? mocha -r ts-node/register --project /test/tsconfig.json -r tsconfig-paths/register test/**/*.ts

0reactions
blakeembreycommented, Apr 30, 2018

Because --project is not a mocha argument.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overriding `tsconfig.json` for ts-node in mocha - Stack Overflow
"test": "TS_NODE_PROJECT=src mocha". picks up my tsconfig.json in the src directory of my project, overriding the default tsconfig.json.
Read more >
How To Test a Node.js Module with Mocha and Assert
First, we need to set up the coding environment. Create a folder with the name of your project in your terminal. This tutorial...
Read more >
ts-mocha - npm
Mocha thin wrapper that allows running TypeScript tests with TypeScript runtime (ts-node) to get rid of compilation complexity.
Read more >
Testing Node.js with Mocha and Chai - LogRocket Blog
We'll cover the following in detail: What is Mocha? Is Mocha.js a BDD tool? Running a Mocha test in Node.js; Writing tests with...
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
If you use callback-based async tests, Mocha will throw an error if done() is called ... Hooks will run in the order they...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found