Búsqueda de sitios web

typescript - mocha y ts-node UnhandledPromiseRejectionWarning: TypeError: No se puede convertir indefinido o nulo a objeto


Estoy tratando de configurar ts-node con mocha, sin embargo, el script de prueba siempre falla.

He intentado

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

y

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

También intenté instalar ts-node local y globalmente, pero siempre este es el resultado

> [email  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.

y este es mi tsconfig.json

{
    "compilerOptions": {
        "target": "es6" ,
        "module": "commonjs",
        "sourceMap": true ,
        "outDir": "dist" ,
        "strict": true ,
        "moduleResolution": "node" ,
        "esModuleInterop": true 
        },
    "lib": ["es2015"]
}

Respuesta #1

Después de ver este ejemplo https://github.com/mochajs/mocha-examples/tree/master/packages/typescript. El problema era que tenía TypeScript instalado globalmente. Esto no debería haber sido un problema, pero lo fue.

Respuesta #2

Instalar el módulo de TypeScript como una dependencia del proyecto lo resolvió para mí. Ejecuta el comando

npm install typescript

Artículos relacionados