【发布时间】:2020-07-31 13:23:20
【问题描述】:
我有以下课程:
import http, { RequestListener } from 'http';
import { Application } from './application';
export class WebServer {
private httpServer: http.Server;
private requestListener: RequestListener;
public async start(application: Application, port: number) {
this.requestListener = application.getRequestListener();
this.httpServer = http.createServer(this.requestListener);
await this.httpServer.listen(port);
}
public async stop() {
await this.httpServer.close();
}
public getHttpServer(): http.Server {
return this.httpServer;
}
public getRequestListener(): RequestListener {
return this.requestListener;
}
}
我有以下简单的测试用例:
import { expect } from 'chai';
import 'mocha';
import { Application } from './../infrastructure';
import { WebServer } from './../infrastructure';
describe('WebServer Tests', () => {
const webServer = new WebServer();
const webServerProto = Object.getPrototypeOf(webServer)
it('Checking WebServer Initialization', () => {
expect(webServerProto).to.not.be.null;
expect(webServerProto.httpServer).to.not.be.null;
expect(webServerProto.requestListener).to.not.be.null;
});
我在运行测试时遇到导入错误。即,我得到以下信息:
TypeError:"uncaughtException": WebServer is not a constructor
这让我相信我的测试文件中的导入有问题。作为参考,这是我的启动脚本和测试脚本:
"start": "node --inspect=5858 -r ts-node/register ./src/index.ts"
"test": "cross-env TS_NODE_FILES=true mocha --exit --require ts-node/register --watch-extensions ts --colors ./src/test/*-tests.ts",
有人知道我需要对我的测试环境做什么才能访问 WebServer 类吗? npm start 工作得很好。运行 npm test 时出现构造函数错误。任何建议将不胜感激!
【问题讨论】:
-
你能有一些循环依赖,例如。在调用 new 之前尝试
console.log(WebServer)..
标签: javascript typescript mocha.js chai