简体   繁体   中英

How do I run a test file using ts-node?

Being fairly new to node and ts-node, I'm struggling to figure out how to run tests from this open source project -- https://github.com/pocesar/node-stratum (which are .ts files). I installed both "typescript" and "ts-node" ...

localhost:node-stratum satishp$ npm install -g typescript
/usr/local/bin/tsc -> /usr/local/lib/node_modules/typescript/bin/tsc
/usr/local/bin/tsserver -> /usr/local/lib/node_modules/typescript/bin/tsserver
+ typescript@2.7.2
added 1 package in 2.974s


localhost:node-stratum satishp$ npm install -g ts-node
/usr/local/bin/ts-node -> /usr/local/lib/node_modules/ts-node/dist/bin.js
+ ts-node@5.0.1
added 17 packages in 1.596s

but when I go to run the tests, located in the "test/tests.ts" directory, I get teh following error

localhost:node-stratum satishp$ ts-node test/tests.ts
Error: Cannot find module '../lib'
    at Function.Module._resolveFilename (module.js:555:15)
    at Function.Module._load (module.js:482:25)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/satishp/Documents/workspace/node-stratum/test/tests.ts:3:1)
    at Module._compile (module.js:660:30)
    at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:400:23)
    at Module._extensions..js (module.js:671:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/usr/local/lib/node_modules/ts-node/src/index.ts:403:12)
    at Module.load (module.js:573:32)

Is there something more I need to do to get the test file to run properly?

After a small bit of research, here's what it looks like you should do:

  • Clone the repo (I assume you've already done this, only here for completeness' sake)
  • Run npm install in the cloned folder. This way you'll have all of the project's specific package versions.
  • Run tsc to compile the source files into the lib folder. Ignore the type errors; the code should still compile.
  • Run tsc -p tests.json to compile the test files, which uses the tests.json file as a tsconfig. Again, ignore the type errors.
  • Run mocha test to actually run the tests on the compiled files.

For a faster workflow, run tsc -w and tsc -p tests.json -w in separate command line instances so that the files will be compiled automatically.

Then in a third instance, you can run mocha test -w to run tests on compilation. However, one of the errors running the tests makes mocha fail, so you'll have to fix that before being able to run mocha in watch mode.

It doesn't help that the project hasn't taken the time to fix the many type errors, nor provide any proper documentation on running the tests, so I'd suggest making an issue on GitHub so the authors can take notice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM