使用typecrit / mocha进行unit testing时找不到模块

对于Typescript,我是一个相当新的东西,并试图在本教程之后设置一个项目: http ://mherman.org/blog/2016/11/05/developing-a-restful-api-with-node-and-typescript/

我正在创build一个名为helloworld.test.ts的文件,并运行npm test来查看api的基本路线是否正常工作。

但是,当我运行npmtesting命令(哪些做: mocha --reporter spec --compilers ts:ts-node/register 'test/**/*.test.ts' )我得到以下错误:

 /home/louis/Bureau/my-simple-project/node_modules/ts-node/src/index.ts:312 throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset)) ^ TSError: ⨯ Unable to compile TypeScript Cannot find type definition file for 'body-parser'. (2688) Cannot find type definition file for 'chai'. (2688) Cannot find type definition file for 'chai-http'. (2688) Cannot find type definition file for 'debug'. (2688) Cannot find type definition file for 'express'. (2688) Cannot find type definition file for 'express-serve-static-core'. (2688) Cannot find type definition file for 'mime'. (2688) Cannot find type definition file for 'mocha'. (2688) Cannot find type definition file for 'morgan'. (2688) Cannot find type definition file for 'node'. (2688) Cannot find type definition file for 'serve-static'. (2688) test/helloworld.test.ts (1,24): Cannot find module 'mocha'. (2307) test/helloworld.test.ts (2,23): Cannot find module 'chai'. (2307) test/helloworld.test.ts (3,27): Cannot find module 'chai-http'. (2307) test/helloworld.test.ts (5,17): Cannot find module '../src/App'. (2307) test/helloworld.test.ts (10,1): Cannot find name 'describe'. (2304) test/helloworld.test.ts (12,3): Cannot find name 'it'. (2304) test/helloworld.test.ts (19,3): Cannot find name 'it'. (2304) at getOutput (/home/louis/Bureau/my-simple-project/node_modules/ts-node/src/index.ts:312:17) at /home/louis/Bureau/my-simple-project/node_modules/ts-node/src/index.ts:343:18 at Object.compile (/home/louis/Bureau/my-simple-project/node_modules/ts-node/src/index.ts:476:19) at Module.m._compile (/home/louis/Bureau/my-simple-project/node_modules/ts-node/src/index.ts:406:44) at Module._extensions..js (module.js:582:10) at Object.require.extensions.(anonymous function) [as .ts] (/home/louis/Bureau/my-simple-project/node_modules/ts-node/src/index.ts:409:12) at Module.load (module.js:490:32) at tryModuleLoad (module.js:449:12) at Function.Module._load (module.js:441:3) at Module.require (module.js:500:17) at require (internal/module.js:20:19) at /home/louis/Bureau/my-simple-project/node_modules/mocha/lib/mocha.js:231:27 at Array.forEach (native) at Mocha.loadFiles (/home/louis/Bureau/my-simple-project/node_modules/mocha/lib/mocha.js:228:14) at Mocha.run (/home/louis/Bureau/my-simple-project/node_modules/mocha/lib/mocha.js:514:10) at Object.<anonymous> (/home/louis/Bureau/my-simple-project/node_modules/mocha/bin/_mocha:480:18) at Module._compile (module.js:573:32) at Object.Module._extensions..js (module.js:582:10) at Module.load (module.js:490:32) at tryModuleLoad (module.js:449:12) at Function.Module._load (module.js:441:3) at Module.runMain (module.js:607:10) at run (bootstrap_node.js:382:7) at startup (bootstrap_node.js:137:9) at bootstrap_node.js:497:3 npm ERR! Test failed. See above for more details. 

testing内容/ helloworld.test.ts:

 import * as mocha from 'mocha'; import * as chai from 'chai'; import chaiHttp = require('chai-http'); import app from '../src/App'; chai.use(chaiHttp); const expect = chai.expect; describe('baseRoute', () => { it('should be json', () => { return chai.request(app).get('/') .then(res => { expect(res.type).to.eql('application/json'); }); }); it('should have a message prop', () => { return chai.request(app).get('/') .then(res => { expect(res.body.message).to.eql('Hello World!'); }); }); }); 

tsconfig.json的内容:

 { "compilerOptions": { "target": "es6", "module": "commonjs", "outDir": "dist", "removeComments": true, "preserveConstEnums": true, "sourceMap": true }, "include": [ "src/**/*.ts" ], "exclude": [ "node_modules" ] } 

package.json的内容:

 { "name": "simple-project", "version": "1.0.0", "description": "description", "main": "index.js", "scripts": { "start": "node dist/index.js", "build": "gulp scripts", "test": "mocha --reporter spec --compilers ts:ts-node/register 'test/**/*.test.ts'" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "@types/body-parser": "0.0.33", "@types/chai": "^3.4.34", "@types/chai-http": "0.0.29", "@types/debug": "0.0.29", "@types/express": "^4.0.33", "@types/mocha": "^2.2.32", "@types/morgan": "^1.7.32", "@types/node": "^6.0.46", "chai": "^3.5.0", "chai-http": "^3.0.0", "gulp": "^3.9.1", "gulp-sourcemaps": "^2.6.1", "gulp-typescript": "^3.1.1", "merge-stream": "^1.0.1", "mocha": "^3.1.2", "ts-node": "^1.6.1", "typescript": "^2.0.6" }, "dependencies": { "body-parser": "^1.15.2", "debug": "^2.2.0", "express": "^4.14.0", "morgan": "^1.7.0" } } 

克隆从教程( https://github.com/mjhea0/typescript-node-api )回购时,我有同样的问题。

我认为npm包是正确安装,因为当我运行npm开始api是正确的工作。

我不知道我应该添加一些文件到这个职位,因为我的文件基本上是几乎相同的回购。

我使用Ubuntu 16.04,我的节点版本是7.0.0,我的npm版本是3.10.10,教程使用打字稿2。

我可以重现。 ts-node已经过时

 npm install ts-node@^3 --dev 

将解决它