首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我使用定制的axios实例时,moxios的Jest保持超时

当我使用定制的axios实例时,moxios的Jest保持超时
EN

Stack Overflow用户
提问于 2019-02-21 10:54:14
回答 1查看 452关注 0票数 2

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

下面是错误:

代码语言:javascript
复制
: Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

下面是测试结果:

代码语言:javascript
复制
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();
        });
    });
  });
});

下面是我要测试的代码:

代码语言:javascript
复制
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);
    }
  }
}

我试着缩短jest超时时间,但没有起作用。我认为是moxios没有正确安装axios实例,但我找不到任何原因。

感谢您的帮助,谢谢您的帮助。

EN

回答 1

Stack Overflow用户

发布于 2019-08-08 22:32:42

您是否尝试过通过将以下内容添加到测试文件来更改Jest环境设置?

代码语言:javascript
复制
/**
 * @jest-environment node
 */
import moxios from 'moxios';
...

Jest倾向于阻止请求发出,除非您添加它。无论哪种方式,我都使用nock而不是moxios,并且我推荐它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54798519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档