使用 typescript/mocha 进行单元测试时找不到模块
Posted
技术标签:
【中文标题】使用 typescript/mocha 进行单元测试时找不到模块【英文标题】:Module not found when doing unit test with typescrit/mocha 【发布时间】:2018-05-01 13:38:00 【问题描述】:我对 Typescript 还很陌生,并试图按照本教程建立一个项目:http://mherman.org/blog/2016/11/05/developing-a-restful-api-with-node-and-typescript/
我正在创建一个名为 helloworld.test.ts 的文件并运行 npm test 以查看 api 的基本路由是否正常工作。
但是,当我运行 npm test 命令(执行以下操作: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.
test/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) 克隆 repo 时,我遇到了同样的问题。
我认为 npm 包已正确安装,因为当我运行 npm start 时,api 正常工作。
我不确定是否应该在这篇文章中添加一些文件,因为我的文件与 repo 中的文件基本相同。
我使用的是 Ubuntu 16.04,我的节点版本是 7.0.0,我的 npm 版本是 3.10.10,教程使用 typescript 2。
【问题讨论】:
【参考方案1】:我可以复制。 ts-node
已过时
npm install ts-node@^3 --dev
会解决的
【讨论】:
另外,我发布了一个拉取请求:github.com/mjhea0/typescript-node-api/pull/12 如果可以的话我会投票,但我需要更多的声望点以上是关于使用 typescript/mocha 进行单元测试时找不到模块的主要内容,如果未能解决你的问题,请参考以下文章