Skip to content

Commit ec2a1e3

Browse files
committed
Setup component testing with jest
1 parent 965c38c commit ec2a1e3

7 files changed

Lines changed: 905 additions & 755 deletions

File tree

.babelrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"presets": [
3+
["env", { "modules": false }]
4+
],
5+
"env": {
6+
"test": {
7+
"presets": [
8+
["env", { "targets": { "node": "current" }}]
9+
]
10+
}
11+
}
12+
}
13+

components/Global/Elements/BlockButton/BlockButton.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
</div>
1717
</template>
1818
<script>
19-
import Icon from '../Icon/Icon.vue'
20-
import Button from '../Button/Button.vue'
19+
import Icon from '~/components/Global/Elements/Icon/Icon.vue'
20+
import Button from '~/components/Global/Elements/Button/Button.vue'
2121
2222
export default {
2323
name: 'hc-block-button',
2424
components: {
25-
Button,
26-
Icon
25+
'hc-button': Button,
26+
'hc-icon': Icon
2727
},
2828
props: {
2929
isPending: {

package.json

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"eslint": "eslint --ext .js,.vue .",
3838
"styleguide": "vue-styleguidist server",
3939
"styleguide:build": "vue-styleguidist build",
40-
"test": "ava",
40+
"test": "jest",
4141
"cypress:open": "cypress open",
4242
"cypress:run": "cypress run",
4343
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov"
@@ -119,7 +119,7 @@
119119
"@nuxtjs/webpackmonitor": "~0.1.0",
120120
"@vue/server-test-utils": "^1.0.0-beta.25",
121121
"@vue/test-utils": "^1.0.0-beta.25",
122-
"ava": "~0.25.0",
122+
"babel-jest": "^23.6.0",
123123
"backpack-core": "~0.7.0",
124124
"codecov": "~3.0.4",
125125
"eslint": "~4.19.1",
@@ -132,6 +132,7 @@
132132
"eslint-plugin-standard": "~3.1.0",
133133
"eslint-plugin-vue": "^4.5.0",
134134
"istanbul": "~0.4.5",
135+
"jest": "^23.6.0",
135136
"jsdom": "^12.0.0",
136137
"jsdom-global": "^3.0.2",
137138
"less": "~2.7.3",
@@ -144,11 +145,21 @@
144145
"sinon": "^6.3.4",
145146
"source-map-support": "~0.5.6",
146147
"vue-docgen-api": "^2.3.11",
148+
"vue-jest": "^2.6.0",
147149
"vue-style-loader": "~4.1.0"
148150
},
149-
"ava": {
150-
"require": [
151-
"./test/helpers/setup.js"
152-
]
151+
"jest": {
152+
"moduleFileExtensions": [
153+
"js",
154+
"json",
155+
"vue"
156+
],
157+
"moduleNameMapper": {
158+
"^~/(.*)$": "<rootDir>/$1"
159+
},
160+
"transform": {
161+
".*\\.(vue)$": "vue-jest",
162+
"^.+\\.js$": "<rootDir>/node_modules/babel-jest"
163+
}
153164
}
154165
}

test/components/Global/Element/BlockButton/BlockButton.test.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
1-
import test from 'ava'
2-
import { mount } from '@vue/test-utils'
3-
import BlockButton from '../../../../../components/Global/Elements/BlockButton/BlockButton'
4-
import Icon from '../../../../../components/Global/Elements/Icon/Icon.vue'
5-
import Button from '../../../../../components/Global/Elements/Button/Button.vue'
6-
7-
const mountBlockButton = (propsData = {}) => {
8-
return mount(BlockButton, {
1+
import { shallowMount } from '@vue/test-utils'
2+
import BlockButton from '~/components/Global/Elements/BlockButton/BlockButton'
3+
4+
const shallowMountBlockButton = (propsData = {}) => {
5+
return shallowMount(BlockButton, {
96
mocks: { $t: () => {} },
107
propsData
118
})
129
}
1310

14-
test('It renders a `<div>`.', (t) => {
15-
const wrapper = mountBlockButton()
16-
t.true(wrapper.is('div'))
11+
test('renders', () => {
12+
const wrapper = shallowMountBlockButton()
13+
expect(wrapper.is('div')).toBeTruthy()
1714
})
1815

19-
test('is enabled when no request is currently pending', (t) => {
20-
const wrapper = mountBlockButton({isPending: false})
21-
t.deepEqual(wrapper.findAll('hc-button').length, 1)
22-
t.deepEqual(wrapper.find('hc-button').attributes(), {color: 'button'})
16+
describe('no request pending', () => {
17+
test('is enabled', () => {
18+
const wrapper = shallowMountBlockButton({isPending: false})
19+
expect(wrapper.findAll('hc-button-stub')).toHaveLength(1)
20+
expect(wrapper.find('hc-button-stub').attributes()).toEqual(expect.not.objectContaining({isloading: 'true'}))
21+
})
2322
})
2423

25-
test('shows a loading spinner when a request is pending', (t) => {
26-
const wrapper = mountBlockButton({isPending: true})
27-
t.truthy(wrapper.find('hc-button').attributes().isloading)
28-
})
24+
describe('request pending', () => {
25+
let wrapper
26+
27+
beforeEach(() => {
28+
wrapper = shallowMountBlockButton({isPending: true})
29+
})
30+
31+
test('shows a loading spinner', () => {
32+
expect(wrapper.find('hc-button-stub').attributes().isloading).toBeTruthy()
33+
})
2934

30-
test('is disabled when a request is pending', (t) => {
31-
const wrapper = mountBlockButton({isPending: true})
32-
t.truthy(wrapper.find('hc-button').attributes().disabled)
35+
test('is disabled', () => {
36+
expect(wrapper.find('hc-button-stub').attributes().disabled).toBeTruthy()
37+
})
3338
})
3439

3540
/*
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import test from 'ava'
21
import { shallowMount } from '@vue/test-utils'
32
import Icon from '../../../../../components/Global/Elements/Icon/Icon'
43

5-
test('It should render an `<i>`.', (t) => {
4+
test('It should render an `<i>`.', () => {
65
const wrapper = shallowMount(Icon)
76

8-
t.true(wrapper.is('i'))
7+
expect(wrapper.is('i')).toBeTruthy()
98
})

test/helpers/setup.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)