测试失败 - ProductService 不是构造函数

Test failed - ProductService is not a constructor

我正在使用 "chai": "^4.2.0","mocha": "^4.0.1"。我是 运行 node --version, v10.15.3 我的目标是测试服务层:

我的 ProductService.js 如下所示:

class ProductService {

    constructor() {
        // constructor
    }

    async createOrUpdateProduct(dataArray) {
        return "done"
    }
}

module.exports = {
    ProductService
};

我的测试 class ProductTestService.js 如下所示:

const assert = require('chai').assert;

const ProductService = require('../Service/ProductService')


describe('Product model', () => {

    it('should add the test data with the Products Service to the Product table', async () => {
        let dataArr = "product data"
        let productServ = new ProductService()

        const res = await productServ.createOrUpdateProduct(dataArr)
        assert.isOk(res.length, dataArr.length);
    });

});

当 运行 测试时,我得到:

对于实例化不起作用的任何建议?

感谢您的回复!

密码

module.exports = {
    ProductService
};

是shorthand为

module.exports = {
    ProductService: ProductService
};

这意味着,当您使用

导入模块时
const ProductService = require('../Service/ProductService');

ProductService 的值正是您导出的值,即具有 属性 ProductService 的对象。

{
    ProductService: ProductService
}

要解决您的问题,请直接导出 class,如果这是您想要从模块中导出的唯一内容

module.exports = ProductService;

或者使用对象解构导入,如果你还想导出其他东西

const { ProductService } = require('../Service/ProductService');