Nuxt,Vue,Jest和Vuetify中项目的写作单元测试

问题描述 投票:1回答:1

TL; DR

Vuetify link复制的组件的单元测试通过,但是我在pug中编写的组件的实际单元测试未通过。

详细信息:

我正在使用VueNUXTVuetifypug作为模板语言构建的项目。我正在配置项目以使用JEST测试运行程序编写测试用例。单元测试当前失败,我将需要一些步骤来确定问题。

我已阅读以下内容:

正在使用以下版本:

  • nuxt: ^2.0.0
  • nuxtjs/vuetify: ^1.10.2
  • pug: ^2.0.4
  • pug-plain-loader: ^1.0.0
  • jest: ^24.1.0
  • vue-jest: ^4.0.0-0

以下是相关的文件夹结构

  • <project root>/jest.config.js
  • <project root>/components/common/CustomCard.vue
  • <project root>/components/common/MessageDialog.vue
  • <project root>/tests/unit/components/common/TestMessageDialog.spec.js
  • <project root>/tests/unit/components/common/TestCustomCard.spec.js

以下内容似乎是配置的相关部分:

jest.config.js

module.exports = {
  verbose: true,
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/$1',
    '^~/(.*)$': '<rootDir>/$1',
    '^vue$': 'vue/dist/vue.common.js'
  },
  moduleFileExtensions: ['js', 'vue', 'json'],
  transform: {
    '^.+\\.js$': 'babel-jest',
    '.*\\.(vue)$': 'vue-jest'
  },
  'collectCoverage': false,
  'collectCoverageFrom': [
    '<rootDir>/components/**/*.vue',
    '<rootDir>/pages/**/*.vue'
  ],
  globals: {
    'vue-jest': {
      pug: {
        doctype: 'html'
      }
    }
  },
  preset: '@vue/cli-plugin-unit-jest/presets/no-babel'
}

TestMessageDialog.spec.js

import {
  mount,
  createLocalVue
} from '@vue/test-utils'
import Vuetify from 'vuetify'
import MessageDialog from '@/components/common/MessageDialog.vue'

describe('MessageDialog.vue', () => {
  const sampleData = {
    titleText: 'title text with unique id : 2020.03.12.00'
  }

  let wrappedMessageDialog

  beforeEach(() => {
    const vuetify = new Vuetify()
    const localVue = createLocalVue()

    wrappedMessageDialog = mount(
      MessageDialog,
      {
        localVue,
        vuetify,
        propsData: sampleData
      }
    )
  })

  it('renders correctly when passed props', () => {
    expect(
      wrappedMessageDialog
        .find({ name: 'v-card-title' })
        .text()
    ).toMatch(sampleData.titleText)
  })
})

MessageDialog.vue

<template lang="pug">
v-dialog(v-model="isVisible" width="600")
  v-card
    v-card-title {{ titleText }}
</template>


<script>

export default {
  props: {
    titleText: {
      default: '',
      type: String
    }
  }
}
</script>

我收到以下错误

FAIL tests/unit/components/common/TestMessageDialog.spec.js
[vue-test-utils]: find did not return Component, cannot call text() on empty Wrapper

      31 |       wrappedMessageDialog
      32 |         .find({ name: 'v-card-title' })
    > 33 |         .text()
         |          ^
      34 |     ).toMatch(sampleData.titleText)
      35 |   })
      36 | })

      at throwError (node_modules/@vue/test-utils/dist/vue-test-utils.js:1709:9)
      at ErrorWrapper.text (node_modules/@vue/test-utils/dist/vue-test-utils.js:8767:3)
      at Object.it (tests/unit/components/common/TestMessageDialog.spec.js:33:10)

  console.error node_modules/vue/dist/vue.common.dev.js:630
    [Vue warn]: Unknown custom element: <v-dialog> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

    found in

    ---> <Anonymous>
           <Root>

<v-dialog><v-card><v-card-title>的类似警告

vue.js vuetify.js nuxt.js jest
1个回答
0
投票

看起来您希望将组件注册到MessageDialog组件中。例如。

<script>
import Foo from './Foo.vue;
...
export default {
  components: { Foo }
  ...
}
...
</script>

More info: https://vuejs.org/v2/guide/components-registration.html#Local-Registration
© www.soinside.com 2019 - 2024. All rights reserved.