Skip to content

Commit

Permalink
test_runner: allow nesting test within describe
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node#46544
Fixes: nodejs/node#46478
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
(cherry picked from commit 2787e2dfc2e10c584259ff34d7aad565447a84d9)
  • Loading branch information
MoLow authored and juliangruber committed Mar 16, 2023
1 parent f131a8c commit 5cebe91
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 62 deletions.
3 changes: 2 additions & 1 deletion lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ function getGlobalRoot () {
}

function test (name, options, fn) {
const subtest = getGlobalRoot().createSubtest(Test, name, options, fn)
const parent = testResources.get(executionAsyncId()) || getGlobalRoot()
const subtest = parent.createSubtest(Test, name, options, fn)
return subtest.start()
}

Expand Down
17 changes: 10 additions & 7 deletions test/message/test_runner_desctibe_it.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict'
require('../common')
const assert = require('node:assert')
const { describe, it } = require('#node:test')
const { describe, it, test } = require('node:test')
const util = require('util')

it.todo('sync pass todo', () => {
Expand Down Expand Up @@ -41,6 +41,8 @@ it('async pass', async () => {

})

test('mixing describe/it and test should work', () => {})

it('async throw fail', async () => {
throw new Error('thrown from async throw fail')
})
Expand Down Expand Up @@ -95,6 +97,7 @@ describe('subtest sync throw fail', () => {
it('+sync throw fail', () => {
throw new Error('thrown from subtest sync throw fail')
})
test('mixing describe/it and test should work', () => {})
})

it('sync throw non-error fail', async () => {
Expand All @@ -106,7 +109,7 @@ describe('level 0a', { concurrency: 4 }, () => {
const p1a = new Promise((resolve) => {
setTimeout(() => {
resolve()
}, 1000)
}, 100)
})

return p1a
Expand All @@ -124,7 +127,7 @@ describe('level 0a', { concurrency: 4 }, () => {
const p1c = new Promise((resolve) => {
setTimeout(() => {
resolve()
}, 2000)
}, 200)
})

return p1c
Expand All @@ -134,7 +137,7 @@ describe('level 0a', { concurrency: 4 }, () => {
const p1c = new Promise((resolve) => {
setTimeout(() => {
resolve()
}, 1500)
}, 150)
})

return p1c
Expand All @@ -143,7 +146,7 @@ describe('level 0a', { concurrency: 4 }, () => {
const p0a = new Promise((resolve) => {
setTimeout(() => {
resolve()
}, 3000)
}, 300)
})

return p0a
Expand Down Expand Up @@ -308,12 +311,12 @@ describe('describe async throw fails', async () => {
describe('timeouts', () => {
it('timed out async test', { timeout: 5 }, async () => {
return new Promise((resolve) => {
setTimeout(resolve, 1000)
setTimeout(resolve, 100)
})
})

it('timed out callback test', { timeout: 5 }, (done) => {
setTimeout(done, 1000)
setTimeout(done, 100)
})

it('large timeout async test is ok', { timeout: 30_000_000 }, async () => {
Expand Down

0 comments on commit 5cebe91

Please sign in to comment.