Skip to content

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

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
wants to merge 2 commits into from
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
25 changes: 22 additions & 3 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,22 @@ class Runner extends Emittery {
uniqueTestTitles.add(specifiedTitle);
}

const specifiedHash = Runnable.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;
}
}

this.tasks.todo.push({title: specifiedTitle, metadata});
this.tasks.todo.push({hash: specifiedHash, title: specifiedTitle, metadata});
this.emit('stateChange', {
type: 'declared-test',
hash: specifiedHash,
title: specifiedTitle,
knownFailing: false,
todo: true
Expand Down Expand Up @@ -126,7 +131,10 @@ class Runner extends Emittery {
}
}

const hash = Runnable.getTitleHash(title);

const task = {
hash,
title,
implementation,
args,
Expand All @@ -136,7 +144,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 +156,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 Down Expand Up @@ -283,13 +294,15 @@ class Runner extends Emittery {
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 +329,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 +347,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 +372,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 +391,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 +414,7 @@ class Runner extends Emittery {

this.emit('stateChange', {
type: 'selected-test',
hash: task.hash,
title: task.title,
knownFailing: false,
skip: false,
Expand Down
7 changes: 7 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const observableToPromise = require('observable-to-promise');
const isPromise = require('is-promise');
const isObservable = require('is-observable');
const plur = require('plur');
const objectHash = require('object-hash');
const assert = require('./assert');
const nowAndTimers = require('./now-and-timers');
const concordanceOptions = require('./concordance-options').default;
Expand Down Expand Up @@ -102,6 +103,7 @@ class Test {
this.failWithoutAssertions = options.failWithoutAssertions;
this.fn = options.fn;
this.metadata = options.metadata;
this.hash = Test.getTitleHash(options.title);
this.title = options.title;
this.logs = [];

Expand Down Expand Up @@ -140,6 +142,10 @@ class Test {
this.timeoutMs = 0;
}

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

bindEndCallback() {
if (this.metadata.callback) {
return (error, savedError) => {
Expand Down Expand Up @@ -481,6 +487,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
19 changes: 19 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 All @@ -110,12 +112,15 @@ test('fail-fast mode - single file & serial', t => {
t.ok(api.options.failFast);
t.strictDeepEqual(tests, [{
ok: true,
hash: '1221417f85',
title: 'first pass'
}, {
ok: false,
hash: 'a9039c8aa0',
title: 'second fail'
}, {
ok: true,
hash: '09fda0bbf9',
title: 'third pass'
}]);
t.is(runStatus.stats.passedTests, 2);
Expand All @@ -137,12 +142,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 All @@ -158,10 +165,12 @@ test('fail-fast mode - multiple files & serial', t => {
t.strictDeepEqual(tests, [{
ok: true,
testFile: path.join(__dirname, 'fixture/fail-fast/multiple-files/fails.js'),
hash: '1221417f85',
title: 'first pass'
}, {
ok: false,
testFile: path.join(__dirname, 'fixture/fail-fast/multiple-files/fails.js'),
hash: 'a9039c8aa0',
title: 'second fail'
}]);
t.is(runStatus.stats.passedTests, 1);
Expand All @@ -183,12 +192,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 All @@ -204,18 +215,22 @@ test('fail-fast mode - multiple files & interrupt', t => {
t.strictDeepEqual(tests, [{
ok: true,
testFile: path.join(__dirname, 'fixture/fail-fast/multiple-files/fails.js'),
hash: '1221417f85',
title: 'first pass'
}, {
ok: false,
testFile: path.join(__dirname, 'fixture/fail-fast/multiple-files/fails.js'),
hash: 'a9039c8aa0',
title: 'second fail'
}, {
ok: true,
testFile: path.join(__dirname, 'fixture/fail-fast/multiple-files/fails.js'),
hash: '09fda0bbf9',
title: 'third pass'
}, {
ok: true,
testFile: path.join(__dirname, 'fixture/fail-fast/multiple-files/passes-slow.js'),
hash: '1221417f85',
title: 'first pass'
}]);
t.is(runStatus.stats.passedTests, 3);
Expand All @@ -237,11 +252,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 +296,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
2 changes: 1 addition & 1 deletion test/reporters/verbose.failfast.log
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

---tty-stream-chunk-separator
✖ a › fails Test failed via `t.fail()`
✖ 9eaac03e61 a › fails Test failed via `t.fail()`
---tty-stream-chunk-separator

1 test failed
Expand Down
2 changes: 1 addition & 1 deletion test/reporters/verbose.failfast2.log
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

---tty-stream-chunk-separator
✖ a › fails Test failed via `t.fail()`
✖ 9eaac03e61 a › fails Test failed via `t.fail()`
---tty-stream-chunk-separator

1 test failed
Expand Down
4 changes: 2 additions & 2 deletions test/reporters/verbose.only.log
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

---tty-stream-chunk-separator
✔ a › only
✔ e90e64e828 a › only
---tty-stream-chunk-separator
✔ b › passes
✔ 63d04ac302 b › passes
---tty-stream-chunk-separator

2 tests passed
Expand Down
Loading