Skip to content

extend --match to support hash (generated from unique title) #2196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ exports.run = async () => { // eslint-disable-line complexity

Options
--watch, -w Re-run tests when tests and source files change
--match, -m Only run tests with matching title (Can be repeated)
--match, -m Only run tests with matching title/hash (Can be repeated)
--update-snapshots, -u Update snapshots
--fail-fast Stop after first test failure
--timeout, -T Set global timeout (milliseconds or human-readable, e.g. 10s, 2m)
Expand Down
1 change: 1 addition & 0 deletions lib/reporters/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ module.exports = {
errorSource: chalk.gray,
errorStack: chalk.gray,
stack: chalk.red,
hash: chalk.dim,
information: chalk.magenta
};
12 changes: 6 additions & 6 deletions lib/reporters/verbose.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@ class VerboseReporter {
break;
case 'hook-finished':
if (evt.logs.length > 0) {
this.lineWriter.writeLine(` ${this.prefixTitle(evt.testFile, evt.title)}`);
this.lineWriter.writeLine(` ${evt.hash} ${this.prefixTitle(evt.testFile, evt.title)}`);
this.writeLogs(evt);
}

break;
case 'selected-test':
if (evt.skip) {
this.lineWriter.writeLine(colors.skip(`- ${this.prefixTitle(evt.testFile, evt.title)}`));
this.lineWriter.writeLine(colors.skip(`- ${evt.hash} ${this.prefixTitle(evt.testFile, evt.title)}`));
} else if (evt.todo) {
this.lineWriter.writeLine(colors.todo(`- ${this.prefixTitle(evt.testFile, evt.title)}`));
this.lineWriter.writeLine(colors.todo(`- ${evt.hash} ${this.prefixTitle(evt.testFile, evt.title)}`));
}

break;
Expand Down Expand Up @@ -293,15 +293,15 @@ class VerboseReporter {

writeTestSummary(evt) {
if (evt.type === 'hook-failed' || evt.type === 'test-failed') {
this.lineWriter.writeLine(`${colors.error(figures.cross)} ${this.prefixTitle(evt.testFile, evt.title)} ${colors.error(evt.err.message)}`);
this.lineWriter.writeLine(`${colors.error(figures.cross)} ${colors.hash(evt.hash)} ${this.prefixTitle(evt.testFile, evt.title)} ${colors.error(evt.err.message)}`);
} else if (evt.knownFailing) {
this.lineWriter.writeLine(`${colors.error(figures.tick)} ${colors.error(this.prefixTitle(evt.testFile, evt.title))}`);
this.lineWriter.writeLine(`${colors.error(figures.tick)} ${colors.hash(evt.hash)} ${colors.error(this.prefixTitle(evt.testFile, evt.title))}`);
} else {
// Display duration only over a threshold
const threshold = 100;
const duration = evt.duration > threshold ? colors.duration(' (' + prettyMs(evt.duration) + ')') : '';

this.lineWriter.writeLine(`${colors.pass(figures.tick)} ${this.prefixTitle(evt.testFile, evt.title)}${duration}`);
this.lineWriter.writeLine(`${colors.pass(figures.tick)} ${colors.hash(evt.hash)} ${this.prefixTitle(evt.testFile, evt.title)}${duration}`);
}

this.writeLogs(evt);
Expand Down
29 changes: 27 additions & 2 deletions lib/runner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const Emittery = require('emittery');
const matcher = require('matcher');
const objectHash = require('object-hash');
const ContextRef = require('./context-ref');
const createChain = require('./create-chain');
const snapshotManager = require('./snapshot-manager');
Expand Down Expand Up @@ -79,9 +80,13 @@ class Runner extends Emittery {
uniqueTestTitles.add(specifiedTitle);
}

const specifiedHash = this.getTitleHash(specifiedTitle);

if (this.match.length > 0) {
// --match selects TODO tests.
if (matcher([specifiedTitle], this.match).length === 1) {
const titleMatched = matcher([specifiedTitle], this.match).length === 1;
const hashMatched = matcher([specifiedHash], this.match).length === 1;
if (titleMatched || hashMatched) {
metadata.exclusive = true;
this.runOnlyExclusive = true;
}
Expand All @@ -90,6 +95,7 @@ class Runner extends Emittery {
this.tasks.todo.push({title: specifiedTitle, metadata});
this.emit('stateChange', {
type: 'declared-test',
hash: specifiedHash,
title: specifiedTitle,
knownFailing: false,
todo: true
Expand Down Expand Up @@ -126,7 +132,10 @@ class Runner extends Emittery {
}
}

const hash = this.getTitleHash(title);

const task = {
hash,
title,
implementation,
args,
Expand All @@ -136,7 +145,9 @@ class Runner extends Emittery {
if (metadata.type === 'test') {
if (this.match.length > 0) {
// --match overrides .only()
task.metadata.exclusive = matcher([title], this.match).length === 1;
const titleMatched = matcher([title], this.match).length === 1;
const hashMatched = matcher([hash], this.match).length === 1;
task.metadata.exclusive = titleMatched || hashMatched;
}

if (task.metadata.exclusive) {
Expand All @@ -146,6 +157,7 @@ class Runner extends Emittery {
this.tasks[metadata.serial ? 'serial' : 'concurrent'].push(task);
this.emit('stateChange', {
type: 'declared-test',
hash,
title,
knownFailing: metadata.failing,
todo: false
Expand All @@ -166,6 +178,10 @@ class Runner extends Emittery {
}, meta);
}

getTitleHash(title) {
return title ? objectHash(title).substring(0, 10) : undefined;
}

compareTestSnapshot(options) {
if (!this.snapshots) {
this.snapshots = snapshotManager.load({
Expand Down Expand Up @@ -276,20 +292,23 @@ class Runner extends Emittery {
compareTestSnapshot: this.boundCompareTestSnapshot,
updateSnapshots: this.updateSnapshots,
metadata: task.metadata,
hash: this.getTitleHash(`${task.title}${titleSuffix || ''}`),
title: `${task.title}${titleSuffix || ''}`
}));
const outcome = await this.runMultiple(hooks, this.serial);
for (const result of outcome.storedResults) {
if (result.passed) {
this.emit('stateChange', {
type: 'hook-finished',
hash: result.hash,
title: result.title,
duration: result.duration,
logs: result.logs
});
} else {
this.emit('stateChange', {
type: 'hook-failed',
hash: result.hash,
title: result.title,
err: serializeError('Hook failure', true, result.error),
duration: result.duration,
Expand All @@ -316,13 +335,15 @@ class Runner extends Emittery {
compareTestSnapshot: this.boundCompareTestSnapshot,
updateSnapshots: this.updateSnapshots,
metadata: task.metadata,
hash: task.hash,
title: task.title
});

const result = await this.runSingle(test);
if (result.passed) {
this.emit('stateChange', {
type: 'test-passed',
hash: result.hash,
title: result.title,
duration: result.duration,
knownFailing: result.metadata.failing,
Expand All @@ -332,6 +353,7 @@ class Runner extends Emittery {
} else {
this.emit('stateChange', {
type: 'test-failed',
hash: result.hash,
title: result.title,
err: serializeError('Test failure', true, result.error),
duration: result.duration,
Expand All @@ -356,6 +378,7 @@ class Runner extends Emittery {

this.emit('stateChange', {
type: 'selected-test',
hash: task.hash,
title: task.title,
knownFailing: task.metadata.failing,
skip: task.metadata.skipped,
Expand All @@ -374,6 +397,7 @@ class Runner extends Emittery {

this.emit('stateChange', {
type: 'selected-test',
hash: task.hash,
title: task.title,
knownFailing: task.metadata.failing,
skip: task.metadata.skipped,
Expand All @@ -396,6 +420,7 @@ class Runner extends Emittery {

this.emit('stateChange', {
type: 'selected-test',
hash: task.hash,
title: task.title,
knownFailing: false,
skip: false,
Expand Down
2 changes: 2 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class Test {
this.failWithoutAssertions = options.failWithoutAssertions;
this.fn = options.fn;
this.metadata = options.metadata;
this.hash = options.hash;
this.title = options.title;
this.logs = [];

Expand Down Expand Up @@ -481,6 +482,7 @@ class Test {
logs: this.logs,
metadata: this.metadata,
passed,
hash: this.hash,
title: this.title
};
}
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"git-branch": "^2.0.1",
"has-ansi": "^3.0.0",
"lolex": "^4.1.0",
"object-hash": "^1.3.1",
"proxyquire": "^2.1.0",
"react": "^16.8.6",
"react-test-renderer": "^16.8.6",
Expand Down
10 changes: 10 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ test('fail-fast mode - single file & serial', t => {
if (evt.type === 'test-failed') {
tests.push({
ok: false,
hash: evt.hash,
title: evt.title
});
} else if (evt.type === 'test-passed') {
tests.push({
ok: true,
hash: evt.hash,
title: evt.title
});
}
Expand Down Expand Up @@ -137,12 +139,14 @@ test('fail-fast mode - multiple files & serial', t => {
tests.push({
ok: false,
testFile: evt.testFile,
hash: evt.hash,
title: evt.title
});
} else if (evt.type === 'test-passed') {
tests.push({
ok: true,
testFile: evt.testFile,
hash: evt.hash,
title: evt.title
});
}
Expand Down Expand Up @@ -183,12 +187,14 @@ test('fail-fast mode - multiple files & interrupt', t => {
tests.push({
ok: false,
testFile: evt.testFile,
hash: evt.hash,
title: evt.title
});
} else if (evt.type === 'test-passed') {
tests.push({
ok: true,
testFile: evt.testFile,
hash: evt.hash,
title: evt.title
});
}
Expand Down Expand Up @@ -237,11 +243,13 @@ test('fail-fast mode - crash & serial', t => {
if (evt.type === 'test-failed') {
tests.push({
ok: false,
hash: evt.hash,
title: evt.title
});
} else if (evt.type === 'test-passed') {
tests.push({
ok: true,
hash: evt.hash,
title: evt.title
});
} else if (evt.type === 'worker-failed') {
Expand Down Expand Up @@ -279,11 +287,13 @@ test('fail-fast mode - timeout & serial', t => {
if (evt.type === 'test-failed') {
tests.push({
ok: false,
hash: evt.hash,
title: evt.title
});
} else if (evt.type === 'test-passed') {
tests.push({
ok: true,
hash: evt.hash,
title: evt.title
});
} else if (evt.type === 'timeout') {
Expand Down