X Tutup
The Wayback Machine - https://web.archive.org/web/20220325181331/https://github.com/nodejs/node/pull/42325
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: add initial test module #42325

Closed
wants to merge 2 commits into from
Closed

test: add initial test module #42325

wants to merge 2 commits into from

Conversation

cjihrig
Copy link
Contributor

@cjihrig cjihrig commented Mar 13, 2022

This commit adds a new test module that exposes an API for creating JavaScript tests. As the tests execute, TAP output is written to standard output. This commit only supports executing individual test files, and does not implement command line functionality for a full test runner (that will come in a future PR).

Refs: #40954

@nodejs-github-bot nodejs-github-bot added lib / src needs-ci labels Mar 13, 2022
@cjihrig cjihrig added the semver-major label Mar 13, 2022
lib/internal/test_runner/harness.js Outdated Show resolved Hide resolved
doc/api/test_runner.md Outdated Show resolved Hide resolved
lib/internal/test_runner/tap_stream.js Outdated Show resolved Hide resolved
lib/internal/test_runner/tap_stream.js Outdated Show resolved Hide resolved
lib/internal/test_runner/tap_stream.js Outdated Show resolved Hide resolved
lib/internal/test_runner/test.js Show resolved Hide resolved
test/message/test_runner_no_refs.js Outdated Show resolved Hide resolved
@targos
Copy link
Member

@targos targos commented Mar 14, 2022

Why semver-major?

@aduh95
Copy link
Contributor

@aduh95 aduh95 commented Mar 14, 2022

Why semver-major?

It adds a new core module (test_runner), so it's a potential breaking change.

@targos
Copy link
Member

@targos targos commented Mar 14, 2022

@cjihrig is this ready for a full review or you're looking for general feedback?

@cjihrig
Copy link
Contributor Author

@cjihrig cjihrig commented Mar 14, 2022

@targos it's ready for a full review (I still need to address Antoine's comments from yesterday).

It's semver major because it adds a new core module. It's experimental so that the API can evolve and features can be added/changed. The v18 cutoff date will be coming up soon, so I'd like to be mindful of that as well. The name test_runner does not currently exist on npm. I tried to claim it, but npm would not allow me due to typosquatting rules, so I think we should be good as far as conflicting with anything existing.

doc/api/test_runner.md Outdated Show resolved Hide resolved
@jasnell
Copy link
Member

@jasnell jasnell commented Mar 14, 2022

First off, thank you for working on this! It's looking great so far. Just did a cursory pass through the code and just found a few issues. I want to check it out and play with it a bit before signing off tho.

lib/internal/test_runner/test.js Outdated Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

I love this. Good work.

lib/internal/test_runner/harness.js Show resolved Hide resolved
@bnb
Copy link
Member

@bnb bnb commented Mar 14, 2022

would it be possible to get CJS and ESM examples for every code block in the docs? Would prefer if we don't ship anything new without both at this point.

aduh95 added a commit to aduh95/core-validate-commit that referenced this issue Mar 14, 2022
richardlau pushed a commit to nodejs/core-validate-commit that referenced this issue Mar 15, 2022
doc/api/test_runner.md Outdated Show resolved Hide resolved
@cjihrig
Copy link
Contributor Author

@cjihrig cjihrig commented Mar 15, 2022

would it be possible to get CJS and ESM examples for every code block in the docs?

I can do that for the code blocks that would not be identical between CJS and ESM.

doc/api/test_runner.md Outdated Show resolved Hide resolved
doc/api/test_runner.md Outdated Show resolved Hide resolved
@mcollina
Copy link
Member

@mcollina mcollina commented Mar 16, 2022

As @martinheidegger said in #42325 (comment), exposing this only as node:test is actually quite nice.

doc/api/test_runner.md Outdated Show resolved Hide resolved
@cjihrig cjihrig force-pushed the test-pr branch 2 times, most recently from 20f1c31 to ac2b747 Compare Mar 16, 2022
Copy link
Member

@mcollina mcollina left a comment

lgtm

aduh95
aduh95 approved these changes Mar 21, 2022
@cjihrig cjihrig added commit-queue-rebase commit-queue labels Mar 21, 2022
@nodejs-github-bot nodejs-github-bot added commit-queue-failed and removed commit-queue labels Mar 21, 2022
@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Mar 21, 2022

Commit Queue failed
- Loading data for nodejs/node/pull/42325
✔  Done loading data for nodejs/node/pull/42325
----------------------------------- PR info ------------------------------------
Title      test: add initial test module (#42325)
Author     Colin Ihrig  (@cjihrig)
Branch     cjihrig:test-pr -> nodejs:master
Labels     semver-major, lib / src, needs-ci, commit-queue-rebase
Commits    2
 - test: add initial test module
 - module,repl: support 'node:'-only core modules
Committers 1
 - cjihrig 
PR-URL: https://github.com/nodejs/node/pull/42325
Refs: https://github.com/nodejs/node/issues/40954
Reviewed-By: Matteo Collina 
Reviewed-By: Antoine du Hamel 
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/42325
Refs: https://github.com/nodejs/node/issues/40954
Reviewed-By: Matteo Collina 
Reviewed-By: Antoine du Hamel 
--------------------------------------------------------------------------------
   ℹ  This PR was created on Sun, 13 Mar 2022 19:54:52 GMT
   ✔  Approvals: 2
   ✔  - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/42325#pullrequestreview-916012177
   ✔  - Antoine du Hamel (@aduh95) (TSC): https://github.com/nodejs/node/pull/42325#pullrequestreview-916296613
   ✔  Last GitHub CI successful
   ✖  No Jenkins CI runs detected
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu
https://github.com/nodejs/node/actions/runs/2018508154

@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Mar 21, 2022

@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Mar 21, 2022

@cjihrig cjihrig added commit-queue and removed commit-queue-failed labels Mar 21, 2022
@nodejs-github-bot nodejs-github-bot removed the commit-queue label Mar 21, 2022
@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Mar 21, 2022

Landed in da399a6...3c4ee52

nodejs-github-bot added a commit that referenced this issue Mar 21, 2022
This commit adds a new 'test' module that exposes an API
for creating JavaScript tests. As the tests execute, TAP
output is written to standard output. This commit only supports
executing individual test files, and does not implement
command line functionality for a full test runner.

PR-URL: #42325
Refs: #40954
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
nodejs-github-bot added a commit that referenced this issue Mar 21, 2022
This commit makes it possible to add new core modules that can
only be require()'ed and imported when the 'node:' scheme is
used. The 'test' module is the first such module.

These 'node:'-only modules are not included in the list returned
by module.builtinModules.

PR-URL: #42325
Refs: #40954
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
@cjihrig cjihrig deleted the test-pr branch Mar 21, 2022
if (mod?.canBeRequiredByUsers) return mod.exports;
if (mod?.canBeRequiredByUsers) {
if (!NativeModule.canBeRequiredWithoutScheme(filename)) {
throw new ERR_UNKNOWN_BUILTIN_MODULE(filename);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that instead of throwing an error, it might be better to skip to the user module of the name test?

if (mod?.canBeRequiredByUsers && NativeModule.canBeRequiredWithoutScheme(filename)) {
  return mod.exports;
}

This would allow for require('test') to still be a user module without introducing a new error and maybe solve the problem of incompatibility?

I am also wondering if it wouldn't be faster to add a canBeRequiredWithoutScheme property in the module constructor

this.canBeRequiredByUsers = !StringPrototypeStartsWith(id, 'internal/');
this.canBeRequiredByUsersWithoutScheme = this.canBeRequiredByUsers && NativeModule.canBeRequiredWithoutScheme(id)

then it could be used like:

if (mod?.canBeRequiredByUsersWithoutScheme) return mod.exports

Copy link
Contributor Author

@cjihrig cjihrig Mar 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that instead of throwing an error, it might be better to skip to the user module of the name test?

You're right. I'll send a fix for that.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future reference: #42430

@ljharb
Copy link
Member

@ljharb ljharb commented Mar 22, 2022

Unfortunate this never cc’d @nodejs/modules. Why is this only available under the node: prefix?

const testResources = new SafeMap();
const hook = createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (resource instanceof Test) {
Copy link
Member

@ljharb ljharb Mar 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Test’s Symbol.hasInstance locked down? If not, this check isn’t robust.

@martinheidegger

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commit-queue-rebase lib / src needs-ci semver-major
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

X Tutup