使用typescrit / mocha进行单元测试时找不到模块

时间:2017-11-17 17:38:46

标签: node.js typescript mocha

我对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)克隆回购时,我遇到了同样的问题。

我认为npm软件包已正确安装,因为当我运行npm start时,api正常工作。

我不确定我是否应该在这篇文章中添加一些文件,因为我的文件基本上和回购邮件中的文件几乎相同。

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

1 个答案:

答案 0 :(得分:1)

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

npm install ts-node@^3 --dev

会解决它

相关问题