运行 (npm t) node_modules/@vue/test-utils/dist/vue-test-utils.js:1704 This error in Vue.js 时如何纠正此类警告?
How to rectify this type of warning when run (npm t) node_modules/@vue/test-utils/dist/vue-test-utils.js:1704 This error in Vue.js?
我有一个组件负责注册用户,我使用 vue-jest 为该组件编写测试用例,我为该 Register 组件编写一些测试用例,当我 运行 npm t 它是显示如下[errors]但所有测试用例都应该通过,如何解决这个错误请帮我解决这个问题
Register.spec.js
import Register from '../../src/Pages/Register.vue';
import { createLocalVue, shallowMount} from '@vue/test-utils'
describe('Register.vue', ()=>{
let wrapper;
beforeEach(() => {
const localVue = createLocalVue();
wrapper = shallowMount(Register, localVue);
});
test('setup correctly', () => {
expect(true).toBe(true)
})
it('renders a vue instance', () => {
expect(shallowMount(Register).vm).toBeTruthy();
})
it('has an image', () => {
expect(wrapper.contains('#side-img')).toBe(true);
});
it('has a side content',()=>{
expect(wrapper.contains('#side-content')).toBe(true);
});
it('has a two title sections',()=>{
expect(wrapper.contains('#signup')).toBe(true);
expect(wrapper.contains('#login')).toBe(true);
});
it('has a full name input box',()=>{
expect(wrapper.contains('#name-input')).toBe(true);
});
it('has a email input box',()=>{
expect(wrapper.contains('#Email-input')).toBe(true);
});
it('has a password input box',()=>{
expect(wrapper.contains('#passField')).toBe(true);
});
it('has a eye icons',()=>{
expect(wrapper.contains('#togglePassword')).toBe(true);
});
it('has a mobile input field',()=>{
expect(wrapper.contains('.telephone')).toBe(true);
});
it('has a button field',()=>{
expect(wrapper.contains('.btn-section')).toBe(true);
})
})
Register.vue
<template>
<div class="main">
<div class="container">
<img id="side-img" src="../assets/sideImg.png" alt="notFound" />
<p id="side-content">Online Book Shopping</p>
<div class="box">
<div class="headings">
<h5 class="signin" id="login" :class="{ active: isLogin }" @click="isLogin = true">Login</h5>
<h5 class="signup" id="signup" :class="{ active: !isLogin }" @click="isLogin = false">signup</h5>
</div>
<form ref="myForm" @submit.prevent="handlesubmit">
<div class="fullname">
<p>FullName</p>
<input type="name" id="name-input" class="namebox" required v-model="fullName" autocomplete="off" pattern="[A-Za-z]{3,12}">
</div>
<div class="username">
<p>EmailID</p>
<input type="email" id="Email-input" class="emailbox" required v-model="email" pattern="^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$">
</div>
<div class="password-section">
<p>Password</p>
<input :type="password_type" class="password" :class="{'password-visible': isPasswordVisible }" id="passField" v-model="password" pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{6,}$" required>
<i class="bi bi-eye-slash" id="togglePassword" @click="togglePassword();"></i>
</div>
<div class="mobile">
<p>MobileNumber</p>
<input type="tel" class="telephone" v-model="mobile" id="tel" pattern="^\d{10}$" required>
</div>
<button class="btn-section" id="btn" type="submit">Signup</button>
</form>
</div>
</div>
</div>
</template>
<script>
import service from '../service/User'
export default {
name: 'Register',
}
contains
的警告本身和文档描述了如何解决该问题:
Deprecation warning
Using contains
is deprecated and will be removed in future releases. Use find
for DOM nodes (using querySelector
syntax), findComponent
for components, or wrapper.get
instead.
由于您的所有测试都使用 contains
和 querySelector
语法,因此将 wrapper.contains
替换为 wrapper.find
, and replace toBe(true)
with toBeTruthy()
。
我有一个组件负责注册用户,我使用 vue-jest 为该组件编写测试用例,我为该 Register 组件编写一些测试用例,当我 运行 npm t 它是显示如下[errors]但所有测试用例都应该通过,如何解决这个错误请帮我解决这个问题
Register.spec.js
import Register from '../../src/Pages/Register.vue';
import { createLocalVue, shallowMount} from '@vue/test-utils'
describe('Register.vue', ()=>{
let wrapper;
beforeEach(() => {
const localVue = createLocalVue();
wrapper = shallowMount(Register, localVue);
});
test('setup correctly', () => {
expect(true).toBe(true)
})
it('renders a vue instance', () => {
expect(shallowMount(Register).vm).toBeTruthy();
})
it('has an image', () => {
expect(wrapper.contains('#side-img')).toBe(true);
});
it('has a side content',()=>{
expect(wrapper.contains('#side-content')).toBe(true);
});
it('has a two title sections',()=>{
expect(wrapper.contains('#signup')).toBe(true);
expect(wrapper.contains('#login')).toBe(true);
});
it('has a full name input box',()=>{
expect(wrapper.contains('#name-input')).toBe(true);
});
it('has a email input box',()=>{
expect(wrapper.contains('#Email-input')).toBe(true);
});
it('has a password input box',()=>{
expect(wrapper.contains('#passField')).toBe(true);
});
it('has a eye icons',()=>{
expect(wrapper.contains('#togglePassword')).toBe(true);
});
it('has a mobile input field',()=>{
expect(wrapper.contains('.telephone')).toBe(true);
});
it('has a button field',()=>{
expect(wrapper.contains('.btn-section')).toBe(true);
})
})
Register.vue
<template>
<div class="main">
<div class="container">
<img id="side-img" src="../assets/sideImg.png" alt="notFound" />
<p id="side-content">Online Book Shopping</p>
<div class="box">
<div class="headings">
<h5 class="signin" id="login" :class="{ active: isLogin }" @click="isLogin = true">Login</h5>
<h5 class="signup" id="signup" :class="{ active: !isLogin }" @click="isLogin = false">signup</h5>
</div>
<form ref="myForm" @submit.prevent="handlesubmit">
<div class="fullname">
<p>FullName</p>
<input type="name" id="name-input" class="namebox" required v-model="fullName" autocomplete="off" pattern="[A-Za-z]{3,12}">
</div>
<div class="username">
<p>EmailID</p>
<input type="email" id="Email-input" class="emailbox" required v-model="email" pattern="^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$">
</div>
<div class="password-section">
<p>Password</p>
<input :type="password_type" class="password" :class="{'password-visible': isPasswordVisible }" id="passField" v-model="password" pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{6,}$" required>
<i class="bi bi-eye-slash" id="togglePassword" @click="togglePassword();"></i>
</div>
<div class="mobile">
<p>MobileNumber</p>
<input type="tel" class="telephone" v-model="mobile" id="tel" pattern="^\d{10}$" required>
</div>
<button class="btn-section" id="btn" type="submit">Signup</button>
</form>
</div>
</div>
</div>
</template>
<script>
import service from '../service/User'
export default {
name: 'Register',
}
contains
的警告本身和文档描述了如何解决该问题:
Deprecation warning
Using
contains
is deprecated and will be removed in future releases. Usefind
for DOM nodes (usingquerySelector
syntax),findComponent
for components, orwrapper.get
instead.
由于您的所有测试都使用 contains
和 querySelector
语法,因此将 wrapper.contains
替换为 wrapper.find
, and replace toBe(true)
with toBeTruthy()
。