[英]How do I run a test file using ts-node?
作為node和ts-node的新手,我正在努力找出如何從這個開源項目中運行測試-https: //github.com/pocesar/node-stratum (它們是.ts文件)。 我同時安裝了“打字稿”和“ 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
但是當我運行位於“ test / tests.ts”目錄中的測試時,出現以下錯誤
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)
我還需要做些什么才能使測試文件正常運行?
經過一番研究,您應該執行以下操作:
npm install
。 這樣,您將擁有項目的所有特定軟件包版本。 tsc
將源文件編譯到lib文件夾中。 忽略類型錯誤; 該代碼仍應編譯。 tsc -p tests.json
編譯測試文件,該文件將tests.json
文件用作tsconfig。 同樣,忽略類型錯誤。 mocha test
以在編譯后的文件上實際運行測試。 為了加快工作流程,請在單獨的命令行實例中運行tsc -w
和tsc -p tests.json -w
,以便自動編譯文件。
然后在第三個實例中,您可以運行mocha test -w
在編譯時運行測試。 但是,運行測試的錯誤之一會導致摩卡失敗,因此您必須先解決該問題,然后才能在監視模式下運行摩卡。
該項目沒有花時間修復許多類型錯誤,也沒有提供有關運行測試的任何適當文檔,這無濟於事,所以我建議在GitHub上發布一個問題,以便作者注意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.