1.vue create xx
2.选择jest
.
3.执行npm run test:unit
结果:
4.快照测试:
import { shallowMount } from '@vue/test-utils' import HelloWorld from '@/components/HelloWorld.vue' describe('HelloWorld.vue', () => { it('renders props.msg when passed', () => { const msg = 'new message' const wrapper = shallowMount(HelloWorld, { propsData: { msg } }) // expect(wrapper.text()).toMatch(msg) expect(wrapper.html()).toMatchSnapshot() }) })
再次执行:
npm run test:unit
第一次运行后,Jest 将在与原始测试所在的目录中生成一个__ snapshots __
目录。让我们来看看新生成的快照。
// Jest Snapshot v1, https://goo.gl/fbAQLP exports[`HelloWorld.vue renders props.msg when passed 1`] = ` <div class="hello"> <h1>new message</h1> <p> For a guide and recipes on how to configure / customize this project,<br> check out the <a href="https://cli.vuejs.org" target="_blank" rel="noopener">vue-cli documentation</a>. </p> <h3>Installed CLI Plugins</h3> <ul> <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-babel" target="_blank" rel="noopener">babel</a></li> <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint" target="_blank" rel="noopener">eslint</a></li> <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-unit-jest" target="_blank" rel="noopener">unit-jest</a></li> </ul> <h3>Essential Links</h3> <ul> <li><a href="https://vuejs.org" target="_blank" rel="noopener">Core Docs</a></li> <li><a href="https://forum.vuejs.org" target="_blank" rel="noopener">Forum</a></li> <li><a href="https://chat.vuejs.org" target="_blank" rel="noopener">Community Chat</a></li> <li><a href="https://twitter.com/vuejs" target="_blank" rel="noopener">Twitter</a></li> <li><a href="https://news.vuejs.org" target="_blank" rel="noopener">News</a></li> </ul> <h3>Ecosystem</h3> <ul> <li><a href="https://router.vuejs.org" target="_blank" rel="noopener">vue-router</a></li> <li><a href="https://vuex.vuejs.org" target="_blank" rel="noopener">vuex</a></li> <li><a href="https://github.com/vuejs/vue-devtools#vue-devtools" target="_blank" rel="noopener">vue-devtools</a></li> <li><a href="https://vue-loader.vuejs.org" target="_blank" rel="noopener">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank" rel="noopener">awesome-vue</a></li> </ul> </div> `;
Jest 在每个步骤都渲染 Vue 组件,并将其渲染状态保存为纯文本。下次运行测试时,如果渲染的组件的任何部分发生更改,则测试将失败并指出差异。
如果您想要更改组件的行为,则需要重新生成这些快照。为此,请运行:
npm run test:unit -- -u
标签:vue,快照,demo,HelloWorld,jest,msg,unit From: https://www.cnblogs.com/vickylinj/p/16918364.html