Skip to content

Commit 6da1886

Browse files
committed
Initialize plugin
1 parent 857864b commit 6da1886

7 files changed

+5734
-0
lines changed

README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# vue-cli-plugin-unit-karmajs
2+
3+
Run unit tests in a @vue/cli project with Karma and Mocha
4+
5+
## Install
6+
7+
```bash
8+
$ vue add vue-cli-plugin-unit-karmajs
9+
```
10+
11+
If you want to install it using npm:
12+
13+
```bash
14+
$ npm install vue-cli-plugin-unit-karmajs
15+
$ vue invoke vue-cli-plugin-unit-karmajs
16+
```
17+
18+
If you don't want to use `vue invoke`, install required devDependencies which are present in `generator/index.js`.
19+
20+
## Usage
21+
22+
`vue-cli-service test:unit [options] [...files]`
23+
24+
### Injected commands:
25+
* `vue-cli-service test:unit`
26+
27+
Default files matches are: any files in tests/unit that end in `.spec.js`.
28+
29+
Command line options:
30+
31+
* `--watch, -w`: run in watch mode
32+
33+
NOTE: If you want to override default karma settings, you can use the `pluginOptions.karma` key in `vue.config.js` like this:
34+
35+
```javascript
36+
pluginOptions: {
37+
karma: {
38+
files: [ 'tests/**/*.spec.js' ]
39+
}
40+
}
41+
```
42+
43+
This module is inspired from [vue-cli-plugin-unit-karma] which is not actively maintained now.
44+
45+
46+
[vue-cli-plugin-unit-karma]: https://github.com/davidwallacejackson/vue-cli-plugin-unit-karma

default-karma-config.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
/**
3+
* Default config for karma
4+
*/
5+
6+
module.exports = {
7+
files: [
8+
'tests/unit/**/*.spec.js'
9+
],
10+
11+
reporters: [ 'mocha' ],
12+
13+
browsers: [ 'Chrome', 'Firefox' ],
14+
15+
frameworks: [ 'mocha', 'chai' ],
16+
};

generator/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = (api, _, __, invoking) => {
2+
api.extendPackage( {
3+
devDependencies: {
4+
"@vue/test-utils": "^1.0.0-beta.29",
5+
"chai": "^4.2.0",
6+
"mocha": "^6.2.0"
7+
},
8+
scripts: {
9+
"test:unit": "vue-cli-service test:unit"
10+
}
11+
} );
12+
};

index.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module.exports = ( api, projectOptions ) => {
2+
let karmaOptions = require( './default-karma-config' );
3+
4+
if ( projectOptions.pluginOptions && projectOptions.pluginOptions.karma ) {
5+
karmaOptions = Object.assign( karmaOptions, projectOptions.pluginOptions.karma );
6+
}
7+
8+
api.chainWebpack( webpackConfig => {
9+
if ( process.env.NODE_ENV === 'test' ) {
10+
webpackConfig.merge( {
11+
target: 'node',
12+
devtool: 'inline-cheap-module-source-map'
13+
} );
14+
15+
// when target === 'node', vue-loader will attempt to generate
16+
// SSR-optimized code. We need to turn that off here.
17+
webpackConfig.module
18+
.rule( 'vue' )
19+
.use( 'vue-loader' )
20+
.tap( options => {
21+
options.optimizeSSR = false;
22+
return options;
23+
} );
24+
}
25+
} );
26+
27+
api.registerCommand( 'test:unit', {
28+
description: 'Run unit tests with karma',
29+
usage: 'vue-cli-service test:unit [options] [...files]',
30+
}, ( args ) => {
31+
const webpackConfig = api.resolveWebpackConfig();
32+
33+
process.env.VUE_CLI_BABEL_TARGET_NODE = true
34+
process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true
35+
36+
return new Promise( ( resolve, reject ) => {
37+
let KarmaServer = require( 'karma' ).Server;
38+
let generateKarmaConfig = require( './karma.conf' );
39+
40+
let karmaServer = new KarmaServer(
41+
generateKarmaConfig( {
42+
webpackConfig,
43+
karmaOptions,
44+
watch: args.watch || args.w
45+
} ), ( exitCode ) => {
46+
console.log( `Karma exited with exitCode ${exitCode}` );
47+
48+
if ( exitCode === 0 ) {
49+
resolve( exitCode );
50+
} else {
51+
reject( exitCode );
52+
}
53+
}
54+
);
55+
56+
karmaServer.start();
57+
} );
58+
}
59+
);
60+
};

karma.conf.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const karmaConstants = require('karma').constants;
2+
const merge = require('webpack-merge');
3+
4+
module.exports = ( { webpackConfig, karmaOptions, watch } ) => {
5+
delete webpackConfig.entry;
6+
webpackConfig = merge( webpackConfig, {
7+
devtool: 'inline-source-map'
8+
} );
9+
10+
const preprocessors = {};
11+
12+
karmaOptions.files.map( fileNameOrPattern => {
13+
preprocessors[ fileNameOrPattern ] = [ 'webpack' ];
14+
} );
15+
16+
let karmaConfig = {
17+
files: karmaOptions.files,
18+
19+
reporters: karmaOptions.reporters,
20+
21+
logLevel: karmaConstants.LOG_ERROR,
22+
23+
autoWatch: watch,
24+
25+
singleRun: !watch,
26+
27+
browsers: karmaOptions.browsers,
28+
29+
frameworks: karmaOptions.frameworks,
30+
31+
preprocessors,
32+
33+
webpack: webpackConfig,
34+
35+
webpackMiddleware: {
36+
stats: 'minimal'
37+
}
38+
};
39+
40+
return karmaConfig;
41+
};

0 commit comments

Comments
 (0)