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

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

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

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

我有一个像这样的测试:

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

  1. import { mount } from '@vue/test-utils'
  2. import Hello from '@/components/hello_component.vue'
  3. describe('Hello Component', () => {
  4. test('is a Vue instance', () => {
  5. const wrapper = mount(Hello)
  6. expect(wrapper.isVueInstance()).toBeTruthy()
  7. })
  8. })

这会引发以下错误:

渲染错误:“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.

  1. // ~/components/hello_component.vue
  2. &lt;template&gt;
  3. &lt;div&gt;
  4. &lt;div v-if=&quot;$auth.loggedIn&quot;&gt;
  5. &lt;h1&gt;Hi, {{ userName }}&lt;/h1&gt;
  6. &lt;/div&gt;
  7. &lt;/div&gt;
  8. &lt;/template&gt;
  9. &lt;script&gt;
  10. export default {
  11. data () {
  12. userName: &quot;Archduke Chocula&quot;
  13. }
  14. }
  15. &lt;/script&gt;

I have a test that looks like this:

  1. // ~/spec/components/hello_component.spec.js
  2. import { mount } from &#39;@vue/test-utils&#39;
  3. import Hello from &#39;@/components/hello_component.vue&#39;
  4. describe(&#39;Hello Component&#39;, () =&gt; {
  5. test(&#39;is a Vue instance&#39;, () =&gt; {
  6. const wrapper = mount(Hello)
  7. expect(wrapper.isVueInstance()).toBeTruthy()
  8. })
  9. })

Which causes the following error

  1. 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对象。

  1. import { mount } from '@vue/test-utils'
  2. import Hello from '@/components/hello_component.vue'
  3. const authMock = {
  4. loggedIn: true
  5. };
  6. describe('Hello Component', () => {
  7. test('is a Vue instance', () => {
  8. const wrapper = mount(Hello, {
  9. mocks: {
  10. $auth: authMock
  11. }
  12. })
  13. expect(wrapper.isVueInstance()).toBeTruthy()
  14. })
  15. })

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

英文:

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

  1. import { mount } from &#39;@vue/test-utils&#39;
  2. import Hello from &#39;@/components/hello_component.vue&#39;
  3. const authMock = {
  4. loggedIn: true
  5. };
  6. describe(&#39;Hello Component&#39;, () =&gt; {
  7. test(&#39;is a Vue instance&#39;, () =&gt; {
  8. const wrapper = mount(Hello, {
  9. mocks: {
  10. $auth: authMock
  11. }
  12. })
  13. expect(wrapper.isVueInstance()).toBeTruthy()
  14. })
  15. })

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:

确定