Unit testing 当我使用自定义axios实例时,Jest with moxios会一直超时

Unit testing 当我使用自定义axios实例时,Jest with moxios会一直超时,unit-testing,jestjs,axios,moxios,Unit Testing,Jestjs,Axios,Moxios,我有一个使用自定义axios实例的服务,我正在尝试测试该实例,但不断出现错误 以下是错误: : Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout. 以下是测试: import moxios from 'moxios'; import NotificationService, { instance } from '../NotificationServic

我有一个使用自定义axios实例的服务,我正在尝试测试该实例,但不断出现错误

以下是错误:

: Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
以下是测试:

import moxios from 'moxios';
import NotificationService, { instance } from '../NotificationService';

beforeEach(() => {
  moxios.install(instance);
});

afterEach(() => {
  moxios.uninstall(instance);
});

const fetchNotifData = {
  data: {
    bell: false,
    rollups: []
  }
};

describe('NotificationService.js', () => {
  it('returns the bell property', async done => {
    const isResolved = true;
    const data = await NotificationService.fetchNotifications(isResolved);

    moxios.wait(() => {
      let request = moxios.requests.mostRecent();
      console.log(request);
      request
        .respondWith({
          status: 200,
          response: fetchNotifData
        })
        .then(() => {
          console.log(data);
          expect(data).toHaveProperty('data.bell');
          done();
        });
    });
  });
});
下面是我要测试的代码:

import axios from 'axios';

// hardcoded user guid
const userId = '8c4';

// axios instance with hardcoded url and auth header
export const instance = axios.create({
  baseURL: 'hidden',
  headers: {
    Authorization:
      'JWT ey'
});

/**
 * Notification Service
 * Call these methods from the Notification Vuex Module
 */
export default class NotificationService {

  /**
   * @GET Gets a list of Notifications for a User
   * @returns {AxiosPromise<any>}
   * @param query
   */
  static async fetchNotifications(query) {
    try {
      const res = await instance.get(`/rollups/user/${userId}`, {
        query: query
      });
      console.log('NotificationService.fetchNotifications()', res);
      return res;
    } catch (error) {
      console.error(error);
    }
  }
}
从“axios”导入axios;
//硬编码用户guid
const userId='8c4';
//具有硬编码url和身份验证标头的axios实例
export const instance=axios.create({
baseURL:'隐藏',
标题:{
授权:
“JWT ey”
});
/**
*通知服务
*从通知Vuex模块调用这些方法
*/
导出默认类NotificationService{
/**
*@GET获取用户的通知列表
*@returns{axiopromise}
*@param查询
*/
静态异步获取通知(查询){
试一试{
const res=wait instance.get(`/rollups/user/${userId}`{
查询:查询
});
log('NotificationService.fetchNotifications()',res);
返回res;
}捕获(错误){
控制台错误(error);
}
}
}
我尝试过缩短jest超时时间,但没有成功。我认为是moxios没有正确安装axios实例,但我找不到任何原因说明它不正确


感谢您的帮助。

您是否尝试过通过将其添加到测试文件来更改Jest环境设置

/**
 * @jest-environment node
 */
import moxios from 'moxios';
...
Jest倾向于阻止请求发出,除非您添加它。无论哪种方式,我都使用
nock
而不是
moxios
,我推荐使用它