Skip to content

Commit

Permalink
add test for this.test.error() behavior in "after each" hooks
Browse files Browse the repository at this point in the history
- cleanup `test/integration/hook-err.spec.js`
- add default params for `runMochaJSONAsync()` helper
- expand "to have passed/failed tests" assertions (for JSON reporter output) to allow matching against `fullTitle` prop
- rename some fixtures to lowercase
  • Loading branch information
boneskull committed Nov 24, 2020
1 parent 78a41d1 commit bc8ce05
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 37 deletions.
17 changes: 13 additions & 4 deletions test/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,12 @@ module.exports = {
expect(
result,
'[not] to have a value satisfying',
expect.it('to have an item satisfying', {title: title})
expect.it(
'to have an item satisfying',
expect
.it('to have property', 'title', title)
.or('to have property', 'fullTitle', title)
)
);
});
}
Expand All @@ -180,9 +185,13 @@ module.exports = {
'<JSONResult> [not] to have failed (test|tests) <string+>',
(expect, result, ...titles) => {
titles.forEach(title => {
expect(result.failures, '[not] to have an item satisfying', {
title: title
});
expect(
result.failures,
'[not] to have an item satisfying',
expect
.it('to have property', 'title', title)
.or('to have property', 'fullTitle', title)
);
});
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('fail the test from the "after each" hook', function() {
it('should fail', function() {
// but not here
});

afterEach(function() {
this.test.error(new Error('failing from after each'));
});
});
2 changes: 1 addition & 1 deletion test/integration/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function runMochaAsync(fixturePath, args, opts) {
* @param {Object} [opts] - Options for `child_process.spawn`
* @returns {Promise<JSONResult>}
*/
function runMochaJSONAsync(fixturePath, args, opts) {
function runMochaJSONAsync(fixturePath, args = [], opts = {}) {
return new Promise((resolve, reject) => {
runMochaJSON(
fixturePath,
Expand Down
71 changes: 39 additions & 32 deletions test/integration/hook-err.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict';

var helpers = require('./helpers');
var runMocha = helpers.runMocha;
var runMochaJSON = require('./helpers').runMochaJSON;
var SPLIT_DOT_REPORTER_REGEXP = helpers.SPLIT_DOT_REPORTER_REGEXP;
var bang = require('../../lib/reporters/base').symbols.bang;
const {
runMocha,
runMochaJSON,
runMochaJSONAsync,
SPLIT_DOT_REPORTER_REGEXP
} = require('./helpers');
const {bang} = require('../../lib/reporters/base').symbols;

describe('hook error handling', function() {
var lines;
let lines;

describe('before hook error', function() {
before(run('hooks/before-hook-error.fixture.js'));
Expand All @@ -28,8 +29,7 @@ describe('hook error handling', function() {

describe('before hook root error', function() {
it('should verify results', function(done) {
var fixture = 'hooks/before-hook-root-error.fixture.js';
runMochaJSON(fixture, [], function(err, res) {
runMochaJSON('hooks/before-hook-root-error', [], (err, res) => {
if (err) {
return done(err);
}
Expand All @@ -43,8 +43,7 @@ describe('hook error handling', function() {

describe('before hook nested error', function() {
it('should verify results', function(done) {
var fixture = 'hooks/before-hook-nested-error.fixture.js';
runMochaJSON(fixture, [], function(err, res) {
runMochaJSON('hooks/before-hook-nested-error', [], (err, res) => {
if (err) {
return done(err);
}
Expand All @@ -62,8 +61,7 @@ describe('hook error handling', function() {

describe('before hook deepnested error', function() {
it('should verify results', function(done) {
var fixture = 'hooks/before-hook-deepnested-error.fixture.js';
runMochaJSON(fixture, [], function(err, res) {
runMochaJSON('hooks/before-hook-deepnested-error', [], (err, res) => {
if (err) {
return done(err);
}
Expand All @@ -80,7 +78,7 @@ describe('hook error handling', function() {
});

describe('before each hook error', function() {
before(run('hooks/beforeEach-hook-error.fixture.js'));
before(run('hooks/before-each-hook-error.fixture.js'));
it('should verify results', function() {
expect(lines, 'to equal', ['before', bang + 'test 3']);
});
Expand All @@ -95,8 +93,7 @@ describe('hook error handling', function() {

describe('after hook nested error', function() {
it('should verify results', function(done) {
var fixture = 'hooks/after-hook-nested-error.fixture.js';
runMochaJSON(fixture, [], function(err, res) {
runMochaJSON('hooks/after-hook-nested-error', [], (err, res) => {
if (err) {
return done(err);
}
Expand All @@ -119,8 +116,7 @@ describe('hook error handling', function() {

describe('after hook deepnested error', function() {
it('should verify results', function(done) {
var fixture = 'hooks/after-hook-deepnested-error.fixture.js';
runMochaJSON(fixture, [], function(err, res) {
runMochaJSON('hooks/after-hook-deepnested-error', [], (err, res) => {
if (err) {
return done(err);
}
Expand All @@ -141,7 +137,7 @@ describe('hook error handling', function() {
});

describe('after each hook error', function() {
before(run('hooks/afterEach-hook-error.fixture.js'));
before(run('hooks/after-each-hook-error.fixture.js'));
it('should verify results', function() {
expect(lines, 'to equal', ['test 1', 'after', bang + 'test 3']);
});
Expand Down Expand Up @@ -202,7 +198,7 @@ describe('hook error handling', function() {
});

describe('async - before each hook error', function() {
before(run('hooks/beforeEach-hook-async-error.fixture.js'));
before(run('hooks/before-each-hook-async-error.fixture.js'));
it('should verify results', function() {
expect(lines, 'to equal', ['before', bang + 'test 3']);
});
Expand All @@ -216,7 +212,7 @@ describe('hook error handling', function() {
});

describe('async - after each hook error', function() {
before(run('hooks/afterEach-hook-async-error.fixture.js'));
before(run('hooks/after-each-hook-async-error.fixture.js'));
it('should verify results', function() {
expect(lines, 'to equal', ['test 1', 'after', bang + 'test 3']);
});
Expand Down Expand Up @@ -257,27 +253,38 @@ describe('hook error handling', function() {
});
});

describe('"this.test.error()-style failure', function() {
it('should fail the associated test', async function() {
return expect(
runMochaJSONAsync('hooks/after-each-this-test-error'),
'when fulfilled',
'to have failed'
).and(
'when fulfilled',
'to have failed test',
'fail the test from the "after each" hook should fail'
);
});
});

function run(fnPath, outputFilter) {
return function(done) {
runMocha(fnPath, ['--reporter', 'dot'], function(err, res) {
return done =>
runMocha(fnPath, ['--reporter', 'dot'], (err, res) => {
expect(err, 'to be falsy');

lines = res.output
.split(SPLIT_DOT_REPORTER_REGEXP)
.map(function(line) {
return line.trim();
})
.map(line => line.trim())
.filter(outputFilter || onlyConsoleOutput());

done();
});
};
}
});

function onlyConsoleOutput() {
var foundSummary = false;
return function(line) {
let foundSummary = false;
return line => {
if (!foundSummary) {
foundSummary = !!/\(\d+ms\)/.exec(line);
}
Expand All @@ -286,9 +293,9 @@ function onlyConsoleOutput() {
}

function onlyErrorTitle(line) {
var foundErrorTitle = false;
var foundError = false;
return function(line) {
let foundErrorTitle = false;
let foundError = false;
return line => {
if (!foundErrorTitle) {
foundErrorTitle = !!/^1\)/.exec(line);
}
Expand Down

0 comments on commit bc8ce05

Please sign in to comment.