Skip to content

Commit cb5f9f7

Browse files
ulkennovemberborn
andauthoredApr 10, 2020
Expose snapshot directory through test.meta
Fixes #1984. Co-authored-by: Mark Wubben <mark@novemberborn.net>
1 parent 447d371 commit cb5f9f7

File tree

8 files changed

+85
-32
lines changed

8 files changed

+85
-32
lines changed
 

‎docs/01-writing-tests.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,19 @@ test('context is unicorn', t => {
267267
});
268268
```
269269

270-
## Retrieving test meta data
270+
## Retrieving test metadata
271271

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.
272+
Access data about the currently loaded test file run by reading `test.meta`.
273+
274+
Available properties:
275+
276+
* `file`: path to the test file
277+
* `snapshotDirectory`: directory where snapshots are stored
273278

274279
```js
275280
const test = require('ava');
276281

277-
console.log('Test currently being run: ', test.meta.file);
282+
console.log('Test file currently being run:', test.meta.file);
278283
```
279284

280285
## Reusing test logic through macros

‎index.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,9 @@ export interface TodoDeclaration {
760760
export interface MetaInterface {
761761
/** Path to the test file being executed. */
762762
file: string;
763+
764+
/** Directory where snapshots are stored. */
765+
snapshotDirectory: string;
763766
}
764767

765768
/** Call to declare a test, or chain to declare hooks or test modifiers */

‎lib/runner.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ class Runner extends Emittery {
5454
let hasStarted = false;
5555
let scheduledStart = false;
5656
const meta = Object.freeze({
57-
file: options.file
57+
file: options.file,
58+
get snapshotDirectory() {
59+
const {file, snapshotDir: fixedLocation, projectDir} = options;
60+
return snapshotManager.determineSnapshotDir({file, fixedLocation, projectDir});
61+
}
5862
});
5963
this.chain = createChain((metadata, testArgs) => { // eslint-disable-line complexity
6064
if (hasStarted) {

‎lib/snapshot-manager.js

+24-22
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const md5Hex = require('md5-hex');
1111
const convertSourceMap = require('convert-source-map');
1212
const slash = require('slash');
1313
const writeFileAtomic = require('write-file-atomic');
14+
const mem = require('mem');
1415

1516
const concordanceOptions = require('./concordance-options').snapshotManager;
1617

@@ -388,8 +389,26 @@ class Manager {
388389
}
389390
}
390391

391-
function determineSnapshotDir({file, fixedLocation, projectDir}) {
392+
const resolveSourceFile = mem(file => {
392393
const testDir = path.dirname(file);
394+
const buffer = tryRead(file);
395+
if (!buffer) {
396+
return file; // Assume the file is stubbed in our test suite.
397+
}
398+
399+
const source = buffer.toString();
400+
const converter = convertSourceMap.fromSource(source) || convertSourceMap.fromMapFileSource(source, testDir);
401+
if (converter) {
402+
const map = converter.toObject();
403+
const firstSource = `${map.sourceRoot || ''}${map.sources[0]}`;
404+
return path.resolve(testDir, firstSource);
405+
}
406+
407+
return file;
408+
});
409+
410+
const determineSnapshotDir = mem(({file, fixedLocation, projectDir}) => {
411+
const testDir = path.dirname(resolveSourceFile(file));
393412
if (fixedLocation) {
394413
const relativeTestLocation = path.relative(projectDir, testDir);
395414
return path.join(fixedLocation, relativeTestLocation);
@@ -405,30 +424,13 @@ function determineSnapshotDir({file, fixedLocation, projectDir}) {
405424
}
406425

407426
return testDir;
408-
}
427+
}, {cacheKey: ([{file}]) => file});
409428

410-
function resolveSourceFile(file) {
411-
const testDir = path.dirname(file);
412-
const buffer = tryRead(file);
413-
if (!buffer) {
414-
return file; // Assume the file is stubbed in our test suite.
415-
}
416-
417-
const source = buffer.toString();
418-
const converter = convertSourceMap.fromSource(source) || convertSourceMap.fromMapFileSource(source, testDir);
419-
if (converter) {
420-
const map = converter.toObject();
421-
const firstSource = `${map.sourceRoot || ''}${map.sources[0]}`;
422-
return path.resolve(testDir, firstSource);
423-
}
424-
425-
return file;
426-
}
429+
exports.determineSnapshotDir = determineSnapshotDir;
427430

428431
function load({file, fixedLocation, projectDir, recordNewSnapshots, updating}) {
429-
const sourceFile = resolveSourceFile(file);
430-
const dir = determineSnapshotDir({file: sourceFile, fixedLocation, projectDir});
431-
const relFile = path.relative(projectDir, sourceFile);
432+
const dir = determineSnapshotDir({file, fixedLocation, projectDir});
433+
const relFile = path.relative(projectDir, resolveSourceFile(file));
432434
const name = path.basename(relFile);
433435
const reportFile = `${name}.md`;
434436
const snapFile = `${name}.snap`;

‎package-lock.json

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"esm": "^3.2.25",
117117
"execa": "^4.0.0",
118118
"get-stream": "^5.1.0",
119+
"mem": "^6.0.1",
119120
"nyc": "^15.0.1",
120121
"p-event": "^4.1.0",
121122
"proxyquire": "^2.1.3",

‎test-tap/api.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ function apiCreator(options = {}) {
3030
return instance;
3131
}
3232

33-
test('test.meta.file', t => {
34-
const api = apiCreator();
33+
test('test.meta', t => {
34+
const api = apiCreator({snapshotDir: 'snapshot-fixture'});
3535

36-
return api.run({files: [path.join(__dirname, 'fixture/meta.js')]})
36+
return api.run({files: [path.join(__dirname, 'fixture', 'meta.js')]})
3737
.then(runStatus => {
38-
t.is(runStatus.stats.passedTests, 2);
38+
t.is(runStatus.stats.passedTests, 3);
3939
});
4040
});
4141

‎test-tap/fixture/meta.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
const {default: test, meta} = require('../..');
22

3+
test('meta is test.meta', t => {
4+
t.is(meta, test.meta);
5+
});
6+
37
test('meta.file', t => {
48
t.is(meta.file, __filename);
59
});
610

7-
test('test.meta.file', t => {
8-
t.is(test.meta.file, __filename);
11+
test('meta.snapshotDirectory', t => {
12+
t.regex(meta.snapshotDirectory, /snapshot-fixture/);
913
});
14+

0 commit comments

Comments
 (0)
Please sign in to comment.