From 55283e335a8ce472a53113a2eeac414c6816bda2 Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Mon, 18 May 2020 17:01:51 -0700 Subject: [PATCH] fix test Signed-off-by: Christopher Hiller --- test/unit/runner.spec.js | 112 ++++++++++++++++++++++++++++----------- 1 file changed, 80 insertions(+), 32 deletions(-) diff --git a/test/unit/runner.spec.js b/test/unit/runner.spec.js index b3f475f199..7bd2e96a1a 100644 --- a/test/unit/runner.spec.js +++ b/test/unit/runner.spec.js @@ -10,6 +10,7 @@ var Test = Mocha.Test; var Runnable = Mocha.Runnable; var Hook = Mocha.Hook; var noop = Mocha.utils.noop; +var errors = require('../../lib/errors'); var EVENT_HOOK_BEGIN = Runner.constants.EVENT_HOOK_BEGIN; var EVENT_TEST_FAIL = Runner.constants.EVENT_TEST_FAIL; var EVENT_TEST_RETRY = Runner.constants.EVENT_TEST_RETRY; @@ -255,8 +256,8 @@ describe('Runner', function() { }); }); - describe('.fail(test, err)', function() { - it('should increment .failures', function() { + describe('fail()', function() { + it('should increment `Runner#failures`', function() { expect(runner.failures, 'to be', 0); runner.fail(new Test('one', noop), {}); expect(runner.failures, 'to be', 1); @@ -264,7 +265,7 @@ describe('Runner', function() { expect(runner.failures, 'to be', 2); }); - it('should set test.state to "failed"', function() { + it('should set `Test#state` to "failed"', function() { var test = new Test('some test', noop); runner.fail(test, 'some error'); expect(test.state, 'to be', STATE_FAILED); @@ -376,6 +377,47 @@ describe('Runner', function() { runner.fail(test, new Error()); expect(runner.failures, 'to be', 0); }); + + describe('when Runner has stopped', function() { + beforeEach(function() { + runner.state = STATE_STOPPED; + }); + + describe('when test is not pending', function() { + describe('when error is the "multiple done" variety', function() { + it('should throw the "multiple done" error', function() { + var test = new Test('test', function() {}); + suite.addTest(test); + var err = new Error(); + err.code = errors.constants.MULTIPLE_DONE; + expect( + function() { + runner.fail(test, err); + }, + 'to throw', + err + ); + }); + }); + + describe('when error is not of the "multiple done" variety', function() { + it('should throw a "fatal" error', function() { + var test = new Test('test', function() {}); + suite.addTest(test); + var err = new Error(); + expect( + function() { + runner.fail(test, err); + }, + 'to throw', + { + code: errors.constants.FATAL + } + ); + }); + }); + }); + }); }); describe('.failHook(hook, err)', function() { @@ -852,7 +894,7 @@ describe('Runner', function() { ]).and('was called once'); }); - describe('when Runner has already started', function() { + describe('when Runner is RUNNING', function() { beforeEach(function() { runner.state = STATE_RUNNING; }); @@ -869,39 +911,45 @@ describe('Runner', function() { }); }); - describe('when Runner not running', function() { - describe('when idle', function() { - beforeEach(function() { - runner.state = STATE_IDLE; - }); + describe('when Runner is IDLE', function() { + beforeEach(function() { + runner.state = STATE_IDLE; + }); - it('should emit start/end events for the benefit of reporters', function() { - expect( - function() { - runner.uncaught(err); - }, - 'to emit from', - runner, - 'start' - ).and('to emit from', runner, 'end'); - }); + it('should emit start/end events for the benefit of reporters', function() { + expect( + function() { + runner.uncaught(err); + }, + 'to emit from', + runner, + 'start' + ).and('to emit from', runner, 'end'); }); + }); - describe('when stopped', function() { - beforeEach(function() { - runner.state = STATE_STOPPED; - }); + describe('when Runner is STOPPED', function() { + beforeEach(function() { + runner.state = STATE_STOPPED; + }); - it('should emit start/end events for the benefit of reporters', function() { - expect( - function() { + it('should not emit start/end events, since this presumably would have already happened', function() { + expect( + function() { + try { runner.uncaught(err); - }, - 'to emit from', - runner, - 'start' - ).and('to emit from', runner, 'end'); - }); + } catch (ignored) {} + }, + 'not to emit from', + runner, + 'start' + ).and('not to emit from', runner, 'end'); + }); + + it('should throw', function() { + expect(function() { + runner.uncaught(err); + }, 'to throw'); }); }); });