11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_cirequire('../../../common'); 31cb0ef41Sopenharmony_ciconst assert = require('node:assert'); 41cb0ef41Sopenharmony_ciconst { describe, it, test } = require('node:test'); 51cb0ef41Sopenharmony_ciconst util = require('util'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciit.todo('sync pass todo', () => { 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci}); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciit('sync pass todo with message', { todo: 'this is a passing todo' }, () => { 131cb0ef41Sopenharmony_ci}); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciit.todo('sync todo', () => { 161cb0ef41Sopenharmony_ci throw new Error('should not count as a failure'); 171cb0ef41Sopenharmony_ci}); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciit('sync todo with message', { todo: 'this is a failing todo' }, () => { 201cb0ef41Sopenharmony_ci throw new Error('should not count as a failure'); 211cb0ef41Sopenharmony_ci}); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciit.skip('sync skip pass', () => { 241cb0ef41Sopenharmony_ci}); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciit('sync skip pass with message', { skip: 'this is skipped' }, () => { 271cb0ef41Sopenharmony_ci}); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciit('sync pass', () => { 301cb0ef41Sopenharmony_ci}); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciit('sync throw fail', () => { 331cb0ef41Sopenharmony_ci throw new Error('thrown from sync throw fail'); 341cb0ef41Sopenharmony_ci}); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciit.skip('async skip pass', async () => { 371cb0ef41Sopenharmony_ci}); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ciit('async pass', async () => { 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci}); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_citest('mixing describe/it and test should work', () => {}); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ciit('async throw fail', async () => { 461cb0ef41Sopenharmony_ci throw new Error('thrown from async throw fail'); 471cb0ef41Sopenharmony_ci}); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ciit('async skip fail', async (t, done) => { 501cb0ef41Sopenharmony_ci t.skip(); 511cb0ef41Sopenharmony_ci throw new Error('thrown from async throw fail'); 521cb0ef41Sopenharmony_ci}); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ciit('async assertion fail', async () => { 551cb0ef41Sopenharmony_ci // Make sure the assert module is handled. 561cb0ef41Sopenharmony_ci assert.strictEqual(true, false); 571cb0ef41Sopenharmony_ci}); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ciit('resolve pass', () => { 601cb0ef41Sopenharmony_ci return Promise.resolve(); 611cb0ef41Sopenharmony_ci}); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ciit('reject fail', () => { 641cb0ef41Sopenharmony_ci return Promise.reject(new Error('rejected from reject fail')); 651cb0ef41Sopenharmony_ci}); 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ciit('unhandled rejection - passes but warns', () => { 681cb0ef41Sopenharmony_ci Promise.reject(new Error('rejected from unhandled rejection fail')); 691cb0ef41Sopenharmony_ci}); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ciit('async unhandled rejection - passes but warns', async () => { 721cb0ef41Sopenharmony_ci Promise.reject(new Error('rejected from async unhandled rejection fail')); 731cb0ef41Sopenharmony_ci}); 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ciit('immediate throw - passes but warns', () => { 761cb0ef41Sopenharmony_ci setImmediate(() => { 771cb0ef41Sopenharmony_ci throw new Error('thrown from immediate throw fail'); 781cb0ef41Sopenharmony_ci }); 791cb0ef41Sopenharmony_ci}); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ciit('immediate reject - passes but warns', () => { 821cb0ef41Sopenharmony_ci setImmediate(() => { 831cb0ef41Sopenharmony_ci Promise.reject(new Error('rejected from immediate reject fail')); 841cb0ef41Sopenharmony_ci }); 851cb0ef41Sopenharmony_ci}); 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ciit('immediate resolve pass', () => { 881cb0ef41Sopenharmony_ci return new Promise((resolve) => { 891cb0ef41Sopenharmony_ci setImmediate(() => { 901cb0ef41Sopenharmony_ci resolve(); 911cb0ef41Sopenharmony_ci }); 921cb0ef41Sopenharmony_ci }); 931cb0ef41Sopenharmony_ci}); 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_cidescribe('subtest sync throw fail', () => { 961cb0ef41Sopenharmony_ci it('+sync throw fail', () => { 971cb0ef41Sopenharmony_ci throw new Error('thrown from subtest sync throw fail'); 981cb0ef41Sopenharmony_ci }); 991cb0ef41Sopenharmony_ci test('mixing describe/it and test should work', () => {}); 1001cb0ef41Sopenharmony_ci}); 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ciit('sync throw non-error fail', async () => { 1031cb0ef41Sopenharmony_ci throw Symbol('thrown symbol from sync throw non-error fail'); 1041cb0ef41Sopenharmony_ci}); 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_cidescribe('level 0a', { concurrency: 4 }, () => { 1071cb0ef41Sopenharmony_ci it('level 1a', async () => { 1081cb0ef41Sopenharmony_ci const p1a = new Promise((resolve) => { 1091cb0ef41Sopenharmony_ci setTimeout(() => { 1101cb0ef41Sopenharmony_ci resolve(); 1111cb0ef41Sopenharmony_ci }, 100); 1121cb0ef41Sopenharmony_ci }); 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci return p1a; 1151cb0ef41Sopenharmony_ci }); 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci it('level 1b', async () => { 1181cb0ef41Sopenharmony_ci const p1b = new Promise((resolve) => { 1191cb0ef41Sopenharmony_ci resolve(); 1201cb0ef41Sopenharmony_ci }); 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci return p1b; 1231cb0ef41Sopenharmony_ci }); 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci it('level 1c', async () => { 1261cb0ef41Sopenharmony_ci const p1c = new Promise((resolve) => { 1271cb0ef41Sopenharmony_ci setTimeout(() => { 1281cb0ef41Sopenharmony_ci resolve(); 1291cb0ef41Sopenharmony_ci }, 200); 1301cb0ef41Sopenharmony_ci }); 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci return p1c; 1331cb0ef41Sopenharmony_ci }); 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci it('level 1d', async () => { 1361cb0ef41Sopenharmony_ci const p1c = new Promise((resolve) => { 1371cb0ef41Sopenharmony_ci setTimeout(() => { 1381cb0ef41Sopenharmony_ci resolve(); 1391cb0ef41Sopenharmony_ci }, 150); 1401cb0ef41Sopenharmony_ci }); 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci return p1c; 1431cb0ef41Sopenharmony_ci }); 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ci const p0a = new Promise((resolve) => { 1461cb0ef41Sopenharmony_ci setTimeout(() => { 1471cb0ef41Sopenharmony_ci resolve(); 1481cb0ef41Sopenharmony_ci }, 300); 1491cb0ef41Sopenharmony_ci }); 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ci return p0a; 1521cb0ef41Sopenharmony_ci}); 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_cidescribe('invalid subtest - pass but subtest fails', () => { 1561cb0ef41Sopenharmony_ci setImmediate(() => { 1571cb0ef41Sopenharmony_ci it('invalid subtest fail', () => { 1581cb0ef41Sopenharmony_ci throw new Error('this should not be thrown'); 1591cb0ef41Sopenharmony_ci }); 1601cb0ef41Sopenharmony_ci }); 1611cb0ef41Sopenharmony_ci}); 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ciit.skip('sync skip option', () => { 1641cb0ef41Sopenharmony_ci throw new Error('this should not be executed'); 1651cb0ef41Sopenharmony_ci}); 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ciit('sync skip option with message', { skip: 'this is skipped' }, () => { 1681cb0ef41Sopenharmony_ci throw new Error('this should not be executed'); 1691cb0ef41Sopenharmony_ci}); 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ciit('sync skip option is false fail', { skip: false }, () => { 1721cb0ef41Sopenharmony_ci throw new Error('this should be executed'); 1731cb0ef41Sopenharmony_ci}); 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_ci// A test with no arguments provided. 1761cb0ef41Sopenharmony_ciit(); 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci// A test with only a named function provided. 1791cb0ef41Sopenharmony_ciit(function functionOnly() {}); 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_ci// A test with only an anonymous function provided. 1821cb0ef41Sopenharmony_ciit(() => {}); 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci// A test with only a name provided. 1851cb0ef41Sopenharmony_ciit('test with only a name provided'); 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ci// A test with an empty string name. 1881cb0ef41Sopenharmony_ciit(''); 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_ci// A test with only options provided. 1911cb0ef41Sopenharmony_ciit({ skip: true }); 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_ci// A test with only a name and options provided. 1941cb0ef41Sopenharmony_ciit('test with a name and options provided', { skip: true }); 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci// A test with only options and a function provided. 1971cb0ef41Sopenharmony_ciit({ skip: true }, function functionAndOptions() {}); 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_ciit('callback pass', (t, done) => { 2001cb0ef41Sopenharmony_ci setImmediate(done); 2011cb0ef41Sopenharmony_ci}); 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ciit('callback fail', (t, done) => { 2041cb0ef41Sopenharmony_ci setImmediate(() => { 2051cb0ef41Sopenharmony_ci done(new Error('callback failure')); 2061cb0ef41Sopenharmony_ci }); 2071cb0ef41Sopenharmony_ci}); 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ciit('sync t is this in test', function(t) { 2101cb0ef41Sopenharmony_ci assert.strictEqual(this, t); 2111cb0ef41Sopenharmony_ci}); 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ciit('async t is this in test', async function(t) { 2141cb0ef41Sopenharmony_ci assert.strictEqual(this, t); 2151cb0ef41Sopenharmony_ci}); 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ciit('callback t is this in test', function(t, done) { 2181cb0ef41Sopenharmony_ci assert.strictEqual(this, t); 2191cb0ef41Sopenharmony_ci done(); 2201cb0ef41Sopenharmony_ci}); 2211cb0ef41Sopenharmony_ci 2221cb0ef41Sopenharmony_ciit('callback also returns a Promise', async (t, done) => { 2231cb0ef41Sopenharmony_ci throw new Error('thrown from callback also returns a Promise'); 2241cb0ef41Sopenharmony_ci}); 2251cb0ef41Sopenharmony_ci 2261cb0ef41Sopenharmony_ciit('callback throw', (t, done) => { 2271cb0ef41Sopenharmony_ci throw new Error('thrown from callback throw'); 2281cb0ef41Sopenharmony_ci}); 2291cb0ef41Sopenharmony_ci 2301cb0ef41Sopenharmony_ciit('callback called twice', (t, done) => { 2311cb0ef41Sopenharmony_ci done(); 2321cb0ef41Sopenharmony_ci done(); 2331cb0ef41Sopenharmony_ci}); 2341cb0ef41Sopenharmony_ci 2351cb0ef41Sopenharmony_ciit('callback called twice in different ticks', (t, done) => { 2361cb0ef41Sopenharmony_ci setImmediate(done); 2371cb0ef41Sopenharmony_ci done(); 2381cb0ef41Sopenharmony_ci}); 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_ciit('callback called twice in future tick', (t, done) => { 2411cb0ef41Sopenharmony_ci setImmediate(() => { 2421cb0ef41Sopenharmony_ci done(); 2431cb0ef41Sopenharmony_ci done(); 2441cb0ef41Sopenharmony_ci }); 2451cb0ef41Sopenharmony_ci}); 2461cb0ef41Sopenharmony_ci 2471cb0ef41Sopenharmony_ciit('callback async throw', (t, done) => { 2481cb0ef41Sopenharmony_ci setImmediate(() => { 2491cb0ef41Sopenharmony_ci throw new Error('thrown from callback async throw'); 2501cb0ef41Sopenharmony_ci }); 2511cb0ef41Sopenharmony_ci}); 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ciit('callback async throw after done', (t, done) => { 2541cb0ef41Sopenharmony_ci setImmediate(() => { 2551cb0ef41Sopenharmony_ci throw new Error('thrown from callback async throw after done'); 2561cb0ef41Sopenharmony_ci }); 2571cb0ef41Sopenharmony_ci 2581cb0ef41Sopenharmony_ci done(); 2591cb0ef41Sopenharmony_ci}); 2601cb0ef41Sopenharmony_ci 2611cb0ef41Sopenharmony_ciit('custom inspect symbol fail', () => { 2621cb0ef41Sopenharmony_ci const obj = { 2631cb0ef41Sopenharmony_ci [util.inspect.custom]() { 2641cb0ef41Sopenharmony_ci return 'customized'; 2651cb0ef41Sopenharmony_ci }, 2661cb0ef41Sopenharmony_ci foo: 1, 2671cb0ef41Sopenharmony_ci }; 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_ci throw obj; 2701cb0ef41Sopenharmony_ci}); 2711cb0ef41Sopenharmony_ci 2721cb0ef41Sopenharmony_ciit('custom inspect symbol that throws fail', () => { 2731cb0ef41Sopenharmony_ci const obj = { 2741cb0ef41Sopenharmony_ci [util.inspect.custom]() { 2751cb0ef41Sopenharmony_ci throw new Error('bad-inspect'); 2761cb0ef41Sopenharmony_ci }, 2771cb0ef41Sopenharmony_ci foo: 1, 2781cb0ef41Sopenharmony_ci }; 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci throw obj; 2811cb0ef41Sopenharmony_ci}); 2821cb0ef41Sopenharmony_ci 2831cb0ef41Sopenharmony_cidescribe('subtest sync throw fails', () => { 2841cb0ef41Sopenharmony_ci it('sync throw fails at first', () => { 2851cb0ef41Sopenharmony_ci throw new Error('thrown from subtest sync throw fails at first'); 2861cb0ef41Sopenharmony_ci }); 2871cb0ef41Sopenharmony_ci it('sync throw fails at second', () => { 2881cb0ef41Sopenharmony_ci throw new Error('thrown from subtest sync throw fails at second'); 2891cb0ef41Sopenharmony_ci }); 2901cb0ef41Sopenharmony_ci}); 2911cb0ef41Sopenharmony_ci 2921cb0ef41Sopenharmony_cidescribe('describe sync throw fails', () => { 2931cb0ef41Sopenharmony_ci it('should not run', () => {}); 2941cb0ef41Sopenharmony_ci throw new Error('thrown from describe'); 2951cb0ef41Sopenharmony_ci}); 2961cb0ef41Sopenharmony_ci 2971cb0ef41Sopenharmony_cidescribe('describe async throw fails', async () => { 2981cb0ef41Sopenharmony_ci it('should not run', () => {}); 2991cb0ef41Sopenharmony_ci throw new Error('thrown from describe'); 3001cb0ef41Sopenharmony_ci}); 3011cb0ef41Sopenharmony_ci 3021cb0ef41Sopenharmony_cidescribe('timeouts', () => { 3031cb0ef41Sopenharmony_ci it('timed out async test', { timeout: 5 }, async () => { 3041cb0ef41Sopenharmony_ci return new Promise((resolve) => { 3051cb0ef41Sopenharmony_ci setTimeout(resolve, 100); 3061cb0ef41Sopenharmony_ci }); 3071cb0ef41Sopenharmony_ci }); 3081cb0ef41Sopenharmony_ci 3091cb0ef41Sopenharmony_ci it('timed out callback test', { timeout: 5 }, (t, done) => { 3101cb0ef41Sopenharmony_ci setTimeout(done, 100); 3111cb0ef41Sopenharmony_ci }); 3121cb0ef41Sopenharmony_ci 3131cb0ef41Sopenharmony_ci 3141cb0ef41Sopenharmony_ci it('large timeout async test is ok', { timeout: 30_000_000 }, async () => { 3151cb0ef41Sopenharmony_ci return new Promise((resolve) => { 3161cb0ef41Sopenharmony_ci setTimeout(resolve, 10); 3171cb0ef41Sopenharmony_ci }); 3181cb0ef41Sopenharmony_ci }); 3191cb0ef41Sopenharmony_ci 3201cb0ef41Sopenharmony_ci it('large timeout callback test is ok', { timeout: 30_000_000 }, (t, done) => { 3211cb0ef41Sopenharmony_ci setTimeout(done, 10); 3221cb0ef41Sopenharmony_ci }); 3231cb0ef41Sopenharmony_ci}); 3241cb0ef41Sopenharmony_ci 3251cb0ef41Sopenharmony_cidescribe('successful thenable', () => { 3261cb0ef41Sopenharmony_ci it('successful thenable', () => { 3271cb0ef41Sopenharmony_ci let thenCalled = false; 3281cb0ef41Sopenharmony_ci return { 3291cb0ef41Sopenharmony_ci get then() { 3301cb0ef41Sopenharmony_ci if (thenCalled) throw new Error(); 3311cb0ef41Sopenharmony_ci thenCalled = true; 3321cb0ef41Sopenharmony_ci return (successHandler) => successHandler(); 3331cb0ef41Sopenharmony_ci }, 3341cb0ef41Sopenharmony_ci }; 3351cb0ef41Sopenharmony_ci }); 3361cb0ef41Sopenharmony_ci 3371cb0ef41Sopenharmony_ci it('rejected thenable', () => { 3381cb0ef41Sopenharmony_ci let thenCalled = false; 3391cb0ef41Sopenharmony_ci return { 3401cb0ef41Sopenharmony_ci get then() { 3411cb0ef41Sopenharmony_ci if (thenCalled) throw new Error(); 3421cb0ef41Sopenharmony_ci thenCalled = true; 3431cb0ef41Sopenharmony_ci return (_, errorHandler) => errorHandler(new Error('custom error')); 3441cb0ef41Sopenharmony_ci }, 3451cb0ef41Sopenharmony_ci }; 3461cb0ef41Sopenharmony_ci }); 3471cb0ef41Sopenharmony_ci 3481cb0ef41Sopenharmony_ci let thenCalled = false; 3491cb0ef41Sopenharmony_ci return { 3501cb0ef41Sopenharmony_ci get then() { 3511cb0ef41Sopenharmony_ci if (thenCalled) throw new Error(); 3521cb0ef41Sopenharmony_ci thenCalled = true; 3531cb0ef41Sopenharmony_ci return (successHandler) => successHandler(); 3541cb0ef41Sopenharmony_ci }, 3551cb0ef41Sopenharmony_ci }; 3561cb0ef41Sopenharmony_ci}); 3571cb0ef41Sopenharmony_ci 3581cb0ef41Sopenharmony_cidescribe('rejected thenable', () => { 3591cb0ef41Sopenharmony_ci let thenCalled = false; 3601cb0ef41Sopenharmony_ci return { 3611cb0ef41Sopenharmony_ci get then() { 3621cb0ef41Sopenharmony_ci if (thenCalled) throw new Error(); 3631cb0ef41Sopenharmony_ci thenCalled = true; 3641cb0ef41Sopenharmony_ci return (_, errorHandler) => errorHandler(new Error('custom error')); 3651cb0ef41Sopenharmony_ci }, 3661cb0ef41Sopenharmony_ci }; 3671cb0ef41Sopenharmony_ci}); 3681cb0ef41Sopenharmony_ci 3691cb0ef41Sopenharmony_cidescribe("async describe function", async () => { 3701cb0ef41Sopenharmony_ci await null; 3711cb0ef41Sopenharmony_ci 3721cb0ef41Sopenharmony_ci await it("it inside describe 1", async () => { 3731cb0ef41Sopenharmony_ci await null 3741cb0ef41Sopenharmony_ci }); 3751cb0ef41Sopenharmony_ci await it("it inside describe 2", async () => { 3761cb0ef41Sopenharmony_ci await null; 3771cb0ef41Sopenharmony_ci }); 3781cb0ef41Sopenharmony_ci 3791cb0ef41Sopenharmony_ci describe("inner describe", async () => { 3801cb0ef41Sopenharmony_ci await null; 3811cb0ef41Sopenharmony_ci 3821cb0ef41Sopenharmony_ci it("it inside inner describe", async () => { 3831cb0ef41Sopenharmony_ci await null; 3841cb0ef41Sopenharmony_ci }); 3851cb0ef41Sopenharmony_ci }); 3861cb0ef41Sopenharmony_ci}); 387