【问题标题】:NRWL NX - Why do I get "Cannot find name 'describe'" when running "ng serve express"?NRWL NX - 为什么我在运行“ng serve express”时得到“找不到名称‘describe’”?
【发布时间】:2020-11-28 04:39:04
【问题描述】:

我创建了一个普通的 @nrwl/express 项目,其中包含一个空的 Web 服务器 server.ts

const express = require('express')
const app = express()

app.get('/', (req, res) => {
    res.json({status: 'OK'})
});

var server = app.listen(3000);

module.exports = { server }

每当我添加一个测试文件时,例如something.test.ts

const { server } = require('../server');

describe('TEST: /', () => {
  it('Should work just fine', async () => {
    // all ok
  });
});

export {};

然后ng serve express 开始抱怨,因为它试图处理测试文件:

TS2593: Cannot find name 'describe'. Do you need to install type definitions for a test runner? 
Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types
field in your tsconfig.

解决这个问题的正确方法是什么?我不一定想将测试库添加为运行时依赖项,因为不应将测试文件捆绑在构建 IMO 中。

【问题讨论】:

  • 它们不会捆绑在构建中。您是否尝试过它所说的并安装了类型?这些只是让 typescript 知道它们是什么的 d.ts 文件。
  • 我尝试按照上面的建议执行npm i @types/jest,但验证了tsconfigtypes 字段中已经存在“jest”。考虑到 Jest 自动出现在 Nrwl NX 中,我认为应该有更好的方法来做到这一点。我不想相信他们会忘记添加这个重要的依赖项。

标签: node.js typescript express nrwl-nx


【解决方案1】:

几天后我想通了(掌心)

我需要编辑tsconfig.app.json 并需要在排除选项中添加"**/*.test.ts"

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../../dist/out-tsc",
    "types": ["node"]
  },
  "exclude": ["**/*.spec.ts", "**/*.test.ts"],
  "include": ["**/*.ts"]
}

现在一切正常,警告消息消失了。

【讨论】:

    猜你喜欢
    • 2020-01-03
    • 2017-08-24
    • 2021-08-10
    • 2019-10-05
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    相关资源
    最近更新 更多