Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mocha and ts-node UnhandledPromiseRejectionWarning:TypeError: Cannot convert undefined or null to object

Tags:

I am trying to setup ts-node with mocha however the test script always fails.

I have tried

mocha --require ts-node/register --extensions ts,tsx --watch --watch-files src 'src/**/*.spec.{ts,tsx}'

and

mocha --require ts-node/register src/**/*.spec.ts

Also I tried installing ts-node locally and globally but always this is the output

> [email protected] test /home/moamen/foo/baz
> mocha --require ts-node/register src/**/*.spec.ts

(node:48106) UnhandledPromiseRejectionWarning: TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at Object.help (/home/moamen/foo/baz/node_modules/yargs/lib/usage.js:240:22)
    at Object.self.showHelp (/home/moamen/foo/baz/node_modules/yargs/lib/usage.js:432:15)
    at Array.<anonymous> (/home/moamen/foo/baz/node_modules/mocha/lib/cli/cli.js:53:13)
    at Object.fail (/home/moamen/foo/baz/node_modules/yargs/lib/usage.js:41:17)
    at /home/moamen/foo/baz/node_modules/yargs/lib/command.js:246:36
(Use `node --trace-warnings ...` to show where the warning was created)
(node:48106) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:48106) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

and this is my tsconfig.json

{
    "compilerOptions": {
        "target": "es6" ,
        "module": "commonjs",
        "sourceMap": true ,
        "outDir": "dist" ,
        "strict": true ,
        "moduleResolution": "node" ,
        "esModuleInterop": true 
        },
    "lib": ["es2015"]
}
like image 286
Moamen Mohamed Avatar asked May 29 '20 05:05

Moamen Mohamed


1 Answers

After seeing this example https://github.com/mochajs/mocha-examples/tree/master/packages/typescript. The problem was that I had typescript installed globally. This shouldn't have been a problem but it was.

like image 178
Moamen Mohamed Avatar answered Oct 05 '22 18:10

Moamen Mohamed