Skip to content

Commit bccd297

Browse files
coreyfarrellnovemberborn
authored andcommitted
Expose test file path within worker process
This adds `test.meta.file` which exposes the filename of the test being run by AVA. Fixes #1976
1 parent c3bcbf2 commit bccd297

File tree

7 files changed

+52
-2
lines changed

7 files changed

+52
-2
lines changed

docs/01-writing-tests.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,16 @@ test('context is unicorn', t => {
267267
});
268268
```
269269

270+
## Retrieving test meta data
271+
272+
Helper files can determine the filename of the test being run by reading `test.meta.file`. This eliminates the need to pass `__filename` from the test to helpers.
273+
274+
```js
275+
import test from 'ava';
276+
277+
console.log('Test currently being run: ', test.meta.file);
278+
```
279+
270280
## Reusing test logic through macros
271281

272282
Additional arguments passed to the test declaration will be passed to the test implementation. This is useful for creating reusable test macros.

index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ export interface TestInterface<Context = {}> {
468468
only: OnlyInterface<Context>;
469469
skip: SkipInterface<Context>;
470470
todo: TodoDeclaration;
471+
meta: MetaInterface;
471472
}
472473

473474
export interface AfterInterface<Context = {}> {
@@ -740,6 +741,11 @@ export interface TodoDeclaration {
740741
(title: string): void;
741742
}
742743

744+
export interface MetaInterface {
745+
/** Path to the test file being executed. */
746+
file: string;
747+
}
748+
743749
/** Call to declare a test, or chain to declare hooks or test modifiers */
744750
declare const test: TestInterface;
745751

@@ -776,6 +782,8 @@ export const skip: SkipInterface;
776782
/** Declare a test that should be implemented later. */
777783
export const todo: TodoDeclaration;
778784

785+
/** Meta data associated with the current process. */
786+
export const meta: MetaInterface;
779787

780788
/*
781789
Tail type from <https://github.com/tycho01/typical/blob/25f11ed92c960aab1ebbf47fd6ead9e0ae51d947/src/array/Tail.ts>.

index.js.flow

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ export interface SerialInterface<Context = {}> {
707707
only: OnlyInterface<Context>;
708708
skip: SkipInterface<Context>;
709709
todo: TodoDeclaration;
710+
meta: MetaInterface;
710711
}
711712

712713
export interface SkipInterface<Context = {}> {
@@ -725,6 +726,11 @@ export interface TodoDeclaration {
725726
(title: string): void;
726727
}
727728

729+
export interface MetaInterface {
730+
/** Path to the test file being executed. */
731+
file: string;
732+
}
733+
728734
/** Call to declare a test, or chain to declare hooks or test modifiers */
729735
declare export default TestInterface<>;
730736

@@ -757,3 +763,6 @@ declare export var skip: SkipInterface<>;
757763

758764
/** Declare a test that should be implemented later. */
759765
declare export var todo: TodoDeclaration;
766+
767+
/** Meta data associated with the current process. */
768+
declare export var meta: MetaInterface;

lib/create-chain.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function createHookChain(hook, isAfterHook) {
6161
return hook;
6262
}
6363

64-
function createChain(fn, defaults) {
64+
function createChain(fn, defaults, meta) {
6565
// Test chaining rules:
6666
// * `serial` must come at the start
6767
// * `only` and `skip` must come at the end
@@ -108,6 +108,8 @@ function createChain(fn, defaults) {
108108
root.todo = startChain('test.todo', fn, Object.assign({}, defaults, {type: 'test', todo: true}));
109109
root.serial.todo = startChain('test.serial.todo', fn, Object.assign({}, defaults, {serial: true, type: 'test', todo: true}));
110110

111+
root.meta = meta;
112+
111113
return root;
112114
}
113115

lib/runner.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class Runner extends Emittery {
4040
const uniqueTestTitles = new Set();
4141
let hasStarted = false;
4242
let scheduledStart = false;
43+
const meta = Object.freeze({
44+
file: options.file
45+
});
4346
this.chain = createChain((metadata, args) => { // eslint-disable-line complexity
4447
if (hasStarted) {
4548
throw new Error('All tests and hooks must be declared synchronously in your test file, and cannot be nested within other tests or hooks.');
@@ -159,7 +162,7 @@ class Runner extends Emittery {
159162
failing: false,
160163
callback: false,
161164
always: false
162-
});
165+
}, meta);
163166
}
164167

165168
compareTestSnapshot(options) {

test/api.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ test('async/await support', t => {
7171
});
7272
});
7373

74+
test('test.meta.file', t => {
75+
const api = apiCreator();
76+
77+
return api.run([path.join(__dirname, 'fixture/meta.js')])
78+
.then(runStatus => {
79+
t.is(runStatus.stats.passedTests, 2);
80+
});
81+
});
82+
7483
test('fail-fast mode - single file & serial', t => {
7584
const api = apiCreator({
7685
failFast: true

test/fixture/meta.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {default as test, meta} from '../..';
2+
3+
test('meta.file', t => {
4+
t.is(meta.file, __filename);
5+
});
6+
7+
test('test.meta.file', t => {
8+
t.is(test.meta.file, __filename);
9+
});

0 commit comments

Comments
 (0)