Skip to content

Commit

Permalink
Expose test file path within worker process
Browse files Browse the repository at this point in the history
This adds `test.meta.file` which exposes the filename of the test being run by AVA.

Fixes #1976
  • Loading branch information
coreyfarrell authored and novemberborn committed Jan 13, 2019
1 parent c3bcbf2 commit bccd297
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 2 deletions.
10 changes: 10 additions & 0 deletions docs/01-writing-tests.md
Expand Up @@ -267,6 +267,16 @@ test('context is unicorn', t => {
});
```

## Retrieving test meta data

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.

```js
import test from 'ava';

console.log('Test currently being run: ', test.meta.file);
```

## Reusing test logic through macros

Additional arguments passed to the test declaration will be passed to the test implementation. This is useful for creating reusable test macros.
Expand Down
8 changes: 8 additions & 0 deletions index.d.ts
Expand Up @@ -468,6 +468,7 @@ export interface TestInterface<Context = {}> {
only: OnlyInterface<Context>;
skip: SkipInterface<Context>;
todo: TodoDeclaration;
meta: MetaInterface;
}

export interface AfterInterface<Context = {}> {
Expand Down Expand Up @@ -740,6 +741,11 @@ export interface TodoDeclaration {
(title: string): void;
}

export interface MetaInterface {
/** Path to the test file being executed. */
file: string;
}

/** Call to declare a test, or chain to declare hooks or test modifiers */
declare const test: TestInterface;

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

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

/*
Tail type from <https://github.com/tycho01/typical/blob/25f11ed92c960aab1ebbf47fd6ead9e0ae51d947/src/array/Tail.ts>.
Expand Down
9 changes: 9 additions & 0 deletions index.js.flow
Expand Up @@ -707,6 +707,7 @@ export interface SerialInterface<Context = {}> {
only: OnlyInterface<Context>;
skip: SkipInterface<Context>;
todo: TodoDeclaration;
meta: MetaInterface;
}

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

export interface MetaInterface {
/** Path to the test file being executed. */
file: string;
}

/** Call to declare a test, or chain to declare hooks or test modifiers */
declare export default TestInterface<>;

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

/** Declare a test that should be implemented later. */
declare export var todo: TodoDeclaration;

/** Meta data associated with the current process. */
declare export var meta: MetaInterface;
4 changes: 3 additions & 1 deletion lib/create-chain.js
Expand Up @@ -61,7 +61,7 @@ function createHookChain(hook, isAfterHook) {
return hook;
}

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

root.meta = meta;

return root;
}

Expand Down
5 changes: 4 additions & 1 deletion lib/runner.js
Expand Up @@ -40,6 +40,9 @@ class Runner extends Emittery {
const uniqueTestTitles = new Set();
let hasStarted = false;
let scheduledStart = false;
const meta = Object.freeze({
file: options.file
});
this.chain = createChain((metadata, args) => { // eslint-disable-line complexity
if (hasStarted) {
throw new Error('All tests and hooks must be declared synchronously in your test file, and cannot be nested within other tests or hooks.');
Expand Down Expand Up @@ -159,7 +162,7 @@ class Runner extends Emittery {
failing: false,
callback: false,
always: false
});
}, meta);
}

compareTestSnapshot(options) {
Expand Down
9 changes: 9 additions & 0 deletions test/api.js
Expand Up @@ -71,6 +71,15 @@ test('async/await support', t => {
});
});

test('test.meta.file', t => {
const api = apiCreator();

return api.run([path.join(__dirname, 'fixture/meta.js')])
.then(runStatus => {
t.is(runStatus.stats.passedTests, 2);
});
});

test('fail-fast mode - single file & serial', t => {
const api = apiCreator({
failFast: true
Expand Down
9 changes: 9 additions & 0 deletions test/fixture/meta.js
@@ -0,0 +1,9 @@
import {default as test, meta} from '../..';

test('meta.file', t => {
t.is(meta.file, __filename);
});

test('test.meta.file', t => {
t.is(test.meta.file, __filename);
});

0 comments on commit bccd297

Please sign in to comment.