Skip to content

Commit 2549bb8

Browse files
committed
feat: add jest.
1 parent 1155876 commit 2549bb8

File tree

9 files changed

+2419
-37
lines changed

9 files changed

+2419
-37
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ Homestead.json
1010
Homestead.yaml
1111
npm-debug.log
1212
yarn-error.log
13+
coverage

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# laravel-vue-test-use-jest
2+
Integrate Jest and code-coverage with Laravel Mix 4 and Laravel 6

babel.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// babel.config.js
2+
module.exports = api => {
3+
const isTest = api.env('test');
4+
5+
return {
6+
presets: [
7+
[
8+
'@babel/preset-env', {
9+
targets: {
10+
node: 'current',
11+
},
12+
},
13+
],
14+
]
15+
}
16+
}

jest.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// jest.config.js
2+
module.exports = {
3+
testRegex: 'resources/js/tests/.*.spec.js$',
4+
moduleNameMapper: {
5+
"^@/(.*)$": "<rootDir>/resources/js/$1"
6+
},
7+
moduleFileExtensions: ['js', 'json', 'vue'],
8+
transform: {
9+
'^.+\\.js$': '<rootDir>/node_modules/babel-jest',
10+
'.*\\.(vue)$': '<rootDir>/node_modules/vue-jest'
11+
},
12+
snapshotSerializers: ['jest-serializer-vue'],
13+
collectCoverageFrom: [
14+
'resources/js/**/*.{js,jsx,ts,tsx,vue}',
15+
],
16+
collectCoverage: true,
17+
coverageReporters: ['html', 'lcov', 'text-summary'],
18+
coverageDirectory: './coverage'
19+
}

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77
"watch-poll": "npm run watch -- --watch-poll",
88
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
99
"prod": "npm run production",
10-
"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"
10+
"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",
11+
"test": "jest"
1112
},
1213
"devDependencies": {
14+
"@babel/preset-env": "^7.7.1",
15+
"@vue/test-utils": "1.0.0-beta.29",
1316
"axios": "^0.19",
17+
"babel-core": "^7.0.0-bridge.0",
18+
"babel-jest": "^24.9.0",
1419
"bootstrap": "^4.0.0",
1520
"cross-env": "^5.1",
21+
"jest": "^24.9.0",
22+
"jest-serializer-vue": "^2.0.2",
1623
"jquery": "^3.2",
1724
"laravel-mix": "^4.0.7",
1825
"lodash": "^4.17.13",
@@ -21,6 +28,8 @@
2128
"sass": "^1.20.1",
2229
"sass-loader": "7.*",
2330
"vue": "^2.5.17",
24-
"vue-template-compiler": "^2.6.10"
31+
"vue-jest": "^3.0.5",
32+
"vue-template-compiler": "^2.6.10",
33+
"vuex": "^3.1.2"
2534
}
2635
}

resources/js/components/ExampleComponent.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div class="row justify-content-center">
44
<div class="col-md-8">
55
<div class="card">
6-
<div class="card-header">Example Component</div>
6+
<div class="card-header">Example Component one</div>
77

88
<div class="card-body">
99
I'm an example component.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`ExampleComponent test case one 1`] = `
4+
<div class="container">
5+
<div class="row justify-content-center">
6+
<div class="col-md-8">
7+
<div class="card">
8+
<div class="card-header">Example Component one</div>
9+
<div class="card-body">
10+
I'm an example component.
11+
</div>
12+
</div>
13+
</div>
14+
</div>
15+
</div>
16+
`;

resources/js/tests/example.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createLocalVue, mount } from '@vue/test-utils'
2+
import Vuex from 'vuex'
3+
import ExampleComponent from '@/components/ExampleComponent.vue'
4+
5+
const localVue = createLocalVue()
6+
localVue.use(Vuex)
7+
8+
test('it works', () => {
9+
expect(1 + 1).toBe(2)
10+
})
11+
12+
describe('ExampleComponent', () => {
13+
test('test case one', () => {
14+
const wrapper = mount(ExampleComponent, {
15+
localVue,
16+
propsData: {},
17+
mocks: {},
18+
stubs: {},
19+
methods: {}
20+
})
21+
22+
expect(wrapper.isVueInstance()).toBeTruthy()
23+
expect(wrapper).toMatchSnapshot()
24+
})
25+
})

0 commit comments

Comments
 (0)