如何在使用Jest测试Nuxt中的组件时添加/模拟Nuxt Auth库

huangapple go评论75阅读模式
英文:

How to add/mock the Nuxt Auth library when testing components in Nuxt with Jest

问题

JS新手在这里。

我已经生成了一个Nuxt应用程序,并在我的nuxt.config.js中全局实现了@nuxt/auth中间件。它在我的应用程序中按预期工作。

现在我想测试一些引用$auth对象的组件。

// ~/components/hello_component.vue

<template>
  <div>
    <div v-if="$auth.loggedIn">
      <h1>Hi, {{ userName }}</h1>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    userName: "Archduke Chocula"
  }
}
</script>

我有一个像这样的测试:

// ~/spec/components/hello_component.spec.js

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

describe('Hello Component', () => {
  test('is a Vue instance', () => {
    const wrapper = mount(Hello)
    expect(wrapper.isVueInstance()).toBeTruthy()
  })
})

这会引发以下错误:

渲染错误:“TypeError: 无法读取未定义的属性'loggedIn'”

所以显然我需要在某个地方定义auth,所以我的问题是:

  1. 我应该在哪里以及如何将此依赖项添加到我的测试中(每个测试?全局为所有测试?)?
  2. 如何模拟loggedIn方法的响应,以便我可以测试已登录/未登录的情况?
  3. 有没有办法在我的测试中模拟Nuxt环境,以便我可以测试我的组件等,就好像它们已在Nuxt中挂载?这是一个好主意吗?

提前感谢任何帮助!

英文:

JS newbie here.

I have generated a Nuxt app and have implemented the @nuxt/auth middleware globally in my nuxt.config.js. It's working as expected in my app.

Now I would like to test some of my components that reference the $auth object.

// ~/components/hello_component.vue

&lt;template&gt;
  &lt;div&gt;
    &lt;div v-if=&quot;$auth.loggedIn&quot;&gt;
      &lt;h1&gt;Hi, {{ userName }}&lt;/h1&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  data () {
    userName: &quot;Archduke Chocula&quot;
  }
}
&lt;/script&gt;

I have a test that looks like this:

// ~/spec/components/hello_component.spec.js

import { mount } from &#39;@vue/test-utils&#39;
import Hello from &#39;@/components/hello_component.vue&#39;

describe(&#39;Hello Component&#39;, () =&gt; {
  test(&#39;is a Vue instance&#39;, () =&gt; {
    const wrapper = mount(Hello)
    expect(wrapper.isVueInstance()).toBeTruthy()
  })
})

Which causes the following error

Error in render: &quot;TypeError: Cannot read property &#39;loggedIn&#39; of undefined&quot;

So clearly I need to define auth somewhere, so my questions are:

  1. Where and how should I add this dependency to my tests (per test? globally for all tests?)?
  2. How can I mock the response of the loggedIn method so that I can test scenarios where I'm either logged in/out?
  3. Is there a way to mock the Nuxt environment in my tests so that I can test my components etc as if they were mounted in Nuxt? Is that a even a good idea?

Thanks in advance for any help!

答案1

得分: 8

你可以通过在调用mount时传递一个options对象中的模拟对象来模拟你的$auth对象。

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

const authMock = {
  loggedIn: true
};

describe('Hello Component', () => {
  test('is a Vue instance', () => {
    const wrapper = mount(Hello, {
      mocks: {
        $auth: authMock
      }
    })
    expect(wrapper.isVueInstance()).toBeTruthy()
  })
})

你可以根据需要扩展模拟对象,或更改它在每个测试中返回的值。

英文:

You can mock your $auth object it by passing the mock in an options object when calling mount

import { mount } from &#39;@vue/test-utils&#39;
import Hello from &#39;@/components/hello_component.vue&#39;

const authMock = {
  loggedIn: true
};

describe(&#39;Hello Component&#39;, () =&gt; {
  test(&#39;is a Vue instance&#39;, () =&gt; {
    const wrapper = mount(Hello, {
      mocks: {
        $auth: authMock
      }
    })
    expect(wrapper.isVueInstance()).toBeTruthy()
  })
})

You can extend the mock however your like, or change the values that it returns for each test.

huangapple
  • 本文由 发表于 2020年1月6日 16:13:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定