node.js - 使用 typescript/mocha 进行单元测试时找不到模块

标签 node.js typescript mocha.js

我对 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,我的 Node 版本是 7.0.0,我的 npm 版本是 3.10.10,教程使用 typescript 2。

最佳答案

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

npm install ts-node@^3 --dev

会修复它

关于node.js - 使用 typescript/mocha 进行单元测试时找不到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47356367/

相关文章:

Angular 网络包 : Failed to compile

javascript - 在 Node 下集成编译为javascript语言的正确方法?

javascript - Mocha Async before() Hook 超时

javascript - break + return vs 返回 Javascript Nodejs

node.js - 使用 Nightwatch.js 页面对象

javascript - 为什么函数变得未定义?

node.js - 记录 Mocha 测试错误的响应正文?

node.js - connect.session 和 connect.cookieParser 的 secret 之间的区别?

javascript - Karma 是否会针对 Angular 和 Jasmine 的 future 版本进行更新?

typescript - 类中的枚举(TypeScript 定义文件)