Skip to content

Commit 8ce5a34

Browse files
committed
Part 5 - Set up test suite for Typescript Vue components
Jest is a very popular and reliable test framework for javascript projects. It can work with Vue components with the help of vue-jest, and with Typescript files with the help of ts-jest. Vue-test-utils offers a series of tools to interact with and retrieve information from Vue Components. You may notice all these libraries were added in the package.json, as well as the configuration necessary for Jest to know where to find the test files.
1 parent 6eaa62c commit 8ce5a34

File tree

4 files changed

+2089
-64
lines changed

4 files changed

+2089
-64
lines changed

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"private": true,
33
"scripts": {
4+
"test": "jest",
45
"dev": "npm run development",
56
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
67
"watch": "npm run development -- --watch",
@@ -10,20 +11,40 @@
1011
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
1112
},
1213
"devDependencies": {
14+
"@types/jest": "^24.0.11",
15+
"@vue/test-utils": "^1.0.0-beta.29",
1316
"axios": "^0.18",
17+
"babel-core": "^6.26.3",
1418
"bootstrap": "^4.0.0",
1519
"cross-env": "^5.1",
20+
"jest": "^24.7.1",
1621
"jquery": "^3.2",
1722
"laravel-mix": "^4.0.7",
1823
"lodash": "^4.17.5",
1924
"popper.js": "^1.12",
2025
"resolve-url-loader": "^2.3.1",
2126
"sass": "^1.15.2",
2227
"sass-loader": "^7.1.0",
28+
"ts-jest": "^24.0.2",
2329
"ts-loader": "^5.4.3",
2430
"typescript": "^3.4.5",
2531
"vue": "^2.5.17",
32+
"vue-jest": "^3.0.4",
2633
"vue-property-decorator": "^8.1.0",
2734
"vue-template-compiler": "^2.6.10"
35+
},
36+
"jest": {
37+
"moduleFileExtensions": [
38+
"js",
39+
"ts",
40+
"json",
41+
"vue"
42+
],
43+
"transform": {
44+
".*\\.(vue)$": "vue-jest",
45+
"^.+\\.tsx?$": "ts-jest"
46+
},
47+
"testURL": "http://localhost/",
48+
"testRegex": "(/tests/js/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$"
2849
}
2950
}

tests/js/Unit/ExampleComponent.spec.js

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {shallowMount, Wrapper} from '@vue/test-utils';
2+
import ExampleComponent from '../../../resources/js/components/ExampleComponent.vue';
3+
4+
describe('ExampleComponent - Unit', () => {
5+
let wrapper: Wrapper<ExampleComponent>;
6+
7+
const mountWithName = (name: string) => {
8+
wrapper = shallowMount(ExampleComponent, {
9+
propsData: {
10+
userName: name,
11+
}
12+
});
13+
};
14+
15+
test('It renders a given user name', () => {
16+
mountWithName('Foobar');
17+
expect(wrapper.text()).toContain('Foobar');
18+
});
19+
20+
test('It writes the given name backwards', () => {
21+
mountWithName('ABC');
22+
expect(wrapper.text()).toContain('CBA');
23+
});
24+
});

0 commit comments

Comments
 (0)