[英]Jest TestEnvironment - TypeError: Cannot add property next, object is not extensible
I want to test a node API using Jest.我想使用 Jest 测试节点 API。 I am testing the routes and websockets.
我正在测试路由和 websockets。 Testing the routes was no problem.
测试路线没问题。 I simply started the server using the
setupFile
option.我只是使用
setupFile
选项启动了服务器。
To test the websockets I wanted to pass the io object to the tests.为了测试 websockets,我想将 io object 传递给测试。 This is not possible through the setupFile since the tests are running in their own context.
这通过 setupFile 是不可能的,因为测试是在它们自己的上下文中运行的。 Thus I changed to the
testEnvironment
option.因此,我更改为
testEnvironment
选项。 My testEnvironment file is the following我的测试环境文件如下
const NodeEnvironment = require('jest-environment-node');
class CustomEnvironment extends NodeEnvironment {
constructor(config, context) {
super(config, context);
this.setupServer();
}
async setup() {
await super.setup();
console.log('Setup Test Environment.');
this.global.io = this.io;
this.global.baseUrl = 'http://localhost:' + this.port;
}
async teardown() {
await super.teardown();
console.log('Teardown Test Environment.');
}
getVmContext() {
return super.getVmContext();
}
setupServer() {
// Code for starting the server and attaching the io object
this.port = portConfig.http;
this.io = io;
}
}
module.exports = CustomEnvironment;
This works and the io object is passed to the test.这有效,并且 io object 已通过测试。 I have multiple test files for different parts of the API. Running those with the
setupFile
was no problem but now Jest is only able to run one file.我有多个测试文件用于 API 的不同部分。使用
setupFile
运行这些文件没有问题,但现在 Jest 只能运行一个文件。 All following test suites are failing with the following message以下所有测试套件均失败并显示以下消息
● Test suite failed to run
TypeError: Cannot add property next, object is not extensible
at Function.handle (node_modules/express/lib/router/index.js:160:12)
at Function.handle (node_modules/express/lib/application.js:174:10)
at new app (node_modules/express/lib/express.js:39:9)
I am not able to find any documentation on that error.我找不到关于该错误的任何文档。 I tried disabling some of the test files but it always fails after the first one, no matter which one it is.
我尝试禁用一些测试文件,但它总是在第一个之后失败,无论它是哪个。
The structure of the test files is the following if relevant:如果相关,测试文件的结构如下:
const axios = require('axios');
describe('Test MODULE routes', () => {
const baseUrl = global.baseUrl;
const io = global.io;
const models = require('../../../models'); // sequelize models which are used in tests
describe('HTTP METHOD + ROUTE', () => {
test('ROUTE DESCRIPTION', async () => {
const response = await axios({
method: 'get',
url: baseUrl + 'ROUTE'
});
expect(response.status).toBe(200);
});
});
// different routes
});
I fixed the error.我修复了错误。 It had nothing to do with jest but with an
module.exports
invocation in the server setup which overwrote the export of the CustomEnvironment
with an express server.它与玩笑无关,而是与服务器设置中的
module.exports
调用有关,它用快速服务器覆盖了CustomEnvironment
的导出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.