我想创建我的测试。我用 koa.js 和我的 server.ts 文件构建了一个服务器:
import Koa from "koa";
import jwt from "koa-jwt";
import bodyParser from "koa-bodyparser";
...
...
const app = new Koa();
app.use(helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'", "cdnjs.cloudflare.com"],
styleSrc: ["'self'", "'unsafe-inline'", "cdnjs.cloudflare.com", "fonts.googleapis.com"],
fontSrc: ["'self'", "fonts.gstatic.com"],
imgSrc: ["'self'", "data:", "online.swagger.io", "validator.swagger.io"]
}
}));
app.use(cors());
app.use(logger(winston));
app.use(bodyParser());
app.use(unprotectedRouter.routes()).use(unprotectedRouter.allowedMethods());
app.use(jwt({ secret: config.jwtSecret }).unless({ path: [/^\/swagger-/] }));
app.use(protectedRouter.routes()).use(protectedRouter.allowedMethods());
cron.start();
app.listen(config.port, () => {
console.log(`Server running on port ${config.port}`);
});
export default app;
我的 test.ts 文件是这样的:
import request from 'supertest';
import app from '../../src/server';
describe('basic route tests', () => {
test('get home route GET /', async () => {
const response = await request(app).get('/');
expect(response.status).toEqual(200);
expect(response.text).toContain('Hello World!');
});
});
所以,我收到一个错误:
为什么我收到这个错误?我认为问题发生在导出应用程序时。 我该如何解决这个错误?