【问题标题】:Can't test class containing import nestJS无法测试包含 import nestJS 的类
【发布时间】:2021-07-26 16:10:16
【问题描述】:

我正在尝试为我在 nodeJS 中实现的服务构建单元测试。 我知道我不能在我的测试文件中使用 import,所以我只在我的测试文件中使用了 require:

const typeorm = require("typeorm");
const DeviceService = require("./device.service.ts");
const Device = require("./entities/device.entity.ts");
const CreateDeviceDto = require("./dto/create-device.ts");

问题是我的 deviceService 中有一个导入。

当我运行测试时,我的文件 device.service.ts 出现错误:

import { Injectable } from '@nestjs/common';
       ^

SyntaxError: Unexpected token {

有关信息,我尝试在我的测试中使用这样的存根创建我的服务:

const deviceRepository = typeorm.getRepository(Device);

const stub = sinon.stub(deviceRepository, "create").returns(stubValue);
const DeviceServiceStubbed = new DeviceService(deviceRepository);

使用的堆栈:nestJs / Chai / Sinon / Typeorm

谢谢!

【问题讨论】:

    标签: mocha.js nestjs sinon typeorm


    【解决方案1】:

    问题是我的 mocha 调用是错误的。 我将 "test": "mocha" 更改为"test": "mocha --require ts-node/register 在我的 package.json 中,现在它工作正常

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 2017-07-24
      • 2014-08-07
      • 2023-03-12
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      相关资源
      最近更新 更多