GeezerVison is showing github.com — test_runner: allow `--import` with no isolation · nodejs/node@36da793
File treeExpand file treeCollapse file tree Open diff view settings fixtures/test-runner/no-isolation
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/runner.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,6 +34,7 @@ const { spawn } = require('child_process'); | 34 | 34 | const { finished } = require('internal/streams/end-of-stream'); | 35 | 35 | const { resolve } = require('path'); | 36 | 36 | const { DefaultDeserializer, DefaultSerializer } = require('v8'); | | 37 | +const { getOptionValue } = require('internal/options'); | 37 | 38 | const { Interface } = require('internal/readline/interface'); | 38 | 39 | const { deserializeError } = require('internal/error_serdes'); | 39 | 40 | const { Buffer } = require('buffer'); | @@ -697,6 +698,11 @@ function run(options = kEmptyObject) { | 697 | 698 |
| 698 | 699 | root.harness.bootstrapPromise = promise; | 699 | 700 |
| | 701 | + const userImports = getOptionValue('--import'); | | 702 | + for (let i = 0; i < userImports.length; i++) { | | 703 | + await cascadedLoader.import(userImports[i], parentURL, kEmptyObject); | | 704 | + } | | 705 | + | 700 | 706 | for (let i = 0; i < testFiles.length; ++i) { | 701 | 707 | const testFile = testFiles[i]; | 702 | 708 | const fileURL = pathToFileURL(testFile); | |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import('node:test').then((test) => { | | 2 | + test.before(() => console.log('before(): global')); | | 3 | + test.beforeEach(() => console.log('beforeEach(): global')); | | 4 | + test.after(() => console.log('after(): global')); | | 5 | + test.afterEach(() => console.log('afterEach(): global')); | | 6 | +}); |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/test-runner/no-isolation/one.test.js +12-7Lines changed: 12 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,30 +3,35 @@ const { before, beforeEach, after, afterEach, test, suite } = require('node:test | 3 | 3 |
| 4 | 4 | globalThis.GLOBAL_ORDER = []; | 5 | 5 |
| | 6 | +function record(data) { | | 7 | + globalThis.GLOBAL_ORDER.push(data); | | 8 | + console.log(data); | | 9 | +} | | 10 | + | 6 | 11 | before(function() { | 7 | | - GLOBAL_ORDER.push(`before one: ${this.name}`); | | 12 | + record(`before one: ${this.name}`); | 8 | 13 | }); | 9 | 14 |
| 10 | 15 | beforeEach(function() { | 11 | | - GLOBAL_ORDER.push(`beforeEach one: ${this.name}`); | | 16 | + record(`beforeEach one: ${this.name}`); | 12 | 17 | }); | 13 | 18 |
| 14 | 19 | after(function() { | 15 | | - GLOBAL_ORDER.push(`after one: ${this.name}`); | | 20 | + record(`after one: ${this.name}`); | 16 | 21 | }); | 17 | 22 |
| 18 | 23 | afterEach(function() { | 19 | | - GLOBAL_ORDER.push(`afterEach one: ${this.name}`); | | 24 | + record(`afterEach one: ${this.name}`); | 20 | 25 | }); | 21 | 26 |
| 22 | 27 | suite('suite one', function() { | 23 | | - GLOBAL_ORDER.push(this.name); | | 28 | + record(this.name); | 24 | 29 |
| 25 | 30 | test('suite one - test', { only: true }, function() { | 26 | | - GLOBAL_ORDER.push(this.name); | | 31 | + record(this.name); | 27 | 32 | }); | 28 | 33 | }); | 29 | 34 |
| 30 | 35 | test('test one', function() { | 31 | | - GLOBAL_ORDER.push(this.name); | | 36 | + record(this.name); | 32 | 37 | }); |
Collapse file Copy file name to clipboard +12-7Lines changed: 12 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 | const { before, beforeEach, after, afterEach, test, suite } = require('node:test'); | 3 | 3 |
| | 4 | +function record(data) { | | 5 | + globalThis.GLOBAL_ORDER.push(data); | | 6 | + console.log(data); | | 7 | +} | | 8 | + | 4 | 9 | before(function() { | 5 | | - GLOBAL_ORDER.push(`before two: ${this.name}`); | | 10 | + record(`before two: ${this.name}`); | 6 | 11 | }); | 7 | 12 |
| 8 | 13 | beforeEach(function() { | 9 | | - GLOBAL_ORDER.push(`beforeEach two: ${this.name}`); | | 14 | + record(`beforeEach two: ${this.name}`); | 10 | 15 | }); | 11 | 16 |
| 12 | 17 | after(function() { | 13 | | - GLOBAL_ORDER.push(`after two: ${this.name}`); | | 18 | + record(`after two: ${this.name}`); | 14 | 19 | }); | 15 | 20 |
| 16 | 21 | afterEach(function() { | 17 | | - GLOBAL_ORDER.push(`afterEach two: ${this.name}`); | | 22 | + record(`afterEach two: ${this.name}`); | 18 | 23 | }); | 19 | 24 |
| 20 | 25 | suite('suite two', function() { | 21 | | - GLOBAL_ORDER.push(this.name); | | 26 | + record(this.name); | 22 | 27 |
| 23 | 28 | before(function() { | 24 | | - GLOBAL_ORDER.push(`before suite two: ${this.name}`); | | 29 | + record(`before suite two: ${this.name}`); | 25 | 30 | }); | 26 | 31 |
| 27 | 32 | test('suite two - test', { only: true }, function() { | 28 | | - GLOBAL_ORDER.push(this.name); | | 33 | + record(this.name); | 29 | 34 | }); | 30 | 35 | }); |
Collapse file Copy file name to clipboard +76Lines changed: 76 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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-reporter=spec', | | 8 | + '--experimental-test-isolation=none', | | 9 | +]; | | 10 | + | | 11 | +const testFiles = [ | | 12 | + fixtures.path('test-runner', 'no-isolation', 'one.test.js'), | | 13 | + fixtures.path('test-runner', 'no-isolation', 'two.test.js'), | | 14 | +]; | | 15 | + | | 16 | +const order = [ | | 17 | + 'before(): global', | | 18 | + | | 19 | + 'before one: <root>', | | 20 | + 'suite one', | | 21 | + | | 22 | + 'before two: <root>', | | 23 | + 'suite two', | | 24 | + | | 25 | + 'beforeEach(): global', | | 26 | + 'beforeEach one: suite one - test', | | 27 | + 'beforeEach two: suite one - test', | | 28 | + | | 29 | + 'suite one - test', | | 30 | + 'afterEach(): global', | | 31 | + 'afterEach one: suite one - test', | | 32 | + 'afterEach two: suite one - test', | | 33 | + | | 34 | + 'beforeEach(): global', | | 35 | + 'beforeEach one: test one', | | 36 | + 'beforeEach two: test one', | | 37 | + 'test one', | | 38 | + | | 39 | + 'afterEach(): global', | | 40 | + 'afterEach one: test one', | | 41 | + 'afterEach two: test one', | | 42 | + | | 43 | + 'before suite two: suite two', | | 44 | + 'beforeEach(): global', | | 45 | + 'beforeEach one: suite two - test', | | 46 | + 'beforeEach two: suite two - test', | | 47 | + | | 48 | + 'suite two - test', | | 49 | + 'afterEach(): global', | | 50 | + 'afterEach one: suite two - test', | | 51 | + 'afterEach two: suite two - test', | | 52 | + | | 53 | + 'after(): global', | | 54 | + 'after one: <root>', | | 55 | + 'after two: <root>', | | 56 | +]; | | 57 | + | | 58 | +test('Using --require to define global hooks works', async (t) => { | | 59 | + const spawned = await common.spawnPromisified(process.execPath, [ | | 60 | + ...testArguments, | | 61 | + '--require', fixtures.path('test-runner', 'no-isolation', 'global-hooks.js'), | | 62 | + ...testFiles, | | 63 | + ]); | | 64 | + | | 65 | + t.assert.ok(spawned.stdout.includes(order.join('\n'))); | | 66 | +}); | | 67 | + | | 68 | +test('Using --import to define global hooks works', async (t) => { | | 69 | + const spawned = await common.spawnPromisified(process.execPath, [ | | 70 | + ...testArguments, | | 71 | + '--import', fixtures.fileURL('test-runner', 'no-isolation', 'global-hooks.js'), | | 72 | + ...testFiles, | | 73 | + ]); | | 74 | + | | 75 | + t.assert.ok(spawned.stdout.includes(order.join('\n'))); | | 76 | +}); |
|