From 28671b403a0bd1d33a83941d4058e235ae65b61f Mon Sep 17 00:00:00 2001 From: juergba Date: Fri, 18 Jan 2019 09:33:01 +0100 Subject: [PATCH] new tests --- .../fixtures/current-test-title.fixture.js | 47 ++++++++ .../after-hook-deepnested-error.fixture.js | 13 +++ .../hooks/after-hook-nested-error.fixture.js | 14 +++ .../before-hook-deepnested-error.fixture.js | 13 +++ .../hooks/before-hook-nested-error.fixture.js | 11 ++ .../hooks/before-hook-root-error.fixture.js | 9 ++ test/integration/hook-err.spec.js | 101 ++++++++++++++++++ test/integration/hooks.spec.js | 13 +++ 8 files changed, 221 insertions(+) create mode 100644 test/integration/fixtures/current-test-title.fixture.js create mode 100644 test/integration/fixtures/hooks/after-hook-deepnested-error.fixture.js create mode 100644 test/integration/fixtures/hooks/after-hook-nested-error.fixture.js create mode 100644 test/integration/fixtures/hooks/before-hook-deepnested-error.fixture.js create mode 100644 test/integration/fixtures/hooks/before-hook-nested-error.fixture.js create mode 100644 test/integration/fixtures/hooks/before-hook-root-error.fixture.js diff --git a/test/integration/fixtures/current-test-title.fixture.js b/test/integration/fixtures/current-test-title.fixture.js new file mode 100644 index 0000000000..6b5057e780 --- /dev/null +++ b/test/integration/fixtures/current-test-title.fixture.js @@ -0,0 +1,47 @@ +'use strict'; +var assert = require('assert'); + +function getTitle(ctx) { + return ctx.currentTest && ctx.currentTest.title; +}; + +before(function () { + assert.equal(getTitle(this), undefined); +}); + +describe('suite A', () => { + + before(function () { + assert.equal(getTitle(this), undefined); + }); + + describe('suite B', () => { + + it('test1 B', () => {}); + + describe('suite C', function () { + var lap = 0; + + before(function () { + assert.equal(getTitle(this), 'test1 C'); + }); + beforeEach(function () { + assert.equal(getTitle(this), ++lap === 1 ? 'test1 C' : 'test2 C'); + }); + + it('test1 C', function () {}); + it('test2 C', function () {}); + + afterEach(function () { + assert.equal(getTitle(this), lap === 1 ? 'test1 C' : 'test2 C'); + }); + after(function () { + assert.equal(getTitle(this), 'test2 C'); + }); + }); + }); +}); + +after(function () { + assert.equal(getTitle(this), undefined); +}); diff --git a/test/integration/fixtures/hooks/after-hook-deepnested-error.fixture.js b/test/integration/fixtures/hooks/after-hook-deepnested-error.fixture.js new file mode 100644 index 0000000000..2c65c94836 --- /dev/null +++ b/test/integration/fixtures/hooks/after-hook-deepnested-error.fixture.js @@ -0,0 +1,13 @@ +'use strict'; + +describe('spec 1', function () { + it('should pass', function () { }); + describe('spec 2 nested - this title should be used', function () { + after(function() { + throw new Error('after hook nested error'); + }); + describe('spec 3 nested', function () { + it('it nested - this title should not be used', function () { }); + }); + }); +}); diff --git a/test/integration/fixtures/hooks/after-hook-nested-error.fixture.js b/test/integration/fixtures/hooks/after-hook-nested-error.fixture.js new file mode 100644 index 0000000000..2c8f18d17a --- /dev/null +++ b/test/integration/fixtures/hooks/after-hook-nested-error.fixture.js @@ -0,0 +1,14 @@ +'use strict'; + +describe('spec 1', function () { + it('should pass', function () { }); + describe('spec nested', function () { + after(function() { + throw new Error('after hook nested error'); + }); + it('it nested - this title should be used', function () { }); + }); + describe('spec 2 nested', function () { + it('it nested - not this title', function () { }); + }); +}); diff --git a/test/integration/fixtures/hooks/before-hook-deepnested-error.fixture.js b/test/integration/fixtures/hooks/before-hook-deepnested-error.fixture.js new file mode 100644 index 0000000000..804e5e415b --- /dev/null +++ b/test/integration/fixtures/hooks/before-hook-deepnested-error.fixture.js @@ -0,0 +1,13 @@ +'use strict'; + +describe('spec 1', function () { + it('should pass', function () { }); + describe('spec 2 nested - this title should be used', function () { + before(function() { + throw new Error('before hook nested error'); + }); + describe('spec 3 nested', function () { + it('it nested - this title should not be used', function () { }); + }); + }); +}); diff --git a/test/integration/fixtures/hooks/before-hook-nested-error.fixture.js b/test/integration/fixtures/hooks/before-hook-nested-error.fixture.js new file mode 100644 index 0000000000..c0ade3a9f4 --- /dev/null +++ b/test/integration/fixtures/hooks/before-hook-nested-error.fixture.js @@ -0,0 +1,11 @@ +'use strict'; + +describe('spec 1', function () { + it('should pass', function () { }); + describe('spec nested', function () { + before(function() { + throw new Error('before hook nested error'); + }); + it('it nested - this title should be used', function () { }); + }); +}); diff --git a/test/integration/fixtures/hooks/before-hook-root-error.fixture.js b/test/integration/fixtures/hooks/before-hook-root-error.fixture.js new file mode 100644 index 0000000000..2af17af591 --- /dev/null +++ b/test/integration/fixtures/hooks/before-hook-root-error.fixture.js @@ -0,0 +1,9 @@ +'use strict'; + +before(function() { + throw new Error('before hook root error'); +}); + +describe('spec 1', function () { + it('should not be called', function () { }); +}); diff --git a/test/integration/hook-err.spec.js b/test/integration/hook-err.spec.js index d3b5c5c9a2..55604851ef 100644 --- a/test/integration/hook-err.spec.js +++ b/test/integration/hook-err.spec.js @@ -2,6 +2,7 @@ var helpers = require('./helpers'); var runMocha = helpers.runMocha; +var runMochaJSON = require('./helpers').runMochaJSON; var splitRegExp = helpers.splitRegExp; var bang = require('../../lib/reporters/base').symbols.bang; @@ -25,6 +26,59 @@ 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) { + if (err) { + return done(err); + } + expect(res, 'to have failed with error', 'before hook root error') + .and('to have failed test', '"before all" hook in "{root}"') + .and('to have passed test count', 0); + done(); + }); + }); + }); + + 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) { + if (err) { + return done(err); + } + expect(res, 'to have failed with error', 'before hook nested error') + .and( + 'to have failed test', + '"before all" hook for "it nested - this title should be used"' + ) + .and('to have passed test count', 1) + .and('to have passed test', 'should pass'); + done(); + }); + }); + }); + + 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) { + if (err) { + return done(err); + } + expect(res, 'to have failed with error', 'before hook nested error') + .and( + 'to have failed test', + '"before all" hook in "spec 2 nested - this title should be used"' + ) + .and('to have passed test count', 1) + .and('to have passed test', 'should pass'); + done(); + }); + }); + }); + describe('before each hook error', function() { before(run('hooks/beforeEach-hook-error.fixture.js')); it('should verify results', function() { @@ -39,6 +93,53 @@ 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) { + if (err) { + return done(err); + } + expect(res, 'to have failed with error', 'after hook nested error') + .and( + 'to have failed test', + '"after all" hook for "it nested - this title should be used"' + ) + .and('to have passed test count', 3) + .and( + 'to have passed test order', + 'should pass', + 'it nested - this title should be used', + 'it nested - not this title' + ); + done(); + }); + }); + }); + + 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) { + if (err) { + return done(err); + } + expect(res, 'to have failed with error', 'after hook nested error') + .and( + 'to have failed test', + '"after all" hook in "spec 2 nested - this title should be used"' + ) + .and('to have passed test count', 2) + .and( + 'to have passed test order', + 'should pass', + 'it nested - this title should not be used' + ); + done(); + }); + }); + }); + describe('after each hook error', function() { before(run('hooks/afterEach-hook-error.fixture.js')); it('should verify results', function() { diff --git a/test/integration/hooks.spec.js b/test/integration/hooks.spec.js index b7b38f6db7..7c3e8bf2b1 100644 --- a/test/integration/hooks.spec.js +++ b/test/integration/hooks.spec.js @@ -2,6 +2,7 @@ var assert = require('assert'); var runMocha = require('./helpers').runMocha; +var runMochaJSON = require('./helpers').runMochaJSON; var splitRegExp = require('./helpers').splitRegExp; var args = ['--reporter', 'dot']; @@ -49,4 +50,16 @@ describe('hooks', function() { done(); }); }); + + it('current test title of all hooks', function(done) { + runMochaJSON('current-test-title.fixture.js', [], function(err, res) { + if (err) { + return done(err); + } + expect(res, 'to have passed') + .and('to have passed test count', 3) + .and('to have passed test order', 'test1 B', 'test1 C', 'test2 C'); + done(); + }); + }); });