简体   繁体   English

Jest TestEnvironment - 类型错误:接下来无法添加属性,object 不可扩展

[英]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.

相关问题 TypeError:无法添加属性 0,object 不可扩展 - TypeError: Cannot add property 0, object is not extensible 未捕获的类型错误:无法添加属性 0,object 在 Array.push 不可扩展 - Uncaught TypeError: Cannot add property 0, object is not extensible at Array.push React App-TypeError:无法添加属性setState,对象不可扩展 - React App - TypeError: Cannot add property setState, object is not extensible 材料表类型错误:无法添加属性表数据,对象不可扩展 - Material-table TypeError: Cannot add property tableData, object is not extensible 无法添加属性“X”,对象不可扩展 angular 9 - Cannot add property "X", object is not extensible angular 9 React:无法添加属性“X”,对象不可扩展 - React : cannot add property 'X', object is not extensible 未捕获的TypeError:无法添加属性12,对象不可扩展 - Uncaught TypeError: Can't add property 12, object is not extensible 无法在Jest中创建自定义TestEnvironment - Cannot create custom TestEnvironment in Jest React Error(TypeError):无法添加属性上下文,对象不可扩展 - React Error (TypeError): Can't add property context, object is not extensible 为什么我在 redux-slice 中收到错误消息“Uncaught (in promise) TypeError: Cannot add property bookId, object is not extensible”? - Why I am getting an error as "Uncaught (in promise) TypeError: Cannot add property bookId, object is not extensible" into my redux-slice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM