【问题标题】:`moxios.wait` never executes when testing VueJS with Jest, Vue Test Utils and Moxios`moxios.wait` 在使用 Jest、Vue Test Utils 和 Moxios 测试 VueJS 时永远不会执行
【发布时间】:2018-08-14 07:06:01
【问题描述】:

我正在尝试为 VueJS 组件编写我的第一个单元测试,该组件在呈现时从 API 端点获取一些数据:

我的 VueJS 组件:

import axios from 'axios';

export default {
  props: {
          userIndexUrl: {
            required: true
          }
        },
  data() {
    userList: [],
    tableIsLoading: true
  },
  created() {
    const url = this.userIndexUrl;
    axios.get(url)
    .then(response => {
      this.userList = response.data.data;
      this.tableIsLoading = false;
    })
  },
}

和我的测试:

import { mount } from 'vue-test-utils'
import moxios from 'moxios'
import UsersAddRemove from 'users_add_remove.vue'

describe('UsersAddRemove', () => {
  const PROPS_DATA = {
      userIndexUrl: '/users.json'
  }

  beforeEach(() => {
    moxios.install();
    moxios.stubRequest('/users1.json', {
      status: 200,
      response: {
        "count": 1,
        "data": [
            { "id" : 1,
              "email" : "brayan@schaeferkshlerin.net",
              "first_name" : "Kenneth",
              "last_name" : "Connelly"
            }
          ]
        }
    });
  });

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

  it('loads users from remote JSON endpoint and renders table', () => {
    const wrapper = mount(UsersAddRemove, { propsData: PROPS_DATA });
    moxios.wait(() => {
      expect(wrapper.html()).toContain('<td class="">brayan@schaeferkshlerin1.net</td>');
      done();
    });
  });
});

所以我期望发生的是组件被实例化,然后它执行 API 调用,这被 Moxios 捕获,之后它应该执行moxios.wait,这没有发生。测试似乎忽略了moxios.wait,即使它不应该通过也成功通过。

我在这里错过了什么?

【问题讨论】:

    标签: vue.js moxios


    【解决方案1】:

    尝试添加 done 作为参数:

      it('loads users from remote JSON endpoint and renders table', (done) => {
      // -----------------------------------------------------------^^^^^^
        const wrapper = mount(UsersAddRemove, { propsData: PROPS_DATA });
        moxios.wait(() => {
          expect(wrapper.html()).toContain('<td class="">brayan@schaeferkshlerin1.net</td>');
          done();
        });
      });
    

    等待 Ajax

    如下更改:

    // remove the stubRequest of beforeEach()
    beforeEach(() => {
      moxios.install();
    });
    
    // afterEach stays the same
    
    it('loads users from remote JSON endpoint and renders table', (done) => {
      const wrapper = mount(UsersAddRemove, { propsData: PROPS_DATA });
    
      moxios.wait(() => {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: {
            "count": 1,
            "data": [{
              "id": 1,
              "email": "brayan@schaeferkshlerin.net",
              "first_name": "Kenneth",
              "last_name": "Connelly"
            }]
          }
        }).then(function() {
          expect(wrapper.html()).toContain('<td class="">brayan@schaeferkshlerin1.net</td>');
          done();
        });
      });
    });
    

    【讨论】:

    • 太棒了,谢谢!但是,现在它实际上并没有等待 AJAX 调用完成。有什么想法吗?
    • 事情是你自己的 created() 不等待 ajax 请求(它只是调用它);并不是说我知道它是否可以等待它。我看看你能不能等到stubRequest 被调用。
    • 我有个主意。不知道它是否会起作用(无法测试),但试一试,你会吗?我添加了moxios.requests.mostRecent().then(() =&gt; {
    • 再拍一张。可以试试吗?
    • 谢谢!我已经看到在一些示例中使用了该方法,但它违背了创建 stubRequests 的意义。无论哪种方式,我都尝试过,但它仍然无法正常工作。我开始认为其他东西可能是这里的罪魁祸首。不过,您确实回答了最初的问题!
    猜你喜欢
    • 2020-02-13
    • 2019-09-26
    • 2019-05-28
    • 2018-08-06
    • 2019-10-24
    • 2019-04-19
    • 1970-01-01
    • 2018-09-01
    • 2018-04-29
    相关资源
    最近更新 更多