1'use strict'; 2require('../../../common'); 3const assert = require('node:assert'); 4const { describe, it, test } = require('node:test'); 5const util = require('util'); 6 7 8it.todo('sync pass todo', () => { 9 10}); 11 12it('sync pass todo with message', { todo: 'this is a passing todo' }, () => { 13}); 14 15it.todo('sync todo', () => { 16 throw new Error('should not count as a failure'); 17}); 18 19it('sync todo with message', { todo: 'this is a failing todo' }, () => { 20 throw new Error('should not count as a failure'); 21}); 22 23it.skip('sync skip pass', () => { 24}); 25 26it('sync skip pass with message', { skip: 'this is skipped' }, () => { 27}); 28 29it('sync pass', () => { 30}); 31 32it('sync throw fail', () => { 33 throw new Error('thrown from sync throw fail'); 34}); 35 36it.skip('async skip pass', async () => { 37}); 38 39it('async pass', async () => { 40 41}); 42 43test('mixing describe/it and test should work', () => {}); 44 45it('async throw fail', async () => { 46 throw new Error('thrown from async throw fail'); 47}); 48 49it('async skip fail', async (t, done) => { 50 t.skip(); 51 throw new Error('thrown from async throw fail'); 52}); 53 54it('async assertion fail', async () => { 55 // Make sure the assert module is handled. 56 assert.strictEqual(true, false); 57}); 58 59it('resolve pass', () => { 60 return Promise.resolve(); 61}); 62 63it('reject fail', () => { 64 return Promise.reject(new Error('rejected from reject fail')); 65}); 66 67it('unhandled rejection - passes but warns', () => { 68 Promise.reject(new Error('rejected from unhandled rejection fail')); 69}); 70 71it('async unhandled rejection - passes but warns', async () => { 72 Promise.reject(new Error('rejected from async unhandled rejection fail')); 73}); 74 75it('immediate throw - passes but warns', () => { 76 setImmediate(() => { 77 throw new Error('thrown from immediate throw fail'); 78 }); 79}); 80 81it('immediate reject - passes but warns', () => { 82 setImmediate(() => { 83 Promise.reject(new Error('rejected from immediate reject fail')); 84 }); 85}); 86 87it('immediate resolve pass', () => { 88 return new Promise((resolve) => { 89 setImmediate(() => { 90 resolve(); 91 }); 92 }); 93}); 94 95describe('subtest sync throw fail', () => { 96 it('+sync throw fail', () => { 97 throw new Error('thrown from subtest sync throw fail'); 98 }); 99 test('mixing describe/it and test should work', () => {}); 100}); 101 102it('sync throw non-error fail', async () => { 103 throw Symbol('thrown symbol from sync throw non-error fail'); 104}); 105 106describe('level 0a', { concurrency: 4 }, () => { 107 it('level 1a', async () => { 108 const p1a = new Promise((resolve) => { 109 setTimeout(() => { 110 resolve(); 111 }, 100); 112 }); 113 114 return p1a; 115 }); 116 117 it('level 1b', async () => { 118 const p1b = new Promise((resolve) => { 119 resolve(); 120 }); 121 122 return p1b; 123 }); 124 125 it('level 1c', async () => { 126 const p1c = new Promise((resolve) => { 127 setTimeout(() => { 128 resolve(); 129 }, 200); 130 }); 131 132 return p1c; 133 }); 134 135 it('level 1d', async () => { 136 const p1c = new Promise((resolve) => { 137 setTimeout(() => { 138 resolve(); 139 }, 150); 140 }); 141 142 return p1c; 143 }); 144 145 const p0a = new Promise((resolve) => { 146 setTimeout(() => { 147 resolve(); 148 }, 300); 149 }); 150 151 return p0a; 152}); 153 154 155describe('invalid subtest - pass but subtest fails', () => { 156 setImmediate(() => { 157 it('invalid subtest fail', () => { 158 throw new Error('this should not be thrown'); 159 }); 160 }); 161}); 162 163it.skip('sync skip option', () => { 164 throw new Error('this should not be executed'); 165}); 166 167it('sync skip option with message', { skip: 'this is skipped' }, () => { 168 throw new Error('this should not be executed'); 169}); 170 171it('sync skip option is false fail', { skip: false }, () => { 172 throw new Error('this should be executed'); 173}); 174 175// A test with no arguments provided. 176it(); 177 178// A test with only a named function provided. 179it(function functionOnly() {}); 180 181// A test with only an anonymous function provided. 182it(() => {}); 183 184// A test with only a name provided. 185it('test with only a name provided'); 186 187// A test with an empty string name. 188it(''); 189 190// A test with only options provided. 191it({ skip: true }); 192 193// A test with only a name and options provided. 194it('test with a name and options provided', { skip: true }); 195 196// A test with only options and a function provided. 197it({ skip: true }, function functionAndOptions() {}); 198 199it('callback pass', (t, done) => { 200 setImmediate(done); 201}); 202 203it('callback fail', (t, done) => { 204 setImmediate(() => { 205 done(new Error('callback failure')); 206 }); 207}); 208 209it('sync t is this in test', function(t) { 210 assert.strictEqual(this, t); 211}); 212 213it('async t is this in test', async function(t) { 214 assert.strictEqual(this, t); 215}); 216 217it('callback t is this in test', function(t, done) { 218 assert.strictEqual(this, t); 219 done(); 220}); 221 222it('callback also returns a Promise', async (t, done) => { 223 throw new Error('thrown from callback also returns a Promise'); 224}); 225 226it('callback throw', (t, done) => { 227 throw new Error('thrown from callback throw'); 228}); 229 230it('callback called twice', (t, done) => { 231 done(); 232 done(); 233}); 234 235it('callback called twice in different ticks', (t, done) => { 236 setImmediate(done); 237 done(); 238}); 239 240it('callback called twice in future tick', (t, done) => { 241 setImmediate(() => { 242 done(); 243 done(); 244 }); 245}); 246 247it('callback async throw', (t, done) => { 248 setImmediate(() => { 249 throw new Error('thrown from callback async throw'); 250 }); 251}); 252 253it('callback async throw after done', (t, done) => { 254 setImmediate(() => { 255 throw new Error('thrown from callback async throw after done'); 256 }); 257 258 done(); 259}); 260 261it('custom inspect symbol fail', () => { 262 const obj = { 263 [util.inspect.custom]() { 264 return 'customized'; 265 }, 266 foo: 1, 267 }; 268 269 throw obj; 270}); 271 272it('custom inspect symbol that throws fail', () => { 273 const obj = { 274 [util.inspect.custom]() { 275 throw new Error('bad-inspect'); 276 }, 277 foo: 1, 278 }; 279 280 throw obj; 281}); 282 283describe('subtest sync throw fails', () => { 284 it('sync throw fails at first', () => { 285 throw new Error('thrown from subtest sync throw fails at first'); 286 }); 287 it('sync throw fails at second', () => { 288 throw new Error('thrown from subtest sync throw fails at second'); 289 }); 290}); 291 292describe('describe sync throw fails', () => { 293 it('should not run', () => {}); 294 throw new Error('thrown from describe'); 295}); 296 297describe('describe async throw fails', async () => { 298 it('should not run', () => {}); 299 throw new Error('thrown from describe'); 300}); 301 302describe('timeouts', () => { 303 it('timed out async test', { timeout: 5 }, async () => { 304 return new Promise((resolve) => { 305 setTimeout(resolve, 100); 306 }); 307 }); 308 309 it('timed out callback test', { timeout: 5 }, (t, done) => { 310 setTimeout(done, 100); 311 }); 312 313 314 it('large timeout async test is ok', { timeout: 30_000_000 }, async () => { 315 return new Promise((resolve) => { 316 setTimeout(resolve, 10); 317 }); 318 }); 319 320 it('large timeout callback test is ok', { timeout: 30_000_000 }, (t, done) => { 321 setTimeout(done, 10); 322 }); 323}); 324 325describe('successful thenable', () => { 326 it('successful thenable', () => { 327 let thenCalled = false; 328 return { 329 get then() { 330 if (thenCalled) throw new Error(); 331 thenCalled = true; 332 return (successHandler) => successHandler(); 333 }, 334 }; 335 }); 336 337 it('rejected thenable', () => { 338 let thenCalled = false; 339 return { 340 get then() { 341 if (thenCalled) throw new Error(); 342 thenCalled = true; 343 return (_, errorHandler) => errorHandler(new Error('custom error')); 344 }, 345 }; 346 }); 347 348 let thenCalled = false; 349 return { 350 get then() { 351 if (thenCalled) throw new Error(); 352 thenCalled = true; 353 return (successHandler) => successHandler(); 354 }, 355 }; 356}); 357 358describe('rejected thenable', () => { 359 let thenCalled = false; 360 return { 361 get then() { 362 if (thenCalled) throw new Error(); 363 thenCalled = true; 364 return (_, errorHandler) => errorHandler(new Error('custom error')); 365 }, 366 }; 367}); 368 369describe("async describe function", async () => { 370 await null; 371 372 await it("it inside describe 1", async () => { 373 await null 374 }); 375 await it("it inside describe 2", async () => { 376 await null; 377 }); 378 379 describe("inner describe", async () => { 380 await null; 381 382 it("it inside inner describe", async () => { 383 await null; 384 }); 385 }); 386}); 387