Skip to content

Commit d1da8e1

Browse files
authored
Merge pull request #4 from dnlup/docs
docs: document plugin behavior
2 parents c052fd5 + 606a8f9 commit d1da8e1

File tree

1 file changed

+125
-5
lines changed

1 file changed

+125
-5
lines changed

README.md

Lines changed: 125 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@
66

77
> unit-ava plugin for vue-cli
88
9-
## Injected commands
9+
###### Note
10+
This plugin is still in development so any feedback is greatly appreciated.
11+
12+
## Table of contents
13+
* [Injected commands](#injected-commands)
14+
* [Installing in an Already Created Project](#installing-in-an-already-created-project)
15+
* [How it works](#how-it-works)
16+
* [ava configuration](#ava-configuration)
17+
* [webpack configuration](#webpack-configuration)
18+
* [Running tests](#running-tests)
19+
* [Why all of this?](#why-all-of-this?)
20+
* [Contributing](#contributing)
21+
22+
### Injected commands
1023

1124
- **`vue-cli-service test:unit`**
1225

@@ -36,16 +49,123 @@
3649

3750
All [command line options](https://github.com/avajs/ava/blob/master/docs/05-command-line.md) are supported, the only one that should not work is `--reset-cache` since `ava` doesn't compile the test files, `webpack` does.
3851

39-
## Installing in an Already Created Project
52+
### Installing in an Already Created Project
4053

4154
```bash
4255
vue add @dnlup/unit-ava
4356
```
4457

45-
## Notes
46-
Compilation of tests files is done with webpack and the artifacts are saved in the `dist_tests` directory in the project root (this directory is added to your `.gitignore`).
58+
Once installed, calling `ava` directly will not work anymore, see [how to run tests](#running-tests).
59+
60+
### How it works
61+
This plugin aims to setup an environment where test files are compiled with `webpack` and the tests are run with ava using the compiled files. I had to make some assumptions to configure `ava` to work out of the box with the `webpack` setup enforced in `vue` projects.
62+
63+
#### ava configuration
64+
This is the configuration that will be generated for `ava` in `package.json`:
65+
66+
```json
67+
{
68+
"ava": {
69+
"files": [
70+
"dist_tests/tests/**/*.js"
71+
],
72+
"sources": [
73+
"!**/*.{js,jsx,ts,vue}"
74+
],
75+
"babel": false,
76+
"compileEnhancements": false,
77+
"require": "./node_modules/@dnlup/vue-cli-plugin-unit-ava/setup.js"
78+
}
79+
}
80+
```
81+
82+
for a reference of all the options see https://github.com/avajs/ava/blob/master/docs/06-configuration.md#options).
83+
84+
##### `files`
85+
This setup `ava` to run tests on the compiled files which are saved by `webpack` in the `dist_tests` folder in the root of your project. ***This value should not be changed at the moment since the output path of `webpack` is hard-coded***. This will have to change of course.
86+
87+
##### `sources`
88+
This is required to make the `--watch` option work properly. I am excluding every file except the compiled ones, otherwise on a file change the test will run multiple times. ***This value should not be changed***.
89+
90+
##### `babel`
91+
This is required to prevent `ava` to compile test files. ***This value should not be changed***.
92+
93+
##### `compileEnhancements`
94+
This disable `power-assert` but you should be able to safely enable it.
95+
96+
##### `require`
97+
This is ***required to setup `jsdom`*** in tests. The purpose of this file is ***not to setup node require hooks*** as suggested in [this recipe](https://github.com/avajs/ava/blob/master/docs/recipes/vue.md), so if you are using a setup file just for that you can safely let the plugin override your setting. If instead you still have to use your own setup file for other reasons, you can create a file like the following:
98+
99+
```js
100+
require('@dnlup/vue-cli-plugin-ava/setup')
101+
102+
//...your code....//
103+
```
104+
105+
and change the value of this field to point to your custom file.
106+
107+
#### webpack configuration
108+
I am modifying the `entry`, `output` and `plugins` field of the confifuration.
109+
110+
##### `entry`
111+
```js
112+
{
113+
entry: {
114+
'tests/unit/<your_test_file>': '<file-path>'
115+
}
116+
}
117+
```
118+
Every test file is an entry that has as key its path (without the extension). This way `webpack` will generate a directory structure identical to the one of the source files.
119+
120+
##### `output`
121+
```js
122+
{
123+
output: {
124+
path: join(api.resolve(''), 'dist_tests'),
125+
filename: '[name].js',
126+
devtoolModuleFilenameTemplate: '[absolute-resource-path]',
127+
devtoolFallbackModuleFilenameTemplate: '[absolute-resource-path]'
128+
}
129+
}
130+
```
131+
* `path`
132+
133+
hardcoded to `dist_tests` directory in the root of your project
134+
135+
* `devtoolModuleFilenameTemplate` and `devtoolFallbackModuleFilenameTemplate`
136+
137+
setup to `[absolute-resource-path]` so that in devtools will not appear `webpack://` but the actual file path
138+
139+
##### `plugins`
140+
A custom plugin named `RewriteSourceMap` is added to rewrite the source-maps to have each source test file as the first element of the `sources` list field of the generated source-map. This will allow `ava` to use the correct path for saving snapshots.
141+
142+
#### `.gitignore`
143+
The `dist_tests` folder is added to your `.gitignore` file.
144+
145+
#### `package.json` scripts
146+
The `test:unit` script is added to your `scripts` section of the `package.json` file.
147+
See [injected commands](#injected-commands).
148+
149+
#### Runing tests
150+
In this configuration you have to call `vue-cli-service test:unit` to run tests. So if for example you want to run tests only on a specific file you would run:
151+
152+
```bash
153+
$ npm run test:unit -- <your file>
154+
```
155+
or
156+
```bash
157+
$ npx vue-cli-service test:unit <your file>
158+
```
159+
160+
See [injected commands](#injected-commands).
161+
162+
Calling `ava` directly would not work anymore.
163+
164+
#### Why all of this?
165+
I went for this setup because I think that in a `vue` project it is better to let webpack compile everything: you have already all your loaders that are working in `dev` mode.
166+
Using `require hooks` for simple cases should be equivalent, but if you start using packages that add other loaders to the webpack config things might get more complicated. I think [Vuetify](https://vuetifyjs.com/en/getting-started/quick-start) could be a good example. I am open to suggestion anyway, so feel free to chime in and propose alternatives. My end goal is having a `ava` plugin to run unit tests :smile: with `@vue/cli`.
47167

48-
## Contributing
168+
### Contributing
49169

50170
* Make your changes
51171
* Add them

0 commit comments

Comments
 (0)