简体   繁体   English

类型错误:无法使用 JEST 单元测试读取 NodeJS 中未定义的属性“应用”

[英]TypeError: Cannot read property 'apply' of undefined in NodeJS using JEST unit testing

I am new to jest unit testing as well as NodeJS and i'm trying to test my API using the following code.我是 jest 单元测试以及 NodeJS 的新手,我正在尝试使用以下代码测试我的 API。

const request = require('supertest');
const quick_registration = require('../../../routes/register/quick_registration_of_user')

describe('Quick Registration of users', () => {
    beforeAll(() => {
        con.connect();
    });

    describe("GET / ", () => {
        test("It should respond with an array of students", async () => {
          const response = await request(quick_registration).get("/api/quickregistration");
          expect(response.status).toBe(200);
        });
      });
})

But the test keep failing and throws me an error但是测试一直失败并给我一个错误

 FAIL  tests/routes/register/quick_registration_of_user.test.js
  Quick Registration of users
    GET /
      × It should respond with an array of students (20ms)

  ● Quick Registration of users › GET /  › It should respond with an array of students

    TypeError: Cannot read property 'apply' of undefined

      at node_modules/express/lib/router/index.js:635:15
      at next (node_modules/express/lib/router/index.js:260:14)
      at Function.handle (node_modules/express/lib/router/index.js:174:3)
      at Server.router (node_modules/express/lib/router/index.js:47:12)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        1.649s, estimated 2s
Ran all test suites.
Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

I'm assuming the line我假设这条线

const quick_registration = require('../../../routes/register/quick_registration_of_user')

is requiring the file where you've defined your router.需要您定义路由器的文件。 You need to be requiring instead the file where you define your app.您需要改为要求您定义应用程序的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 摩卡单元测试:未捕获的类型错误:无法读取未定义的属性“应用” - Mocha Unit Testing: Uncaught TypeError: Cannot read property 'apply' of undefined 类型错误:无法读取未定义 + Jest + NodeJS 的属性“原型” - TypeError: Cannot read property 'prototype' of undefined + Jest + NodeJS 开玩笑与fetch-mock生成错误:TypeError:在nodejs上使用时无法读取未定义的属性'prototype' - Jest with fetch-mock generating error: TypeError: Cannot read property 'prototype' of undefined when using on nodejs Jest TypeError:无法读取未定义的属性“id” - Jest TypeError: Cannot read property 'id' of undefined JEST TypeError:无法读取未定义的属性“json” - JEST TypeError: Cannot read property 'json' of undefined Express.js-TypeError:无法读取未定义的属性“ apply” - Expressjs - TypeError: Cannot read property 'apply' of undefined 异步 - 类型错误:无法读取未定义的属性“应用” - async - TypeError: Cannot read property 'apply' of undefined 类型错误:无法读取未定义的属性“应用”? - TypeError: Cannot read property 'apply' of undefined? “TypeError:无法读取属性'apply'of undefined” - “TypeError: Cannot read property 'apply' of undefined” 类型错误:无法在 EventEmitter 读取未定义的属性“应用” - TypeError: Cannot read property 'apply' of undefined at EventEmitter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM