Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
Fix the Jest configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Jan 26, 2020
1 parent ee82b45 commit 9a16b3d
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ yarn-error.log*
# Template unwanted files
/template/.nuxt/
/template/dist/
/template/package-lock.json
/template/package*.json
/template/static/sw*
27 changes: 14 additions & 13 deletions template/.babelrc.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
module.exports = {
"env": {
"test": {
"presets": [
env: {
test: {
presets: [
[
"@babel/preset-env",
'@babel/preset-env',
{
"targets": {
"node": "current"
}
}
]
]
}
}
}
targets: {
node: 'current',
},
},
],
],
plugins: ['require-context-hook'],
},
},
};
1 change: 1 addition & 0 deletions template/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@vue/test-utils": "^1.0.0-beta.27",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.1.0",
"babel-plugin-require-context-hook": "^1.0.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^4.1.0",
"eslint-import-resolver-alias": "^1.1.2",
Expand Down
17 changes: 17 additions & 0 deletions template/components/Icon.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<template>
<component :is="name" class="icon" />
</template>

<script>
import components from '~/utils/get-icons';
export default {
components,
props: {
name: {
type: String,
required: true,
},
},
};
</script>
21 changes: 19 additions & 2 deletions template/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = {
projects: [
{
displayName: 'Test',
setupFiles: ['<rootDir>/test/__setup__/require-context.js'],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/$1',
'^~/(.*)$': '<rootDir>/$1',
Expand All @@ -17,13 +19,28 @@ module.exports = {
{
runner: 'jest-runner-eslint',
displayName: 'ESLint',
testMatch: ['<rootDir>/**/*.vue', '<rootDir>/**/*.js', '!<rootDir>/dist'],
moduleFileExtensions: ['js', 'vue'],
testMatch: [
'<rootDir>/components/**/*.vue',
'<rootDir>/layouts/**/*.vue',
'<rootDir>/pages/**/*.vue',
'<rootDir>/middleware/**/*.js',
'<rootDir>/plugins/**/*.js',
'<rootDir>/store/**/*.js',
'<rootDir>/test/**/*.js',
'<rootDir>/utils/**/*.js',
],
},
{
runner: 'jest-runner-stylelint',
displayName: 'StyleLint',
moduleFileExtensions: ['css', 'scss', 'vue'],
testMatch: ['<rootDir>/**/*.vue', '<rootDir>/**/*.scss', '!<rootDir>/dist'],
testMatch: [
'<rootDir>/components/**/*.vue',
'<rootDir>/layouts/**/*.vue',
'<rootDir>/pages/**/*.vue',
'<rootDir>/assets/scss/**/*.scss',
],
},
],
};
4 changes: 1 addition & 3 deletions template/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export default {
/*
** Plugins to load before mounting the App
*/
plugins: [
'~/plugins/vue-icon',
],
plugins: ['~/plugins/vue-icon'],
/*
** Nuxt.js dev-modules
*/
Expand Down
32 changes: 3 additions & 29 deletions template/plugins/vue-icon.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
/**
* Inject a global `VueIcon` component.
* Inject the `VueIcon` component globally.
*/
import Vue from 'vue';
import VueIcon from '~/components/Icon';

const files = require.context('~/assets/svg', true, /[\s\S]*$/);
const components = files.keys().reduce((icons, path) => {
const file = path.replace('./', '');
const key = file.replace('.svg', '');
icons[key] = async () => import(`~/assets/svg/${file}`);
return icons;
}, {});

Vue.component('vue-icon', {
components,
props: {
/**
* The name of the icon.
*/
name: {
type: String,
required: true,
},
},
render(h) {
console.log(this);
return h(this.name, {
class: 'icon',
on: this.$listeners,
attr: this.$attrs,
});
},
});
Vue.component('vue-icon', VueIcon);
10 changes: 7 additions & 3 deletions template/test/Logo.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { mount } from '@vue/test-utils';
import Logo from '@/components/Logo';
import { createLocalVue, shallowMount } from '@vue/test-utils';
import VueIcon from '~/components/Icon';
import Logo from '~/components/Logo';

const localVue = createLocalVue();
localVue.component('vue-icon', VueIcon);

describe('Logo', () => {
test('is a Vue instance', () => {
const wrapper = mount(Logo);
const wrapper = shallowMount(Logo, { localVue });
expect(wrapper.isVueInstance()).toBeTruthy();
});
});
3 changes: 3 additions & 0 deletions template/test/__setup__/require-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import registerRequireContextHook from 'babel-plugin-require-context-hook/register';

registerRequireContextHook();
9 changes: 9 additions & 0 deletions template/utils/get-icons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const files = require.context('../assets/svg/', true, /\.svg$/);
const components = files.keys().reduce((icons, path) => {
const file = path.replace('./', '');
const key = file.replace('.svg', '');
icons[key] = async () => import(`~/assets/svg/${file}`);
return icons;
}, {});

export default components;

0 comments on commit 9a16b3d

Please sign in to comment.