|
| 1 | +import * as common from '../common/index.mjs'; |
| 2 | +import * as fixtures from '../common/fixtures.mjs'; |
| 3 | +import { describe, it, run } from 'node:test'; |
| 4 | +import { join } from 'node:path'; |
| 5 | + |
| 6 | +const testFixtures = fixtures.path('test-runner', 'plan'); |
| 7 | + |
| 8 | +describe('input validation', () => { |
| 9 | + it('throws if options is not an object', (t) => { |
| 10 | + t.assert.throws(() => { |
| 11 | + t.plan(1, null); |
| 12 | + }, { |
| 13 | + code: 'ERR_INVALID_ARG_TYPE', |
| 14 | + message: /The "options" argument must be of type object/, |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + it('throws if options.wait is not a number or a boolean', (t) => { |
| 19 | + t.assert.throws(() => { |
| 20 | + t.plan(1, { wait: 'foo' }); |
| 21 | + }, { |
| 22 | + code: 'ERR_INVALID_ARG_TYPE', |
| 23 | + message: /The "options\.wait" property must be one of type boolean or number\. Received type string/, |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('throws if count is not a number', (t) => { |
| 28 | + t.assert.throws(() => { |
| 29 | + t.plan('foo'); |
| 30 | + }, { |
| 31 | + code: 'ERR_INVALID_ARG_TYPE', |
| 32 | + message: /The "count" argument must be of type number/, |
| 33 | + }); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +describe('test planning', () => { |
| 38 | + it('should pass when assertions match plan', async () => { |
| 39 | + const stream = run({ |
| 40 | + files: [join(testFixtures, 'match.mjs')] |
| 41 | + }); |
| 42 | + |
| 43 | + stream.on('test:fail', common.mustNotCall()); |
| 44 | + stream.on('test:pass', common.mustCall(1)); |
| 45 | + |
| 46 | + // eslint-disable-next-line no-unused-vars |
| 47 | + for await (const _ of stream); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should fail when less assertions than planned', async () => { |
| 51 | + const stream = run({ |
| 52 | + files: [join(testFixtures, 'less.mjs')] |
| 53 | + }); |
| 54 | + |
| 55 | + stream.on('test:fail', common.mustCall(1)); |
| 56 | + stream.on('test:pass', common.mustNotCall()); |
| 57 | + |
| 58 | + // eslint-disable-next-line no-unused-vars |
| 59 | + for await (const _ of stream); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should fail when more assertions than planned', async () => { |
| 63 | + const stream = run({ |
| 64 | + files: [join(testFixtures, 'more.mjs')] |
| 65 | + }); |
| 66 | + |
| 67 | + stream.on('test:fail', common.mustCall(1)); |
| 68 | + stream.on('test:pass', common.mustNotCall()); |
| 69 | + |
| 70 | + // eslint-disable-next-line no-unused-vars |
| 71 | + for await (const _ of stream); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should handle plan with subtests correctly', async () => { |
| 75 | + const stream = run({ |
| 76 | + files: [join(testFixtures, 'subtest.mjs')] |
| 77 | + }); |
| 78 | + |
| 79 | + stream.on('test:fail', common.mustNotCall()); |
| 80 | + stream.on('test:pass', common.mustCall(2)); // Parent + child test |
| 81 | + |
| 82 | + // eslint-disable-next-line no-unused-vars |
| 83 | + for await (const _ of stream); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should handle plan via options', async () => { |
| 87 | + const stream = run({ |
| 88 | + files: [join(testFixtures, 'plan-via-options.mjs')] |
| 89 | + }); |
| 90 | + |
| 91 | + stream.on('test:fail', common.mustCall(1)); |
| 92 | + stream.on('test:pass', common.mustCall(1)); |
| 93 | + |
| 94 | + // eslint-disable-next-line no-unused-vars |
| 95 | + for await (const _ of stream); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should handle streaming with plan', async () => { |
| 99 | + const stream = run({ |
| 100 | + files: [join(testFixtures, 'streaming.mjs')] |
| 101 | + }); |
| 102 | + |
| 103 | + stream.on('test:fail', common.mustNotCall()); |
| 104 | + stream.on('test:pass', common.mustCall(1)); |
| 105 | + |
| 106 | + // eslint-disable-next-line no-unused-vars |
| 107 | + for await (const _ of stream); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should handle nested subtests with plan', async () => { |
| 111 | + const stream = run({ |
| 112 | + files: [join(testFixtures, 'nested-subtests.mjs')] |
| 113 | + }); |
| 114 | + |
| 115 | + stream.on('test:fail', common.mustNotCall()); |
| 116 | + stream.on('test:pass', common.mustCall(3)); // Parent + 2 levels of nesting |
| 117 | + |
| 118 | + // eslint-disable-next-line no-unused-vars |
| 119 | + for await (const _ of stream); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('with timeout', () => { |
| 123 | + it('should handle basic timeout scenarios', async () => { |
| 124 | + const stream = run({ |
| 125 | + files: [join(testFixtures, 'timeout-basic.mjs')] |
| 126 | + }); |
| 127 | + |
| 128 | + stream.on('test:fail', common.mustCall(1)); |
| 129 | + stream.on('test:pass', common.mustCall(1)); |
| 130 | + |
| 131 | + // eslint-disable-next-line no-unused-vars |
| 132 | + for await (const _ of stream); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should fail when timeout expires before plan is met', async (t) => { |
| 136 | + const stream = run({ |
| 137 | + files: [join(testFixtures, 'timeout-expired.mjs')] |
| 138 | + }); |
| 139 | + |
| 140 | + stream.on('test:fail', common.mustCall(1)); |
| 141 | + stream.on('test:pass', common.mustNotCall()); |
| 142 | + |
| 143 | + // eslint-disable-next-line no-unused-vars |
| 144 | + for await (const _ of stream); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should handle wait:true option specifically', async () => { |
| 148 | + const stream = run({ |
| 149 | + files: [join(testFixtures, 'timeout-wait-true.mjs')] |
| 150 | + }); |
| 151 | + |
| 152 | + stream.on('test:fail', common.mustCall(1)); |
| 153 | + stream.on('test:pass', common.mustCall(1)); |
| 154 | + |
| 155 | + // eslint-disable-next-line no-unused-vars |
| 156 | + for await (const _ of stream); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should handle wait:false option (should not wait)', async () => { |
| 160 | + const stream = run({ |
| 161 | + files: [join(testFixtures, 'timeout-wait-false.mjs')] |
| 162 | + }); |
| 163 | + |
| 164 | + stream.on('test:fail', common.mustCall(1)); // Fails because plan is not met immediately |
| 165 | + stream.on('test:pass', common.mustNotCall()); |
| 166 | + |
| 167 | + // eslint-disable-next-line no-unused-vars |
| 168 | + for await (const _ of stream); |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments