Skip to content

Commit

Permalink
Merge branch 'master' into issue/4054
Browse files Browse the repository at this point in the history
  • Loading branch information
khg0712 authored Jan 3, 2020
2 parents 9672e46 + 24c22be commit d617c77
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 53 deletions.
4 changes: 3 additions & 1 deletion bin/mocha
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ const trimV8Option = value =>
Object.keys(opts).forEach(opt => {
if (isNodeFlag(opt)) {
nodeArgs[trimV8Option(opt)] = opts[opt];
disableTimeouts(opt);
} else {
mochaArgs[opt] = opts[opt];
}
});

// disable 'timeout' for debugFlags
Object.keys(nodeArgs).forEach(opt => disableTimeouts(opt));

// Native debugger handling
// see https://nodejs.org/api/debugger.html#debugger_debugger
// look for 'inspect' or 'debug' that would launch this debugger,
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1911,14 +1911,14 @@ or the [source](https://github.com/mochajs/mocha/blob/master/lib/mocha.js).
[connect-test-output]: https://github.com/senchalabs/connect/blob/90a725343c2945aaee637e799b1cd11e065b2bff/tests.md
[emacs]: https://www.gnu.org/software/emacs/
[emacs-mocha.el]: https://github.com/scottaj/mocha.el
[example-babel]: https://github.com/mochajs/mocha-examples/tree/master/babel
[example-babel]: https://github.com/mochajs/mocha-examples/tree/master/packages/babel
[example-connect-test]: https://github.com/senchalabs/connect/tree/master/test
[example-express-test]: https://github.com/visionmedia/express/tree/master/test
[example-mocha-test]: https://github.com/mochajs/mocha/tree/master/test
[example-mocha-config]: https://github.com/mochajs/mocha/tree/master/example/config
[example-superagent-test]: https://github.com/visionmedia/superagent/tree/master/test/node
[example-third-party-reporter]: https://github.com/mochajs/mocha-examples/tree/master/packages/third-party-reporter
[example-typescript]: https://github.com/mochajs/mocha-examples/tree/master/typescript
[example-typescript]: https://github.com/mochajs/mocha-examples/tree/master/packages/typescript
[example-websocket.io-test]: https://github.com/LearnBoost/websocket.io/tree/master/test
[expect.js]: https://github.com/LearnBoost/expect.js
[expresso]: https://github.com/tj/expresso
Expand Down
20 changes: 15 additions & 5 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ exports.cursor = {
}
};

function showDiff(err) {
var showDiff = (exports.showDiff = function(err) {
return (
err &&
err.showDiff !== false &&
sameType(err.actual, err.expected) &&
err.expected !== undefined
);
}
});

function stringifyDiffObjs(err) {
if (!utils.isString(err.actual) || !utils.isString(err.expected)) {
Expand All @@ -182,9 +182,19 @@ function stringifyDiffObjs(err) {
* @return {string} Diff
*/
var generateDiff = (exports.generateDiff = function(actual, expected) {
return exports.inlineDiffs
? inlineDiff(actual, expected)
: unifiedDiff(actual, expected);
try {
return exports.inlineDiffs
? inlineDiff(actual, expected)
: unifiedDiff(actual, expected);
} catch (err) {
var msg =
'\n ' +
color('diff added', '+ expected') +
' ' +
color('diff removed', '- actual: failed to generate Mocha diff') +
'\n';
return msg;
}
});

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/reporters/xunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ XUnit.prototype.test = function(test) {
if (test.state === STATE_FAILED) {
var err = test.err;
var diff =
Base.hideDiff || !err.actual || !err.expected
? ''
: '\n' + Base.generateDiff(err.actual, err.expected);
!Base.hideDiff && Base.showDiff(err)
? '\n' + Base.generateDiff(err.actual, err.expected)
: '';
this.write(
tag(
'testcase',
Expand Down
28 changes: 22 additions & 6 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ Runner.prototype.fail = function(test, err) {
* - Failed `before each` hook skips remaining tests in a
* suite and jumps to corresponding `after each` hook,
* which is run only once
* - Failed `after` hook does not alter
* execution order
* - Failed `after` hook does not alter execution order
* - Failed `after each` hook skips remaining tests in a
* suite and subsuites, but executes other `after each`
* hooks
Expand Down Expand Up @@ -386,19 +385,28 @@ Runner.prototype.hook = function(name, fn) {
if (testError) {
self.fail(self.test, testError);
}
// conditional this.skip()
// conditional skip
if (hook.pending) {
if (name === HOOK_TYPE_AFTER_ALL) {
utils.deprecate(
'Skipping a test within an "after all" hook is DEPRECATED and will throw an exception in a future version of Mocha. ' +
'Use a return statement or other means to abort hook execution.'
);
}
if (name === HOOK_TYPE_BEFORE_EACH || name === HOOK_TYPE_AFTER_EACH) {
if (name === HOOK_TYPE_AFTER_EACH) {
// TODO define and implement use case
if (self.test) {
self.test.pending = true;
}
} else if (name === HOOK_TYPE_BEFORE_EACH) {
if (self.test) {
self.test.pending = true;
}
self.emit(constants.EVENT_HOOK_END, hook);
hook.pending = false; // activates hook for next test
return fn(new Error('abort hookDown'));
} else {
// TODO throw error for afterAll
suite.tests.forEach(function(test) {
test.pending = true;
});
Expand Down Expand Up @@ -619,6 +627,7 @@ Runner.prototype.runTests = function(suite, fn) {
return;
}

// static skip, no hooks are executed
if (test.isPending()) {
if (self.forbidPending) {
test.isPending = alwaysFalse;
Expand All @@ -634,6 +643,7 @@ Runner.prototype.runTests = function(suite, fn) {
// execute test and hook(s)
self.emit(constants.EVENT_TEST_BEGIN, (self.test = test));
self.hookDown(HOOK_TYPE_BEFORE_EACH, function(err, errSuite) {
// conditional skip within beforeEach
if (test.isPending()) {
if (self.forbidPending) {
test.isPending = alwaysFalse;
Expand All @@ -643,15 +653,21 @@ Runner.prototype.runTests = function(suite, fn) {
self.emit(constants.EVENT_TEST_PENDING, test);
}
self.emit(constants.EVENT_TEST_END, test);
return next();
// skip inner afterEach hooks below errSuite level
var origSuite = self.suite;
self.suite = errSuite;
return self.hookUp(HOOK_TYPE_AFTER_EACH, function(e, eSuite) {
self.suite = origSuite;
next(e, eSuite);
});
}
if (err) {
return hookErr(err, errSuite, false);
}
self.currentRunnable = self.test;
self.runTest(function(err) {
test = self.test;
// conditional this.skip()
// conditional skip within it
if (test.pending) {
if (self.forbidPending) {
test.isPending = alwaysFalse;
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@
"minimatch": "3.0.4",
"mkdirp": "0.5.1",
"ms": "2.1.1",
"node-environment-flags": "1.0.5",
"node-environment-flags": "1.0.6",
"object.assign": "4.1.0",
"strip-json-comments": "2.0.1",
"supports-color": "6.0.0",
Expand Down
4 changes: 2 additions & 2 deletions test/integration/fixtures/options/slow-test.fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('a suite', function() {
setTimeout(done, 500);
});

it('should succeed in 1.5s', function(done) {
setTimeout(done, 1500);
it('should succeed in 1.1s', function(done) {
setTimeout(done, 1100);
});
});
46 changes: 37 additions & 9 deletions test/integration/fixtures/pending/skip-async-beforeEach.fixture.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
'use strict';
var assert = require('assert');

describe('skip in beforeEach', function () {
beforeEach(function (done) {
describe('skip in beforeEach', function() {
var runOrder = [];
beforeEach(function(done) {
runOrder.push('beforeEach');
var self = this;
setTimeout(function () {
setTimeout(function() {
self.skip(); // done() is not required
}, 0);
}, 10);
});

it('should skip this test-1', function () {
it('should skip this test-1', function() {
throw new Error('never run this test');
});
it('should skip this test-2', function () {
throw new Error('never run this test');

describe('inner', function() {
beforeEach(function() {
runOrder.push('should not run');
});

it('should skip this test-2', function() {
throw new Error('never run this test');
});
it('should skip this test-3', function() {
throw new Error('never run this test');
});

afterEach(function() {
runOrder.push('should not run');
});
});
it('should skip this test-3', function () {
throw new Error('never run this test');

afterEach(function() {
runOrder.push('afterEach');
});
after(function() {
runOrder.push('after');
assert.deepStrictEqual(runOrder, [
'beforeEach', 'afterEach',
'beforeEach', 'afterEach',
'beforeEach', 'afterEach',
'after'
]);
throw new Error('should throw this error');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

describe('skip conditionally in beforeEach', function() {
var n = 1;
beforeEach(function() {
if (n !== 2) {
this.skip();
}
});

it('should skip this test-1', function() {
throw new Error('never run this test');
});
it('should run this test-2', function() {});

describe('inner suite', function() {
it('should skip this test-3', function() {
throw new Error('never run this test');
});
});

afterEach(function() { n++; });
after(function() {
if (n === 4) {
throw new Error('should throw this error');
}
});
});
40 changes: 35 additions & 5 deletions test/integration/fixtures/pending/skip-sync-beforeEach.fixture.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
'use strict';
var assert = require('assert');

describe('skip in beforeEach', function () {
beforeEach(function () {
describe('skip in beforeEach', function() {
var runOrder = [];
beforeEach(function() {
runOrder.push('beforeEach');
this.skip();
});

it('should never run this test', function () {
it('should skip this test-1', function() {
throw new Error('never run this test');
});

it('should never run this test', function () {
throw new Error('never run this test');
describe('inner', function() {
beforeEach(function() {
runOrder.push('should not run');
});

it('should skip this test-2', function() {
throw new Error('never run this test');
});
it('should skip this test-3', function() {
throw new Error('never run this test');
});

afterEach(function() {
runOrder.push('should not run');
});
});

afterEach(function() {
runOrder.push('afterEach');
});
after(function() {
runOrder.push('after');
assert.deepStrictEqual(runOrder, [
'beforeEach', 'afterEach',
'beforeEach', 'afterEach',
'beforeEach', 'afterEach',
'after'
]);
throw new Error('should throw this error');
});
});
15 changes: 15 additions & 0 deletions test/integration/options/timeout.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,19 @@ describe('--timeout', function() {
done();
});
});

it('should disable timeout with --inspect', function(done) {
var fixture = 'options/slow-test';
runMochaJSON(fixture, ['--inspect', '--timeout', '200'], function(
err,
res
) {
if (err) {
done(err);
return;
}
expect(res, 'to have passed').and('to have passed test count', 2);
done();
});
});
});
Loading

0 comments on commit d617c77

Please sign in to comment.