Skip to content

Commit 58cf248

Browse files
committed
Change test.meta properties to be file URLs
1 parent 35994d4 commit 58cf248

File tree

10 files changed

+113
-81
lines changed

10 files changed

+113
-81
lines changed

docs/01-writing-tests.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ Available properties:
280280
* `file`: path to the test file
281281
* `snapshotDirectory`: directory where snapshots are stored
282282

283+
In AVA 4 these are file URL strings.
284+
283285
```js
284286
const test = require('ava');
285287

lib/runner.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {pathToFileURL} from 'url';
2+
13
import Emittery from 'emittery';
24
import matcher from 'matcher';
35

@@ -9,6 +11,7 @@ import {load as loadSnapshots, determineSnapshotDir} from './snapshot-manager.js
911
import Runnable from './test.js';
1012
import {waitForReady} from './worker/state.cjs';
1113

14+
const makeFileURL = file => file.startsWith('file://') ? file : pathToFileURL(file).toString();
1215
export default class Runner extends Emittery {
1316
constructor(options = {}) {
1417
super();
@@ -76,10 +79,10 @@ export default class Runner extends Emittery {
7679
let hasStarted = false;
7780
let scheduledStart = false;
7881
const meta = Object.freeze({
79-
file: options.file,
82+
file: makeFileURL(options.file),
8083
get snapshotDirectory() {
8184
const {file, snapshotDir: fixedLocation, projectDir} = options;
82-
return determineSnapshotDir({file, fixedLocation, projectDir});
85+
return makeFileURL(determineSnapshotDir({file, fixedLocation, projectDir}));
8386
}
8487
});
8588
this.chain = createChain((metadata, testArgs) => { // eslint-disable-line complexity

test-tap/api.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,6 @@ const opts = [
3737
];
3838

3939
for (const opt of opts) {
40-
test(`test.meta - workerThreads: ${opt.workerThreads}`, async t => {
41-
const api = await apiCreator({
42-
...opt,
43-
snapshotDir: 'snapshot-fixture'
44-
});
45-
return api.run({files: [path.join(__dirname, 'fixture', 'meta.cjs')]})
46-
.then(runStatus => {
47-
t.equal(runStatus.stats.passedTests, 2);
48-
});
49-
});
50-
5140
test(`fail-fast mode - workerThreads: ${opt.workerThreads} - single file & serial`, async t => {
5241
const api = await apiCreator({
5342
...opt,

test-tap/fixture/meta.cjs

Lines changed: 0 additions & 10 deletions
This file was deleted.

test-tap/hooks.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test('before', t => {
1616
t.plan(1);
1717

1818
const array = [];
19-
return promiseEnd(new Runner(), runner => {
19+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
2020
runner.chain.before(() => {
2121
array.push('a');
2222
});
@@ -34,7 +34,7 @@ test('after', t => {
3434
t.plan(2);
3535

3636
const array = [];
37-
return promiseEnd(new Runner(), runner => {
37+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
3838
runner.on('stateChange', evt => {
3939
if (evt.type === 'test-passed') {
4040
t.pass();
@@ -58,7 +58,7 @@ test('after not run if test failed', t => {
5858
t.plan(2);
5959

6060
const array = [];
61-
return promiseEnd(new Runner(), runner => {
61+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
6262
runner.on('stateChange', evt => {
6363
if (evt.type === 'test-failed') {
6464
t.pass();
@@ -81,7 +81,7 @@ test('after.always run even if test failed', t => {
8181
t.plan(2);
8282

8383
const array = [];
84-
return promiseEnd(new Runner(), runner => {
84+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
8585
runner.on('stateChange', evt => {
8686
if (evt.type === 'test-failed') {
8787
t.pass();
@@ -104,7 +104,7 @@ test('after.always run even if before failed', t => {
104104
t.plan(1);
105105

106106
const array = [];
107-
return promiseEnd(new Runner(), runner => {
107+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
108108
runner.chain.before(() => {
109109
throw new Error('something went wrong');
110110
});
@@ -123,7 +123,7 @@ test('stop if before hooks failed', t => {
123123
t.plan(1);
124124

125125
const array = [];
126-
return promiseEnd(new Runner(), runner => {
126+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
127127
runner.chain.before(() => {
128128
array.push('a');
129129
});
@@ -146,7 +146,7 @@ test('before each with concurrent tests', t => {
146146
t.plan(1);
147147

148148
const array = [[], []];
149-
return promiseEnd(new Runner(), runner => {
149+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
150150
let i = 0;
151151
let k = 0;
152152

@@ -176,7 +176,7 @@ test('before each with serial tests', t => {
176176
t.plan(1);
177177

178178
const array = [];
179-
return promiseEnd(new Runner(), runner => {
179+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
180180
runner.chain.beforeEach(() => {
181181
array.push('a');
182182
});
@@ -203,7 +203,7 @@ test('fail if beforeEach hook fails', t => {
203203
t.plan(2);
204204

205205
const array = [];
206-
return promiseEnd(new Runner(), runner => {
206+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
207207
runner.on('stateChange', evt => {
208208
if (evt.type === 'hook-failed') {
209209
t.pass();
@@ -228,7 +228,7 @@ test('after each with concurrent tests', t => {
228228
t.plan(1);
229229

230230
const array = [[], []];
231-
return promiseEnd(new Runner(), runner => {
231+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
232232
let i = 0;
233233
let k = 0;
234234

@@ -258,7 +258,7 @@ test('after each with serial tests', t => {
258258
t.plan(1);
259259

260260
const array = [];
261-
return promiseEnd(new Runner(), runner => {
261+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
262262
runner.chain.afterEach(() => {
263263
array.push('a');
264264
});
@@ -285,7 +285,7 @@ test('afterEach not run if concurrent tests failed', t => {
285285
t.plan(1);
286286

287287
const array = [];
288-
return promiseEnd(new Runner(), runner => {
288+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
289289
runner.chain.afterEach(() => {
290290
array.push('a');
291291
});
@@ -302,7 +302,7 @@ test('afterEach not run if serial tests failed', t => {
302302
t.plan(1);
303303

304304
const array = [];
305-
return promiseEnd(new Runner(), runner => {
305+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
306306
runner.chain.afterEach(() => {
307307
array.push('a');
308308
});
@@ -319,7 +319,7 @@ test('afterEach.always run even if concurrent tests failed', t => {
319319
t.plan(1);
320320

321321
const array = [];
322-
return promiseEnd(new Runner(), runner => {
322+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
323323
runner.chain.afterEach.always(() => {
324324
array.push('a');
325325
});
@@ -336,7 +336,7 @@ test('afterEach.always run even if serial tests failed', t => {
336336
t.plan(1);
337337

338338
const array = [];
339-
return promiseEnd(new Runner(), runner => {
339+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
340340
runner.chain.afterEach.always(() => {
341341
array.push('a');
342342
});
@@ -353,7 +353,7 @@ test('afterEach.always run even if beforeEach failed', t => {
353353
t.plan(1);
354354

355355
const array = [];
356-
return promiseEnd(new Runner(), runner => {
356+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
357357
runner.chain.beforeEach(() => {
358358
throw new Error('something went wrong');
359359
});
@@ -376,7 +376,7 @@ test('afterEach: property `passed` of execution-context is false when test faile
376376

377377
const passed = [];
378378
let i;
379-
return promiseEnd(new Runner(), runner => {
379+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
380380
runner.chain.afterEach(a => {
381381
passed[i] = a.passed;
382382
});
@@ -399,7 +399,7 @@ test('afterEach.always: property `passed` of execution-context is false when tes
399399

400400
const passed = [];
401401
let i;
402-
return promiseEnd(new Runner(), runner => {
402+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
403403
runner.chain.afterEach.always(a => {
404404
passed[i] = a.passed;
405405
});
@@ -421,7 +421,7 @@ test('afterEach.always: property `passed` of execution-context is false when bef
421421
t.plan(1);
422422

423423
let passed;
424-
return promiseEnd(new Runner(), runner => {
424+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
425425
runner.chain.before(() => {
426426
throw new Error('something went wrong');
427427
});
@@ -440,7 +440,7 @@ test('afterEach.always: property `passed` of execution-context is true when test
440440
t.plan(1);
441441

442442
let passed;
443-
return promiseEnd(new Runner(), runner => {
443+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
444444
runner.chain.afterEach(() => {
445445
throw new Error('something went wrong');
446446
});
@@ -459,7 +459,7 @@ test('ensure hooks run only around tests', t => {
459459
t.plan(1);
460460

461461
const array = [];
462-
return promiseEnd(new Runner(), runner => {
462+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
463463
runner.chain.beforeEach(() => {
464464
array.push('beforeEach');
465465
});
@@ -486,7 +486,7 @@ test('ensure hooks run only around tests', t => {
486486
});
487487

488488
test('shared context', t => {
489-
return promiseEnd(new Runner(), runner => {
489+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
490490
runner.on('stateChange', evt => {
491491
if (evt.type === 'hook-failed' || evt.type === 'test-failed') {
492492
t.fail();
@@ -529,7 +529,7 @@ test('shared context', t => {
529529
});
530530

531531
test('shared context of any type', t => {
532-
return promiseEnd(new Runner(), runner => {
532+
return promiseEnd(new Runner({file: import.meta.url}), runner => {
533533
runner.on('stateChange', evt => {
534534
if (evt.type === 'hook-failed' || evt.type === 'test-failed') {
535535
t.fail();
@@ -549,7 +549,7 @@ test('shared context of any type', t => {
549549

550550
test('teardowns cannot be used in hooks', async t => {
551551
let hookFailure = null;
552-
await promiseEnd(new Runner(), runner => {
552+
await promiseEnd(new Runner({file: import.meta.url}), runner => {
553553
runner.on('stateChange', evt => {
554554
if (evt.type === 'hook-failed') {
555555
hookFailure = evt;

0 commit comments

Comments
 (0)