Javascript Jest Mocked request.js超时-在Jest.setTimeout.Error指定的5000ms超时内未调用异步回调:

Javascript Jest Mocked request.js超时-在Jest.setTimeout.Error指定的5000ms超时内未调用异步回调:,javascript,unit-testing,request,jestjs,Javascript,Unit Testing,Request,Jestjs,我试图模拟request.js来模拟文件上传,但它总是挂起。我增加了等待时间,但它总是超时 我试图模拟的代码遵循request.post.form.append结构。如果我手动输入表单数据,它总是会失败,因此出于功能原因,无法修改功能代码 我将require.js mock设置为只适合一个用例,这就是为什么它是这样编写的。 我的require.js mock如下所示: const request = { post: jest.fn(() => ({ form: jest.fn(

我试图模拟request.js来模拟文件上传,但它总是挂起。我增加了等待时间,但它总是超时

我试图模拟的代码遵循
request.post.form.append
结构。如果我手动输入表单数据,它总是会失败,因此出于功能原因,无法修改功能代码

我将require.js mock设置为只适合一个用例,这就是为什么它是这样编写的。 我的require.js mock如下所示:

const request = {
  post: jest.fn(() => ({
    form: jest.fn(() => ({
      append: jest.fn(test => {
        console.log(test)
        return Promise.resolve({
          status: 200,
          body: { test }
        })
      })
    }))
  }))
};

module.exports = request;
    it('should upload a file', async () => {
      mocks.post.mockReturnValueOnce({
        status: 200,
        body: { test: 'response' }
      });

      const res = await dataSource.uploadFile(
        { name: 'projects', id: '123' },
        null,
        {
          filename: 'test',
          encoding: '7bit',
          mimetype: 'text/plain',
          createReadStream: jest.fn
        },
         '12345'
      );
      console.log('RES', res); // never gets here
      expect(mocks.post).toBeCalledWith('testName/123/files', {
        filename: 'test',
        encoding: '7bit',
        mimetype: 'text/plain',
        createReadStream: jest.fn
      });
      expect(res).toMatchSnapshot();
    });
  console.log folder/folderName/src/files/__mocks__/request.js:5
    file

  console.log folder/folderName/src/files/__mocks__/request.js:5
    file

 FAIL  folder/folderName/src/files/__tests__/upload.spec.js (12.673s)
    uploadFile
      ✕ should upload a file (5006ms)

  ● FilesAPI › uploadFile › should upload a file

    Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error: 
我的jest代码如下:

const request = {
  post: jest.fn(() => ({
    form: jest.fn(() => ({
      append: jest.fn(test => {
        console.log(test)
        return Promise.resolve({
          status: 200,
          body: { test }
        })
      })
    }))
  }))
};

module.exports = request;
    it('should upload a file', async () => {
      mocks.post.mockReturnValueOnce({
        status: 200,
        body: { test: 'response' }
      });

      const res = await dataSource.uploadFile(
        { name: 'projects', id: '123' },
        null,
        {
          filename: 'test',
          encoding: '7bit',
          mimetype: 'text/plain',
          createReadStream: jest.fn
        },
         '12345'
      );
      console.log('RES', res); // never gets here
      expect(mocks.post).toBeCalledWith('testName/123/files', {
        filename: 'test',
        encoding: '7bit',
        mimetype: 'text/plain',
        createReadStream: jest.fn
      });
      expect(res).toMatchSnapshot();
    });
  console.log folder/folderName/src/files/__mocks__/request.js:5
    file

  console.log folder/folderName/src/files/__mocks__/request.js:5
    file

 FAIL  folder/folderName/src/files/__tests__/upload.spec.js (12.673s)
    uploadFile
      ✕ should upload a file (5006ms)

  ● FilesAPI › uploadFile › should upload a file

    Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error: 
适用的测试报告如下:

const request = {
  post: jest.fn(() => ({
    form: jest.fn(() => ({
      append: jest.fn(test => {
        console.log(test)
        return Promise.resolve({
          status: 200,
          body: { test }
        })
      })
    }))
  }))
};

module.exports = request;
    it('should upload a file', async () => {
      mocks.post.mockReturnValueOnce({
        status: 200,
        body: { test: 'response' }
      });

      const res = await dataSource.uploadFile(
        { name: 'projects', id: '123' },
        null,
        {
          filename: 'test',
          encoding: '7bit',
          mimetype: 'text/plain',
          createReadStream: jest.fn
        },
         '12345'
      );
      console.log('RES', res); // never gets here
      expect(mocks.post).toBeCalledWith('testName/123/files', {
        filename: 'test',
        encoding: '7bit',
        mimetype: 'text/plain',
        createReadStream: jest.fn
      });
      expect(res).toMatchSnapshot();
    });
  console.log folder/folderName/src/files/__mocks__/request.js:5
    file

  console.log folder/folderName/src/files/__mocks__/request.js:5
    file

 FAIL  folder/folderName/src/files/__tests__/upload.spec.js (12.673s)
    uploadFile
      ✕ should upload a file (5006ms)

  ● FilesAPI › uploadFile › should upload a file

    Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error: 
我注意到它在我的请求mock中记录了两次
test
,所以我猜它正在等待其他东西正确返回。我错过了什么


它正在运行的代码对于实际的上传效果非常好,我正在努力为它编写一个单元测试。

因此您在这里指定的超时需要比默认超时短

如果未调用此方法,则默认超时时间间隔为5秒。您可以通过添加

jest.setTimeout(10000); // 10 seconds
关于官方文件的解释

在SetupTestFrameworkScript文件中可以执行此操作

在某些情况下,您只需要执行一次安装,如果您已经有了
jest.config.js
文件

// jest.config.js
module.exports = {

  // setup test framework after jest loaded
  setupFilesAfterEnv: [
    './tests/setupTestFrameworkScriptFile.js' // The path to a module that runs some code to configure or set up the testing framework before each test
  ],
};
现在我们可以为每个测试用例进行一次性设置

// ./tests/setupTestFrameworkScriptFile.js file

jest.setTimeout(10000) // we set timeout interval is 10 seconds for every test case

beforeAll(async () => {
  await initializeDatabase(); // this is just an example
  initializeOtherDepedency();

}, 3000) // assume we only need 3 seconds for initialization before runnng test case.
最后一个就是您的测试用例文件

// uploadfile.test.js
it('should upload a file', async () => {

})
另见本文件: