Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: enable in-source testing #46638

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/internal/modules/esm/initialize_import_meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,31 @@
const { getOptionValue } = require('internal/options');
const experimentalImportMetaResolve =
getOptionValue('--experimental-import-meta-resolve');
const isTestRunner = getOptionValue('--test-in-source');
const {
PromisePrototypeThen,
PromiseReject,
ObjectAssign,
} = primordials;
const asyncESM = require('internal/process/esm_loader');

let testModule;
let runnerModule;

function lazyLoadTestModule() {
if (testModule === undefined) {
testModule = require('internal/test_runner/harness');
}
return testModule;
}

function lazyLoadRunnerModule() {
if (runnerModule === undefined) {
runnerModule = require('internal/test_runner/runner');
}
return runnerModule;
}

function createImportMetaResolve(defaultParentUrl) {
return async function resolve(specifier, parentUrl = defaultParentUrl) {
return PromisePrototypeThen(
Expand All @@ -34,6 +53,21 @@ function initializeImportMeta(meta, context) {
}

meta.url = url;
if (isTestRunner) {
const { test, describe, it, before, after, beforeEach, afterEach } = lazyLoadTestModule();
const { run } = lazyLoadRunnerModule();
meta.test = test;
ObjectAssign(meta.test, {
after,
afterEach,
before,
beforeEach,
describe,
it,
run,
test,
});
}
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const { kCancelledByParent, Test, ItTest, Suite } = require('internal/test_runne
const { setupTestReporters } = require('internal/test_runner/utils');
const { bigint: hrtime } = process.hrtime;

const isTestRunnerCli = getOptionValue('--test');
const isTestRunnerCli = getOptionValue('--test') || getOptionValue('--test-in-source');
const testResources = new SafeMap();
const wasRootSetup = new SafeWeakSet();

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const kTestTimeoutFailure = 'testTimeoutFailure';
const kHookFailure = 'hookFailed';
const kDefaultTimeout = null;
const noop = FunctionPrototype;
const isTestRunner = getOptionValue('--test');
const isTestRunner = getOptionValue('--test') || getOptionValue('--test-in-source');
const testOnlyFlag = !isTestRunner && getOptionValue('--test-only');
const testNamePatternFlag = isTestRunner ? null :
getOptionValue('--test-name-pattern');
Expand Down
3 changes: 3 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,9 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
AddOption("--test",
"launch test runner on startup",
&EnvironmentOptions::test_runner);
AddOption("--test-in-source",
"run tests within source file",
&EnvironmentOptions::test_runner_in_source);
AddOption("--experimental-test-coverage",
"enable code coverage in the test runner",
&EnvironmentOptions::test_runner_coverage);
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class EnvironmentOptions : public Options {
std::string diagnostic_dir;
bool test_runner = false;
bool test_runner_coverage = false;
bool test_runner_in_source = false;
std::vector<std::string> test_name_pattern;
std::vector<std::string> test_reporter;
std::vector<std::string> test_reporter_destination;
Expand Down