|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var path = require('node:path').posix; |
| 4 | +var helpers = require('./helpers'); |
| 5 | +var runMocha = helpers.runMocha; |
| 6 | +var runMochaJSON = helpers.runMochaJSON; |
| 7 | + |
| 8 | +describe('nested test detection', function () { |
| 9 | + var nestedTestErrorMessage = 'Nested tests are not allowed'; |
| 10 | + |
| 11 | + describe('BDD interface', function () { |
| 12 | + it('should fail when nested tests are detected', function (done) { |
| 13 | + var fixture = path.join('nested-tests', 'bdd-nested'); |
| 14 | + var spawnOpts = {stdio: 'pipe'}; |
| 15 | + runMocha( |
| 16 | + fixture, |
| 17 | + ['--ui', 'bdd'], |
| 18 | + function (err, res) { |
| 19 | + if (err) { |
| 20 | + return done(err); |
| 21 | + } |
| 22 | + expect(res, 'to have failed with output', new RegExp(nestedTestErrorMessage)); |
| 23 | + expect(res, 'to have failed with output', /inner nested test.*detected inside test.*outer test/); |
| 24 | + done(); |
| 25 | + }, |
| 26 | + spawnOpts |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should report correct test counts when nested tests fail', function (done) { |
| 31 | + var fixture = path.join('nested-tests', 'bdd-nested'); |
| 32 | + runMochaJSON(fixture, ['--ui', 'bdd'], function (err, res) { |
| 33 | + if (err) { |
| 34 | + return done(err); |
| 35 | + } |
| 36 | + expect(res, 'to have failed') |
| 37 | + .and('to have passed test count', 2) // 'normal test' and 'another normal test' should pass |
| 38 | + .and('to have failed test count', 1); // 'outer test with nested test' should fail |
| 39 | + done(); |
| 40 | + }); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('TDD interface', function () { |
| 45 | + it('should fail when nested tests are detected', function (done) { |
| 46 | + var fixture = path.join('nested-tests', 'tdd-nested'); |
| 47 | + var spawnOpts = {stdio: 'pipe'}; |
| 48 | + runMocha( |
| 49 | + fixture, |
| 50 | + ['--ui', 'tdd'], |
| 51 | + function (err, res) { |
| 52 | + if (err) { |
| 53 | + return done(err); |
| 54 | + } |
| 55 | + expect(res, 'to have failed with output', new RegExp(nestedTestErrorMessage)); |
| 56 | + expect(res, 'to have failed with output', /inner nested test.*detected inside test.*outer test/); |
| 57 | + done(); |
| 58 | + }, |
| 59 | + spawnOpts |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should report correct test counts when nested tests fail', function (done) { |
| 64 | + var fixture = path.join('nested-tests', 'tdd-nested'); |
| 65 | + runMochaJSON(fixture, ['--ui', 'tdd'], function (err, res) { |
| 66 | + if (err) { |
| 67 | + return done(err); |
| 68 | + } |
| 69 | + expect(res, 'to have failed') |
| 70 | + .and('to have passed test count', 2) // 'normal test' and 'another normal test' should pass |
| 71 | + .and('to have failed test count', 1); // 'outer test with nested test' should fail |
| 72 | + done(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('async nested tests', function () { |
| 78 | + it('should handle synchronous nested tests', function (done) { |
| 79 | + var fixture = path.join('nested-tests', 'bdd-async-nested'); |
| 80 | + var spawnOpts = {stdio: 'pipe'}; |
| 81 | + runMocha( |
| 82 | + fixture, |
| 83 | + ['--ui', 'bdd', '--grep', 'sync nested test'], |
| 84 | + function (err, res) { |
| 85 | + if (err) { |
| 86 | + return done(err); |
| 87 | + } |
| 88 | + expect(res, 'to have failed with output', new RegExp(nestedTestErrorMessage)); |
| 89 | + expect(res, 'to have failed with output', /nested in sync.*detected inside test.*sync nested test/); |
| 90 | + done(); |
| 91 | + }, |
| 92 | + spawnOpts |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should handle async/await nested tests', function (done) { |
| 97 | + var fixture = path.join('nested-tests', 'bdd-async-nested'); |
| 98 | + var spawnOpts = {stdio: 'pipe'}; |
| 99 | + runMocha( |
| 100 | + fixture, |
| 101 | + ['--ui', 'bdd', '--grep', 'async/await nested test'], |
| 102 | + function (err, res) { |
| 103 | + if (err) { |
| 104 | + return done(err); |
| 105 | + } |
| 106 | + expect(res, 'to have failed with output', new RegExp(nestedTestErrorMessage)); |
| 107 | + expect(res, 'to have failed with output', /nested in async.*detected inside test.*async\/await nested test/); |
| 108 | + done(); |
| 109 | + }, |
| 110 | + spawnOpts |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should handle callback-based nested tests as uncaught errors', function (done) { |
| 115 | + var fixture = path.join('nested-tests', 'bdd-async-nested'); |
| 116 | + var spawnOpts = {stdio: 'pipe'}; |
| 117 | + runMocha( |
| 118 | + fixture, |
| 119 | + ['--ui', 'bdd', '--grep', 'callback nested test'], |
| 120 | + function (err, res) { |
| 121 | + if (err) { |
| 122 | + return done(err); |
| 123 | + } |
| 124 | + expect(res, 'to have failed with output', new RegExp(nestedTestErrorMessage)); |
| 125 | + expect(res, 'to have failed with output', /Uncaught.*nested in callback.*detected inside test.*callback nested test/); |
| 126 | + done(); |
| 127 | + }, |
| 128 | + spawnOpts |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should handle promise-based nested tests as uncaught errors', function (done) { |
| 133 | + var fixture = path.join('nested-tests', 'bdd-async-nested'); |
| 134 | + var spawnOpts = {stdio: 'pipe'}; |
| 135 | + runMocha( |
| 136 | + fixture, |
| 137 | + ['--ui', 'bdd', '--grep', 'promise nested test'], |
| 138 | + function (err, res) { |
| 139 | + if (err) { |
| 140 | + return done(err); |
| 141 | + } |
| 142 | + expect(res, 'to have failed with output', new RegExp(nestedTestErrorMessage)); |
| 143 | + expect(res, 'to have failed with output', /Uncaught.*nested in promise.*detected inside test.*promise nested test/); |
| 144 | + done(); |
| 145 | + }, |
| 146 | + spawnOpts |
| 147 | + ); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe('error details', function () { |
| 152 | + it('should provide helpful error messages with test names', function (done) { |
| 153 | + var fixture = path.join('nested-tests', 'bdd-nested'); |
| 154 | + var spawnOpts = {stdio: 'pipe'}; |
| 155 | + runMocha( |
| 156 | + fixture, |
| 157 | + ['--ui', 'bdd'], |
| 158 | + function (err, res) { |
| 159 | + if (err) { |
| 160 | + return done(err); |
| 161 | + } |
| 162 | + expect(res, 'to have failed with output', /Error: Nested test "inner nested test"/); |
| 163 | + expect(res, 'to have failed with output', /detected inside test "outer test with nested test"/); |
| 164 | + expect(res, 'to have failed with output', /createUnsupportedError/); |
| 165 | + done(); |
| 166 | + }, |
| 167 | + spawnOpts |
| 168 | + ); |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments