|
| 1 | +import * as common from '../common/index.mjs'; |
| 2 | +import * as fixtures from '../common/fixtures.mjs'; |
| 3 | +import { test } from 'node:test'; |
| 4 | + |
| 5 | +const testArguments = [ |
| 6 | + '--test', |
| 7 | + '--test-isolation=none', |
| 8 | +]; |
| 9 | + |
| 10 | +const testFiles = [ |
| 11 | + fixtures.path('test-runner', 'no-isolation', 'one.test.js'), |
| 12 | + fixtures.path('test-runner', 'no-isolation', 'two.test.js'), |
| 13 | +]; |
| 14 | + |
| 15 | +const order = [ |
| 16 | + 'before(): global', |
| 17 | + |
| 18 | + 'before one: <root>', |
| 19 | + 'suite one', |
| 20 | + |
| 21 | + 'before two: <root>', |
| 22 | + 'suite two', |
| 23 | + |
| 24 | + 'beforeEach(): global', |
| 25 | + 'beforeEach one: suite one - test', |
| 26 | + 'beforeEach two: suite one - test', |
| 27 | + |
| 28 | + 'suite one - test', |
| 29 | + 'afterEach(): global', |
| 30 | + 'afterEach one: suite one - test', |
| 31 | + 'afterEach two: suite one - test', |
| 32 | + |
| 33 | + 'before suite two: suite two', |
| 34 | + 'beforeEach(): global', |
| 35 | + 'beforeEach one: suite two - test', |
| 36 | + 'beforeEach two: suite two - test', |
| 37 | + |
| 38 | + 'suite two - test', |
| 39 | + 'afterEach(): global', |
| 40 | + 'afterEach one: suite two - test', |
| 41 | + 'afterEach two: suite two - test', |
| 42 | + |
| 43 | + 'after(): global', |
| 44 | + 'after one: <root>', |
| 45 | + 'after two: <root>', |
| 46 | +].join('\n'); |
| 47 | + |
| 48 | +/** |
| 49 | + * TODO: The `--require` flag is processed in `loadPreloadModules` (process/pre_execution.js) BEFORE |
| 50 | + * the root test is created by the test runner. This causes a global `before` hook to register (and |
| 51 | + * run) but then the root test-case is created, causing the "subsequent" hooks to get lost. This |
| 52 | + * behaviour (CJS route only) is different from the ESM route, where test runner explicitly handles |
| 53 | + * `--import` in `root.runInAsyncScope` (test_runner/runner.js). |
| 54 | + * @see https://github.com/nodejs/node/pull/57595#issuecomment-2770724492 |
| 55 | + * @see https://github.com/nodejs/node/issues/57728 |
| 56 | + * Moved from test/parallel/test-runner-no-isolation-hooks.mjs |
| 57 | + */ |
| 58 | +test('use --require to define global hooks', async (t) => { |
| 59 | + const { stdout } = await common.spawnPromisified(process.execPath, [ |
| 60 | + ...testArguments, |
| 61 | + '--require', fixtures.path('test-runner', 'no-isolation', 'global-hooks.cjs'), |
| 62 | + ...testFiles, |
| 63 | + ]); |
| 64 | + |
| 65 | + const testHookOutput = stdout.split('\n▶')[0]; |
| 66 | + |
| 67 | + t.assert.equal(testHookOutput, order); |
| 68 | +}); |
0 commit comments