Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 超时-在jest.setTimeout指定的5000ms超时内未调用异步回调_Reactjs_Express_Jestjs_Supertest - Fatal编程技术网

Reactjs 超时-在jest.setTimeout指定的5000ms超时内未调用异步回调

Reactjs 超时-在jest.setTimeout指定的5000ms超时内未调用异步回调,reactjs,express,jestjs,supertest,Reactjs,Express,Jestjs,Supertest,我正在使用jest测试API的用户功能(注册和登录) 测试代码: const request = require('supertest'); const app = require('../../app'); describe('Test User Functionality', () => { test('User should be able to login', async done => { const response = await request(ap

我正在使用jest测试API的用户功能(注册和登录)

测试代码:

const request = require('supertest');
const app = require('../../app');

describe('Test User Functionality', () => {  
  test('User should be able to login', async done => {
    const response = await request(app)
      .post('/api/users/login')
      .send({
        email: 'test@test.com',
        password: 'welcome1',
      })
      .expect(200);
    done();
    //expect(response.statusCode).toBe(200);
  });
  test('User should be able to signup', async done => {
    const response = await request(app)
      .post('/api/users/signup')
      .send({
        username: 'testuser',
        email: 'test@test1.com',
        password: 'welcome1',
      })
      .expect(200);
    done();
    //expect(response.statusCode).toBe(200);
  });
});
在我有一个测试的情况下,它工作得很好,但有多个测试在里面,它显示超时错误

以下是错误的屏幕截图:

我尝试添加超时,交换测试,但仍然没有成功


任何人都请帮忙

在使用异步请求测试react组件时,我遇到了相同的问题

发生这种情况是因为您没有正确完成请求

你可以很容易地解决这个问题

选项1:移动
done
作为
expect
函数调用的第二个参数,如下所示

const response = await request(app)
  .post('/api/users/signup')
  .send({
    username: 'testuser',
    email: 'test@test1.com',
    password: 'welcome1',
  })
  .expect(200, done);
选项2:使用
end
方法

const response = await request(app)
      .post('/api/users/signup')
      .send({
        username: 'testuser',
        email: 'test@test1.com',
        password: 'welcome1',
      })
      .expect(200)
      .end((err, res) => { 
        // do anything you want! 
      })

或者您可以签出文档()

一个可能的问题可能是express Middleware。 要了解这是否是您的情况,您可以:

  • 注释掉express应用程序(
    应用程序)的所有中间件。使用(/*中间件*/)
  • 查看测试是否以某种方式开始进行(通过/未超时)
  • 开始取消对中间产品的注释,将其缩小到罪犯
  • 一旦你找到了超时的原因,你就可以更深入了。 模拟导致问题的中间件的不同部分:

  • 通过在中创建目录来模拟第三方库 在根目录中插入一个文件
    library\u name.js
    (这是 为开玩笑而手动模拟: )
  • 模拟中间件,让它们直接调用next
    app.use(Your.Middleware)
    并在中间件文件中传递
  • (包含中间件的文件可能包含一些可能导致问题的附加设置。)

    我的具体情况是: 我在中间件中使用了
    ioredis
    ,在Redis实例化的文件中,它多次尝试连接到商店,导致超时。

    事实证明,主机名是错误的。泄露问题的关键是模仿中间件并找到额外的设置。然后Jest显示了另一个提示商店连接问题的错误。

    您解决过这个问题吗?
    /** Possibly imports and some setup here */
    
    // Make sure the function being called is mocked
    export default {
        Middleware: (req, res, next) => {
            next();
        },
    }