Skip to content

Commit

Permalink
Now first working Jest tests. Therefore Hello.vue now has a Component…
Browse files Browse the repository at this point in the history
… property `hellomsg`, which we can check from within the test. The App also uses the new property and defines the hellomsg.
  • Loading branch information
jonashackt committed Jun 27, 2018
1 parent c5beb58 commit f98d2ea
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 28 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,41 @@ https://docs.npmjs.com/getting-started/updating-local-packages

## Testing

#### Install vue-test-utils

https://github.com/vuejs/vue-test-utils

`npm install --save-dev @vue/test-utils`

#### Jest

https://facebook.github.io/jest/

Intro-Blogpost: https://blog.codecentric.de/2017/06/javascript-unit-tests-sind-schwer-aufzusetzen-keep-calm-use-jest/

https://github.com/vuejs/vue-test-utils-jest-example

###### Jest Configuration

* [package.json](frontend/package.json):

```
"scripts": {
...
"unit": "jest --config test/unit/jest.conf.js --coverage",
....
},
```

* [frontend/test/unit/jest.conf.js](frontend/test/unit/jest.conf.js)

###### Run Unit tests

`npm run unit`

Run all tests (incl. E2E): `npm test`


#### E2E tests with Nightwatch

http://nightwatchjs.org/
Expand Down
9 changes: 9 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"vue-router": "^3.0.1"
},
"devDependencies": {
"@vue/test-utils": "^1.0.0-beta.20",
"autoprefixer": "^7.1.2",
"babel-core": "^6.26.3",
"babel-helper-vue-jsx-merge-props": "^2.0.3",
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<template>
<div id="app">
<router-view></router-view>
<router-view :hellomsg="msg"></router-view>
</div>
</template>

<script>
export default {
name: 'app'
name: 'app',
data () {
return {
msg: 'Welcome to your Vue.js powered Spring Boot App'
}
}
}
</script>

Expand Down
9 changes: 2 additions & 7 deletions frontend/src/components/Hello.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div class="hello">
<img src="./../assets/spring-boot-vuejs-logo.png">
<h1>{{ msg }}</h1>
<h1>{{ hellomsg }}</h1>
<h2>See the sources here: </h2>
<ul>
<li><a href="https://github.com/jonashackt/spring-boot-vuejs" target="_blank">github.com/jonashackt/spring-boot-vuejs</a></li>
Expand All @@ -21,12 +21,7 @@
<script>
export default {
name: 'hello',
data () {
return {
msg: 'Welcome to your Vue.js powered Spring Boot App'
}
}
props: { hellomsg: { type: String, required: true } }
}
</script>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/User.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div class="service">
<div class="user">
<h1>Create User</h1>

<h3>Just some database interaction...</h3>
Expand All @@ -23,7 +23,7 @@
import {AXIOS} from './http-common'
export default {
name: 'service',
name: 'user',
data () {
return {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/router/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Vue from 'vue'
import Router from 'vue-router'
import VueRouter from 'vue-router'
import Hello from '@/components/Hello'
import Service from '@/components/Service'
import Bootstrap from '@/components/Bootstrap'
import User from '@/components/User'

Vue.use(Router)
Vue.use(VueRouter)

export default new Router({
routes: [
Expand Down
4 changes: 1 addition & 3 deletions frontend/test/unit/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"env": {
"mocha": true
"jest": true
},
"globals": {
"expect": true,
"sinon": true
}
}
1 change: 0 additions & 1 deletion frontend/test/unit/jest.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module.exports = {
],
snapshotSerializers: ['<rootDir>/node_modules/jest-serializer-vue'],
setupFiles: ['<rootDir>/test/unit/setup'],
mapCoverage: true,
coverageDirectory: '<rootDir>/test/unit/coverage',
collectCoverageFrom: [
'src/**/*.{js,vue}',
Expand Down
17 changes: 17 additions & 0 deletions frontend/test/unit/specs/App.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { shallowMount, createLocalVue } from '@vue/test-utils';
import VueRouter from 'vue-router'
import App from '@/App';

const localVue = createLocalVue()
localVue.use(VueRouter)
const router = new VueRouter()

describe('App component should', () => {
it('render without crashing', () => {
const wrapper = shallowMount(App, {
localVue,
router
});
expect(wrapper.find('hello')).toBeDefined();
});
});
11 changes: 0 additions & 11 deletions frontend/test/unit/specs/Hello.spec.js

This file was deleted.

10 changes: 10 additions & 0 deletions frontend/test/unit/specs/components/Hello.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { shallowMount } from '@vue/test-utils';
import Hello from '@/components/Hello'

describe('Hello.vue', () => {
it('should render correct hello message', () => {
const wrapper = shallowMount(Hello, { propsData: { hellomsg: 'Welcome to your Jest powered Vue.js App' } });
const contentH1 = wrapper.find('h1');
expect(contentH1.text()).toEqual('Welcome to your Jest powered Vue.js App');
})
})
10 changes: 10 additions & 0 deletions frontend/test/unit/specs/components/User.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { shallowMount } from '@vue/test-utils';
import User from '@/components/User'

describe('User.vue', () => {
it('should render Create User Button', () => {
const wrapper = shallowMount(User);
const contentButton = wrapper.find('button');
expect(contentButton.text()).toEqual('Create User');
})
})

0 comments on commit f98d2ea

Please sign in to comment.