mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 06:05:11 +00:00
chore(examples): Add Jest + Vue Test Utils example (#4116)
* chore(examples): Add Jest + Vue Test Utils example * style: Lint * refactor: Rename button component & fix lint errors * refactor: Use export default * refactor: Babel 7 setup
This commit is contained in:
parent
6c5d89dead
commit
5c0d12fb13
1
examples/jest-vtu-example/.eslintignore
Normal file
1
examples/jest-vtu-example/.eslintignore
Normal file
@ -0,0 +1 @@
|
||||
*.spec.js.snap
|
14
examples/jest-vtu-example/.gitignore
vendored
Normal file
14
examples/jest-vtu-example/.gitignore
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
# dependencies
|
||||
node_modules
|
||||
|
||||
# logs
|
||||
npm-debug.log
|
||||
|
||||
# Nuxt build
|
||||
.nuxt
|
||||
|
||||
# Nuxt generate
|
||||
dist
|
||||
|
||||
# coverage report
|
||||
coverage/
|
9
examples/jest-vtu-example/README.md
Normal file
9
examples/jest-vtu-example/README.md
Normal file
@ -0,0 +1,9 @@
|
||||
# Jest + Vue Test Utils example
|
||||
|
||||
```sh
|
||||
# Install dependencies
|
||||
npm i # or yarn
|
||||
|
||||
# Run tests
|
||||
npm test
|
||||
```
|
7
examples/jest-vtu-example/babel.config.js
Normal file
7
examples/jest-vtu-example/babel.config.js
Normal file
@ -0,0 +1,7 @@
|
||||
module.exports = {
|
||||
env: {
|
||||
test: {
|
||||
presets: ['@babel/preset-env']
|
||||
}
|
||||
}
|
||||
}
|
36
examples/jest-vtu-example/components/Btn/Btn.spec.js
Normal file
36
examples/jest-vtu-example/components/Btn/Btn.spec.js
Normal file
@ -0,0 +1,36 @@
|
||||
import { shallowMount } from '@vue/test-utils'
|
||||
import Btn from './Btn'
|
||||
|
||||
const factory = () => shallowMount(Btn, {
|
||||
propsData: {
|
||||
label: 'click me!'
|
||||
}
|
||||
})
|
||||
|
||||
describe('Btn', () => {
|
||||
test('mounts properly', () => {
|
||||
const wrapper = factory()
|
||||
expect(wrapper.isVueInstance()).toBeTruthy()
|
||||
})
|
||||
|
||||
test('renders properly', () => {
|
||||
const wrapper = factory()
|
||||
expect(wrapper.html()).toMatchSnapshot()
|
||||
})
|
||||
|
||||
test('calls handleClick on click', () => {
|
||||
const wrapper = factory()
|
||||
const handleClickMock = jest.fn()
|
||||
wrapper.setMethods({
|
||||
handleClick: handleClickMock
|
||||
})
|
||||
wrapper.find('button').trigger('click')
|
||||
expect(handleClickMock).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('clicked is true after click', () => {
|
||||
const wrapper = factory()
|
||||
wrapper.find('button').trigger('click')
|
||||
expect(wrapper.vm.clicked).toBe(true)
|
||||
})
|
||||
})
|
25
examples/jest-vtu-example/components/Btn/Btn.vue
Normal file
25
examples/jest-vtu-example/components/Btn/Btn.vue
Normal file
@ -0,0 +1,25 @@
|
||||
<template>
|
||||
<button @click="handleClick">
|
||||
{{ label }}
|
||||
</button>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'Button',
|
||||
props: {
|
||||
label: {
|
||||
type: String,
|
||||
required: true
|
||||
}
|
||||
},
|
||||
data: () => ({
|
||||
clicked: false
|
||||
}),
|
||||
methods: {
|
||||
handleClick() {
|
||||
this.clicked = true
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
@ -0,0 +1,7 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`Btn renders properly 1`] = `
|
||||
<button>
|
||||
click me!
|
||||
</button>
|
||||
`;
|
5
examples/jest-vtu-example/layouts/default.vue
Normal file
5
examples/jest-vtu-example/layouts/default.vue
Normal file
@ -0,0 +1,5 @@
|
||||
<template>
|
||||
<div>
|
||||
<nuxt />
|
||||
</div>
|
||||
</template>
|
3
examples/jest-vtu-example/nuxt.config.js
Normal file
3
examples/jest-vtu-example/nuxt.config.js
Normal file
@ -0,0 +1,3 @@
|
||||
export default {
|
||||
mode: 'universal'
|
||||
}
|
46
examples/jest-vtu-example/package.json
Normal file
46
examples/jest-vtu-example/package.json
Normal file
@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "example-jest-vtu",
|
||||
"scripts": {
|
||||
"dev": "nuxt",
|
||||
"build": "nuxt build",
|
||||
"start": "nuxt start",
|
||||
"generate": "nuxt generate",
|
||||
"test": "jest ."
|
||||
},
|
||||
"dependencies": {
|
||||
"nuxt-edge": "latest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.1.2",
|
||||
"@vue/test-utils": "^1.0.0-beta.25",
|
||||
"babel-core": "^7.0.0-bridge.0",
|
||||
"babel-jest": "^23.6.0",
|
||||
"jest": "^23.6.0",
|
||||
"jest-serializer-vue": "^2.0.2",
|
||||
"vue-jest": "^2.6.0"
|
||||
},
|
||||
"jest": {
|
||||
"moduleFileExtensions": [
|
||||
"js",
|
||||
"json",
|
||||
"vue"
|
||||
],
|
||||
"watchman": false,
|
||||
"moduleNameMapper": {
|
||||
"^~/(.*)$": "<rootDir>/$1",
|
||||
"^~~/(.*)$": "<rootDir>/$1"
|
||||
},
|
||||
"transform": {
|
||||
"^.+\\.js$": "<rootDir>/node_modules/babel-jest",
|
||||
".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
|
||||
},
|
||||
"snapshotSerializers": [
|
||||
"<rootDir>/node_modules/jest-serializer-vue"
|
||||
],
|
||||
"collectCoverage": true,
|
||||
"collectCoverageFrom": [
|
||||
"<rootDir>/components/**/*.vue",
|
||||
"<rootDir>/pages/*.vue"
|
||||
]
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`index renders properly 1`] = `
|
||||
<section class="container">
|
||||
<div>
|
||||
<h1 class="title">
|
||||
jest-vtu-example
|
||||
</h1>
|
||||
<btn-stub label="click me!"></btn-stub>
|
||||
</div>
|
||||
</section>
|
||||
`;
|
14
examples/jest-vtu-example/pages/index.spec.js
Normal file
14
examples/jest-vtu-example/pages/index.spec.js
Normal file
@ -0,0 +1,14 @@
|
||||
import { shallowMount } from '@vue/test-utils'
|
||||
import index from './index'
|
||||
|
||||
describe('index', () => {
|
||||
test('mounts properly', () => {
|
||||
const wrapper = shallowMount(index)
|
||||
expect(wrapper.isVueInstance()).toBeTruthy()
|
||||
})
|
||||
|
||||
test('renders properly', () => {
|
||||
const wrapper = shallowMount(index)
|
||||
expect(wrapper.html()).toMatchSnapshot()
|
||||
})
|
||||
})
|
20
examples/jest-vtu-example/pages/index.vue
Normal file
20
examples/jest-vtu-example/pages/index.vue
Normal file
@ -0,0 +1,20 @@
|
||||
<template>
|
||||
<section class="container">
|
||||
<div>
|
||||
<h1 class="title">
|
||||
jest-vtu-example
|
||||
</h1>
|
||||
<Btn label="click me!" />
|
||||
</div>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import Btn from '~/components/Btn/Btn.vue'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
Btn
|
||||
}
|
||||
}
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user