You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/recipes/precompiling-with-webpack.md
+65-50
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,10 @@ The AVA [readme](https://github.com/avajs/ava#transpiling-imported-modules) ment
8
8
-[Multiple test files](#multiple-test-files)
9
9
10
10
### Single test file
11
+
11
12
This is the simplest use case. You might need this if you are [using aliases](https://github.com/avajs/ava/issues/1011).
12
13
13
-
###### webpack.config.js
14
+
###### `webpack.config.js`
14
15
15
16
```js
16
17
constpath=require('path');
@@ -25,11 +26,15 @@ module.exports = {
25
26
},
26
27
externals: [nodeExternals()],
27
28
module: {
28
-
rules: [{
29
-
test:/\.(js|jsx)$/,
30
-
use:'babel-loader',
31
-
options: { cacheDirectory:true }
32
-
}]
29
+
rules: [
30
+
{
31
+
test:/\.(js|jsx)$/,
32
+
use:'babel-loader',
33
+
options: {
34
+
cacheDirectory:true
35
+
}
36
+
}
37
+
]
33
38
}
34
39
};
35
40
```
@@ -39,6 +44,7 @@ The important bits are `target: 'node'`, which ignores Node.js-specific `require
39
44
You can now run `$ ava _build/test.js` to run the tests contained in this output.
40
45
41
46
### Multiple test files
47
+
42
48
Things are a little more complicated with multiple test files. We recommend [using babel-register](babelrc.md) until the performance penalty becomes too great.
43
49
44
50
The possible approaches are:
@@ -49,6 +55,7 @@ The possible approaches are:
49
55
-[Test against precompiled sources](#test-against-precompiled-sources)
50
56
51
57
#### Refer precompiled source in tests
58
+
52
59
Source files can be compiled to `_src` folder and referenced in tests. While this is less than elegant, it performs well and the workflow can be optimized with [`babel-cli` watch mode](https://babeljs.io/docs/usage/cli/#babel).
53
60
54
61
```js
@@ -59,16 +66,16 @@ import fresh from '../_src';
59
66
```
60
67
61
68
#### Single entry file
62
-
Multiple test files can be compiled into a single file. This may have the best performance but that does come at a cost. All tests will be in the same file, which can make it harder to know which test has failed, since AVA can't show the file name the test was originally in. You'll also lose [process isolation](https://github.com/avajs/ava#process-isolation).
63
69
64
-
###### webpack.config.js
70
+
Multiple test files can be compiled into a single file. This may have the best performance, but it does come at a cost. All tests will be in the same file, which can make it harder to know which test has failed, since AVA can't show the file name the test was originally in. You'll also lose [process isolation](https://github.com/avajs/ava#process-isolation).
We can ask webpack to generate multiple entry files. This helps retain file names so that error reports are easy to interpret. But each entry file gets it's own copy of the source files. This results in considerably larger file sizes. This can [perform quite poorly](https://github.com/avajs/ava/pull/1385#issuecomment-304684047) on the first execution.
142
151
143
-
###### webpack.config.js
152
+
We can ask webpack to generate multiple entry files. This helps retain file names so that error reports are easy to interpret. But each entry file gets its own copy of the source files. This results in considerably larger file sizes. This can [perform quite poorly](https://github.com/avajs/ava/pull/1385#issuecomment-304684047) on the first execution.
This is the most complicated to setup but performs quite well and also retains file names. In this approach, we use the `babel-cli` to compile the source files but preserve file structure. Require paths in tests are rewritten when compiling them in webpack. The following example is for a specific file structure. Depending on how your source and test files are organised, you might need to make changes.
0 commit comments