Skip to content

Commit 1222ce9

Browse files
ulkennovemberborn
andauthoredApr 26, 2020
Run tests at selected line numbers
Co-Authored-By: Mark Wubben <mark@novemberborn.net>
1 parent 75cbc3b commit 1222ce9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+762
-73
lines changed
 

‎docs/05-command-line.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Commands:
1515

1616
Positionals:
1717
pattern Glob patterns to select what test files to run. Leave empty if you
18-
want AVA to run all test files instead [string]
18+
want AVA to run all test files instead. Add a colon and specify line
19+
numbers of specific tests to run [string]
1920

2021
Options:
2122
--version Show version number [boolean]
@@ -42,6 +43,7 @@ Options:
4243
Examples:
4344
ava
4445
ava test.js
46+
ava test.js:4,7-9
4547
```
4648

4749
*Note that the CLI will use your local install of AVA when available, even when run globally.*
@@ -149,6 +151,66 @@ test(function foo(t) {
149151
});
150152
```
151153

154+
## Running tests at specific line numbers
155+
156+
AVA lets you run tests exclusively by referring to their line numbers. Target a single line, a range of lines or both. You can select any line number of a test.
157+
158+
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.
159+
160+
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` and `@ava/typescript`.
161+
162+
### Running a single test
163+
164+
To only run a particular test in a file, append the line number of the test to the path or pattern passed to AVA.
165+
166+
Given the following test file:
167+
168+
`test.js`
169+
170+
```js
171+
1: test('unicorn', t => {
172+
2: t.pass();
173+
3: });
174+
4:
175+
5: test('rainbow', t => {
176+
6: t.fail();
177+
7: });
178+
```
179+
180+
Running `npx ava test.js:2` for would run the `unicorn` test. In fact you could use any line number between `1` and `3`.
181+
182+
### Running multiple tests
183+
184+
To run multiple tests, either target them one by one or select a range of line numbers. As line numbers are given per file, you can run multiple files with different line numbers for each file. If the same file is provided multiple times, line numbers are merged and only run once.
185+
186+
### Examples
187+
188+
Single line numbers:
189+
190+
```console
191+
npx ava test.js:2,9
192+
```
193+
194+
Range:
195+
196+
```console
197+
npx ava test.js:4-7
198+
```
199+
200+
Mix of single line number and range:
201+
202+
```console
203+
npx ava test.js:4,9-12
204+
```
205+
206+
Different files:
207+
208+
```console
209+
npx ava test.js:3 test2.js:4,7-9
210+
```
211+
212+
When running a file with and without line numbers, line numbers take precedence.
213+
152214
## Resetting AVA's cache
153215

154216
AVA may cache certain files, especially when you use 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 reset the cache by running:

‎lib/api.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const isCi = require('./is-ci');
1616
const RunStatus = require('./run-status');
1717
const fork = require('./fork');
1818
const serializeError = require('./serialize-error');
19+
const {getApplicableLineNumbers} = require('./line-numbers');
1920

2021
function resolveModules(modules) {
2122
return arrify(modules).map(name => {
@@ -118,7 +119,11 @@ class Api extends Emittery {
118119
if (filter.length === 0) {
119120
selectedFiles = testFiles;
120121
} else {
121-
selectedFiles = globs.applyTestFileFilter({cwd: this.options.projectDir, filter, testFiles});
122+
selectedFiles = globs.applyTestFileFilter({
123+
cwd: this.options.projectDir,
124+
filter: filter.map(({pattern}) => pattern),
125+
testFiles
126+
});
122127
}
123128
}
124129
} catch (error) {
@@ -209,9 +214,11 @@ class Api extends Emittery {
209214
return;
210215
}
211216

217+
const lineNumbers = getApplicableLineNumbers(globs.normalizeFileForMatching(apiOptions.projectDir, file), filter);
212218
const options = {
213219
...apiOptions,
214220
providerStates,
221+
lineNumbers,
215222
recordNewSnapshots: !isCi,
216223
// If we're looking for matches, run every single test process in exclusive-only mode
217224
runOnlyExclusive: apiOptions.match.length > 0 || runtimeOptions.runOnlyExclusive === true
@@ -223,7 +230,7 @@ class Api extends Emittery {
223230
}
224231

225232
const worker = fork(file, options, apiOptions.nodeArguments);
226-
runStatus.observeWorker(worker, file);
233+
runStatus.observeWorker(worker, file, {selectingLines: lineNumbers.length > 0});
227234

228235
pendingWorkers.add(worker);
229236
worker.promise.then(() => {

0 commit comments

Comments
 (0)
Please sign in to comment.