W
W
Wayne12121212020-09-14 19:16:31
JavaScript
Wayne1212121, 2020-09-14 19:16:31

Jest vue utils test how to test axios requests?

Hello!

Login Component

<template lang="html">
<div class="auth">
  <div class="auth__container">
    <input
      class='auth__input'
      v-model='login'
    />
    <input
      class='auth__input'
      v-model='password'
    />
    <button
      class='auth__button'
      @click='entry'
    >entry</button>
    <span
      v-if='error'
      class='auth__error'
    >{{ error }}</span>
    <span
      v-if='success'
      class='auth__success'
    >{{ success }}</span>
  </div>
</div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      login: '',
      password: '',

      error: '',
      success: '',

      rest: {}
    }
  },
  methods: {
    async entry() {
      let {
        data
      } = await axios.get('login.js')

      this.rest = data
    }
  }
}
</script>


Test

import { mount } from '@vue/test-utils'
import Login from '@/components/Login.vue'

describe('Login', () => {
  it('toEq', async () => {
    const wrapper = mount(Login)

    wrapper.find('button').trigger('click')

    return expect(wrapper.vm.rest).toBe('value')
  })
})


Expected response is 'value' but rest is ' '

If I try nextTick
import { mount } from '@vue/test-utils'
import Login from '@/components/Login.vue'

describe('Login', () => {
  it('toEq', done => {
    const wrapper = mount(Login)

    wrapper.find('button').trigger('click')

    wrapper.vm.$nextTick(() => {
      expect(wrapper.vm.rest).toBe('value')

      done()
    })
  })
})


Then there is a wait of 7 seconds and the test fails due to the fact that the time has passed

. What should I do?
 Please help, I've looked through everything and I don't understand

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2020-09-15
@Wayne1212121

Mock the method and check if it is called on click.
Unit testing requests is a bad idea.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question