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/01-writing-tests.md
+7-44
Original file line number
Diff line number
Diff line change
@@ -10,9 +10,7 @@ AVA tries to run test files with their current working directory set to the dire
10
10
11
11
## Test isolation
12
12
13
-
AVA 3 runs each test file in a separate Node.js process. This allows you to change the global state or overriding a built-in in one test file, without affecting another.
14
-
15
-
AVA 4 runs each test file in a new worker thread, though you can fall back to AVA 3's behavior of running in separate processes.
13
+
Each test file is run in a new worker thread. This is new as of AVA 4, though you can fall back to AVA 3's behavior of running in separate processes.
16
14
17
15
AVA will set `process.env.NODE_ENV` to `test`, unless the `NODE_ENV` environment variable has been set. This is useful if the code you're testing has test defaults (for example when picking what database to connect to). It may cause your code or its dependencies to behave differently though. Note that `'NODE_ENV' in process.env` will always be `true`.
18
16
@@ -91,19 +89,6 @@ test('handles observables', t => {
91
89
});
92
90
```
93
91
94
-
## Callback support
95
-
96
-
*👉 AVA 4 removes support for `test.cb()` and `t.end()`.*
97
-
98
-
AVA 3 supports using `t.end` as the final callback when using Node.js-style error-first callback APIs. AVA will consider any truthy value passed as the first argument to `t.end` to be an error. Note that `t.end` requires "callback mode", which can be enabled by using the `test.cb` chain.
99
-
100
-
```js
101
-
test.cb('data.txt can be read', t=> {
102
-
// `t.end` automatically checks for error as first argument
103
-
fs.readFile('data.txt', t.end);
104
-
});
105
-
```
106
-
107
92
## Running specific tests
108
93
109
94
During development it can be helpful to only run a few specific tests. This can be accomplished using the `.only` modifier:
@@ -122,8 +107,6 @@ You can use the `.only` modifier with all tests. It cannot be used with hooks or
122
107
123
108
*Note:* The `.only` modifier applies to the test file it's defined in, so if you run multiple test files, tests in other files will still run. If you want to only run the `test.only` test, provide just that test file to AVA.
124
109
125
-
In AVA 3, you cannot update snapshots when using `.only()`.
126
-
127
110
## Skipping tests
128
111
129
112
Sometimes failing tests can be hard to fix. You can tell AVA to temporarily skip these tests using the `.skip` modifier. They'll still be shown in the output (as having been skipped) but are never run.
@@ -138,8 +121,6 @@ You must specify the implementation function. You can use the `.skip` modifier w
138
121
139
122
If the test is likely to be failing for a while, use `.failing()` instead.
140
123
141
-
In AVA 3, you cannot update snapshots when using `.skip()`.
142
-
143
124
## Test placeholders ("todo")
144
125
145
126
You can use the `.todo` modifier when you're planning to write a test. Like skipped tests these placeholders are shown in the output. They only require a title; you cannot specify the implementation function.
@@ -277,10 +258,8 @@ Access data about the currently loaded test file run by reading `test.meta`.
277
258
278
259
Available properties:
279
260
280
-
*`file`: path to the test file
281
-
*`snapshotDirectory`: directory where snapshots are stored
282
-
283
-
In AVA 4 these are file URL strings.
261
+
*`file`: path to the test file, as a file URL string
262
+
*`snapshotDirectory`: directory where snapshots are stored, as a file URL string
284
263
285
264
```js
286
265
consttest=require('ava');
@@ -294,7 +273,7 @@ console.log('Test file currently being run:', test.meta.file);
294
273
295
274
Additional arguments passed to the test declaration will be passed to the test implementation. This is useful for creating reusable test macros.
The `providedTitle` argument defaults to `undefined` if the user does not supply a string title. This means you can use a parameter assignment to set the default value. The example above uses the empty string as the default.
323
-
324
-
However with AVA 4 the preferred approach is to use the `test.macro()` helper:
287
+
However the preferred approach is to use the `test.macro()` helper:
We encourage you to use macros instead of building your own test generators ([here is an example](https://github.com/avajs/ava-codemods/blob/47073b5b58aa6f3fb24f98757be5d3f56218d160/test/ok-to-truthy.js#L7-L9) of code that should be replaced with a macro). Macros are designed to perform static analysis of your code, which can lead to better performance, IDE integration, and linter rules.
318
+
The `providedTitle` argument defaults to `undefined` if the user does not supply a string title. This means you can use a parameter assignment to set the default value. The example above uses the empty string as the default.
Copy file name to clipboardexpand all lines: docs/02-execution-context.md
+1-29
Original file line number
Diff line number
Diff line change
@@ -26,10 +26,6 @@ Contains shared state from hooks.
26
26
27
27
When used in `test.afterEach()` or `test.afterEach.always()` hooks this tells you whether the test has passed. When used in a test itself (including teardown functions) this remains `true` until an assertion fails, the test has ended with an error, or a teardown function caused an error. This value has no meaning in other hooks.
28
28
29
-
## `t.end()`
30
-
31
-
End the test. Only works with `test.cb()`. Removed in AVA 4.
32
-
33
29
## `t.log(...values)`
34
30
35
31
Log values contextually alongside the test result instead of immediately printing them to `stdout`. Behaves somewhat like `console.log`, but without support for placeholder tokens.
@@ -40,36 +36,12 @@ Plan how many assertions there are in the test. The test will fail if the actual
40
36
41
37
## `t.teardown(fn)`
42
38
43
-
Registers the `fn` function to be run after the test has finished. You can register multiple functions. In AVA 3 the functions are called in order, but in AVA 4 they'll run in _reverse_ order.<sup>†</sup>. You can use asynchronous functions: only one will run at a time.
39
+
Registers the `fn` function to be run after the test has finished. You can register multiple functions. They'll run in reverse order, so the most last registered function is run first. You can use asynchronous functions: only one will run at a time.
44
40
45
41
You cannot perform assertions using the `t` object or register additional functions from inside `fn`.
46
42
47
43
You cannot use `t.teardown()` in hooks either.
48
44
49
-
<sup>†</sup> You can opt in to this behavior in AVA 3 by enabling the `reverseTeardowns` experiment.
50
-
51
-
**`package.json`**:
52
-
53
-
```json
54
-
{
55
-
"ava": {
56
-
"nonSemVerExperiments": {
57
-
"reverseTeardowns": true
58
-
}
59
-
}
60
-
}
61
-
```
62
-
63
-
**`ava.config.js`**:
64
-
65
-
```js
66
-
exportdefault {
67
-
nonSemVerExperiments: {
68
-
reverseTeardowns:true
69
-
}
70
-
}
71
-
```
72
-
73
45
## `t.timeout(ms)`
74
46
75
47
Set a timeout for the test, in milliseconds. The test will fail if this timeout is exceeded. The timeout is reset each time an assertion is made.
Copy file name to clipboardexpand all lines: docs/03-assertions.md
+4-71
Original file line number
Diff line number
Diff line change
@@ -75,69 +75,6 @@ test('skip assertion', t => {
75
75
});
76
76
```
77
77
78
-
## Enhanced assertion messages
79
-
80
-
With AVA 3, enabling [Babel](./recipes/babel.md) will also enable [`power-assert`](https://github.com/power-assert-js/power-assert), giving you more descriptive assertion messages.
81
-
82
-
Let's take this example, using Node's standard [`assert` library](https://nodejs.org/api/assert.html):
83
-
84
-
```js
85
-
consta=/foo/;
86
-
constb='bar';
87
-
constc='baz';
88
-
require('assert').ok(a.test(b) || b === c);
89
-
```
90
-
91
-
If you paste that into a Node REPL it'll return:
92
-
93
-
```
94
-
AssertionError: false == true
95
-
```
96
-
97
-
With AVA's `assert` assertion however, this test:
98
-
99
-
```js
100
-
test('enhanced assertions', t=> {
101
-
consta=/foo/;
102
-
constb='bar';
103
-
constc='baz';
104
-
t.assert(a.test(b) || b === c);
105
-
});
106
-
```
107
-
108
-
Will output:
109
-
110
-
```
111
-
6: const c = 'baz';
112
-
7: t.assert(a.test(b) || b === c);
113
-
8: });
114
-
115
-
Value is not truthy:
116
-
117
-
false
118
-
119
-
a.test(b) || b === c
120
-
=> false
121
-
122
-
b === c
123
-
=> false
124
-
125
-
c
126
-
=> 'baz'
127
-
128
-
b
129
-
=> 'bar'
130
-
131
-
a.test(b)
132
-
=> false
133
-
134
-
b
135
-
=> 'bar'
136
-
137
-
a
138
-
=> /foo/
139
-
```
140
-
141
78
## Custom assertions
142
79
143
80
You can use any assertion library instead of or in addition to the built-in one, provided it throws exceptions when the assertion fails.
@@ -166,7 +103,7 @@ Failing assertion. Returns a boolean indicating whether the assertion passed.
166
103
167
104
### `.assert(value, message?)`
168
105
169
-
Asserts that `value` is truthy. This is [`power-assert`](#enhanced-assertion-messages) enabled. Returns a boolean indicating whether the assertion passed.
106
+
Asserts that `value` is truthy. Returns a boolean indicating whether the assertion passed.
170
107
171
108
### `.truthy(value, message?)`
172
109
@@ -194,7 +131,7 @@ Assert that `value` is not the same as `expected`. This is based on [`Object.is(
194
131
195
132
### `.deepEqual(value, expected, message?)`
196
133
197
-
Assert that `value` is deeply equal to `expected`. See [Concordance](https://github.com/concordancejs/concordance) for details. In AVA 3 this works with [React elements and `react-test-renderer`](https://github.com/concordancejs/react).
134
+
Assert that `value` is deeply equal to `expected`. See [Concordance](https://github.com/concordancejs/concordance) for details.
198
135
199
136
### `.notDeepEqual(value, expected, message?)`
200
137
@@ -239,7 +176,7 @@ Assert that an error is thrown. `fn` must be a function which should throw. The
239
176
*`name`: the expected `.name` value of the thrown error
240
177
*`code`: the expected `.code` value of the thrown error
241
178
242
-
`expectation` does not need to be specified. If you don't need it but do want to set an assertion message you have to specify `undefined`. (AVA 3 also allows you to specify `null`. This will be removed in AVA 4. You can opt into this change early by enabling the `disableNullExpectations` experiment.)
179
+
`expectation` does not need to be specified. If you don't need it but do want to set an assertion message you have to specify `undefined`.
243
180
244
181
Example:
245
182
@@ -271,7 +208,7 @@ The thrown value *must* be an error. It is returned so you can run more assertio
271
208
*`name`: the expected `.name` value of the thrown error
272
209
*`code`: the expected `.code` value of the thrown error
273
210
274
-
`expectation` does not need to be specified. If you don't need it but do want to set an assertion message you have to specify `undefined`. (AVA 3 also allows you to specify `null`. This will be removed in AVA 4. You can opt into this change early by enabling the `disableNullExpectations` experiment.)
211
+
`expectation` does not need to be specified. If you don't need it but do want to set an assertion message you have to specify `undefined`.
275
212
276
213
Example:
277
214
@@ -322,10 +259,6 @@ Assert that `contents` does not match `regex`. Returns a boolean indicating whet
322
259
323
260
Compares the `expected` value with a previously recorded snapshot. Snapshots are stored for each test, so ensure you give your tests unique titles.
324
261
325
-
AVA 3 supports an `options` object that lets you select a specific snapshot, for instance `{id: 'my snapshot'}`. This is buggy and will be removed in AVA 4.
326
-
327
-
In AVA 3, you cannot update snapshots while using `t.snapshot.skip()`.
AVA supports snapshot testing, [as introduced by Jest](https://facebook.github.io/jest/docs/snapshot-testing.html), through its [Assertions](./03-assertions.md) interface. You can snapshot any value. In AVA 3 you can also snapshot React elements:
6
-
7
-
```js
8
-
// Your component
9
-
constHelloWorld= () =><h1>Hello World...!</h1>;
10
-
11
-
exportdefaultHelloWorld;
12
-
```
13
-
14
-
```js
15
-
// Your test
16
-
consttest=require('ava');
17
-
constrender=require('react-test-renderer');
18
-
constHelloWorld=require('.');
19
-
20
-
test('HelloWorld component', t=> {
21
-
consttree=render.create(<HelloWorld/>).toJSON();
22
-
t.snapshot(tree);
23
-
});
24
-
```
25
-
26
-
[Try it out in this example project.](https://github.com/avajs/ava-snapshot-example)
5
+
AVA supports snapshot testing, [as introduced by Jest](https://facebook.github.io/jest/docs/snapshot-testing.html), through its [Assertions](./03-assertions.md) interface. You can snapshot any value.
27
6
28
7
Snapshots are stored alongside your test files. If your tests are in a `test` or `tests` folder the snapshots will be stored in a `snapshots` folder. If your tests are in a `__tests__` folder then they they'll be stored in a `__snapshots__` folder.
29
8
@@ -44,7 +23,7 @@ You can then check your code. If the change was intentional you can use the `--u
44
23
$ ava --update-snapshots
45
24
```
46
25
47
-
In AVA 4, if you need to update snapshots for only a particular test, you can use `--update-snapshots` together with e.g. `--match` or `.only()` to select the test.
26
+
If you need to update snapshots for only a particular test, you can use `--update-snapshots` together with e.g. `--match` or `.only()` to select the test.
48
27
49
28
You can specify a fixed location for storing the snapshot files in AVA's [`package.json` configuration](./06-configuration.md):
--watch, -w Re-run tests when files change [boolean]
45
45
46
46
Examples:
@@ -49,8 +49,6 @@ Examples:
49
49
ava test.js:4,7-9
50
50
```
51
51
52
-
*Note that, for AVA 3, the CLI will use your local install of AVA when available, even when run globally. AVA 4 cannot be run globally.*
53
-
54
52
AVA searches for test files using the following patterns:
55
53
56
54
*`test.js`
@@ -164,7 +162,7 @@ AVA lets you run tests exclusively by referring to their line numbers. Target a
164
162
165
163
The format is a comma-separated list of `[X|Y-Z]` where `X`, `Y` and `Z` are integers between `1` and the last line number of the file.
166
164
167
-
This feature is only available from the command line. It won't work if you use tools like `ts-node/register` or `@babel/register`, and it does not currently work with `@ava/babel` (available for AVA 3) and `@ava/typescript`.
165
+
This feature is only available from the command line.
168
166
169
167
### Running a single test
170
168
@@ -220,7 +218,7 @@ When running a file with and without line numbers, line numbers take precedence.
220
218
221
219
## Resetting AVA's cache
222
220
223
-
AVA 3 itself does not cache files unless used with our [`@ava/babel`](https://github.com/avajs/babel) provider. If it seems like your latest changes aren't being picked up by AVA you can try resetting the cache by running:
221
+
AVA maintains some temporary state. You can clear this state by running:
224
222
225
223
```console
226
224
npx ava reset-cache
@@ -230,16 +228,10 @@ This deletes all files in the `node_modules/.cache/ava` directory.
Use the `--verbose` flag to enable the verbose reporter. This is always used in CI environments unless the [TAP reporter](#tap-reporter) is enabled.
242
-
243
235
### TAP reporter
244
236
245
237
[](https://stackblitz.com/github/avajs/ava/tree/main/examples/tap-reporter?file=test.js&terminal=test&view=editor)
0 commit comments