GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test_runner: refactor `mock_loader` by aduh95 · Pull Request #54223 · nodejs/node

Conversation

@aduh95

@aduh95 aduh95 commented Aug 6, 2024

Copy link
Copy Markdown
Contributor

It seems the mock_loader can be greatly simplified if it doesn't try to support things Node.js doesn't support (e.g. importing a path, we only support importing URLs), and do not need to rely too much on internals.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bricss reacted with rocket emoji
All reactions
  • 🚀 1 reaction
@nodejs-github-bot

nodejs-github-bot commented Aug 6, 2024

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/test_runner
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test Issues and PRs related to Node.js core tests and test infrastructure. test_runner Issues and PRs related to the test runner subsystem. labels Aug 6, 2024
@codecov

codecov Bot commented Aug 6, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.31%. Comparing base (68e94c1) to head (ac0933b).
Report is 336 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #54223   +/-   ##
=======================================
  Coverage   87.31%   87.31%           
=======================================
  Files         648      648           
  Lines      182362   182336   -26     
  Branches    34986    34980    -6     
=======================================
- Hits       159229   159212   -17     
+ Misses      16398    16392    -6     
+ Partials     6735     6732    -3     
Files with missing lines Coverage Δ
lib/internal/test_runner/mock/loader.js 95.52% <100.00%> (-0.62%) ⬇️
lib/internal/test_runner/mock/mock.js 99.25% <100.00%> (+<0.01%) ⬆️

... and 24 files with indirect coverage changes

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

anonrig
anonrig approved these changes Aug 6, 2024
View reviewed changes
@cjihrig

cjihrig commented Aug 6, 2024

Copy link
Copy Markdown
Contributor

importing a path, we only support importing URLs

Can you clarify this? From what I can tell, dynamic import does work with paths.

> await import('./foo.mjs')
[Module: null prototype] { foo: { foo: 1 } }
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aduh95

aduh95 commented Aug 6, 2024

Copy link
Copy Markdown
Contributor Author

Can you clarify this? From what I can tell, dynamic import does work with paths.

In the example you give here (import("./foo.mjs")), you're passing a relative URL, not a path. As long as you are not on Windows and the path you're using does not contain any special char (such as %, ?, #, etc.), a relative path can serves as a relative URL, hence the confusion I suppose – also, an absolute Unix path can serve as a origin-relative URL as long as no special char are in use.

/// Assuming path of the current module is /tmp/entry.mjs

await import('./foo?test.js'); // Would attempt to load /tmp/foo with import.meta.url being file:///tmp/foo?test.js
await import('./foo%3Ftest.js'); // Would attempt to load /tmp/foo?test.js with import.meta.url being file:///tmp/foo%3Ftest.js

await import('/path/to/file#1.js'); // Would try to load /path/to/file with import.meta.url being file:///path/to/file#1.js
await import('/path/to/file%231.js'); // Would try to load /path/to/file#1.js with import.meta.url being file:///path/to/file%231.js
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cjihrig

cjihrig commented Aug 6, 2024

Copy link
Copy Markdown
Contributor

Another question: if this is only an internal refactor/simplification, I would expect the existing tests to continue working. Why are all of the changes to the test file required?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aduh95

aduh95 commented Aug 6, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Another question: if this is only an internal refactor/simplification, I would expect the existing tests to continue working. Why are all of the changes to the test file required?

That's fair, let's move the test refactor to a dedicated PR (#54233)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cjihrig
cjihrig approved these changes Aug 6, 2024
View reviewed changes

@cjihrig cjihrig left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM if the CI passes.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@aduh95
aduh95 force-pushed the refactor-mock_loader branch from d8ed05d to e5acdee Compare August 6, 2024 22:00
@aduh95 aduh95 added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. labels Aug 6, 2024
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Aug 6, 2024
@nodejs-github-bot

nodejs-github-bot commented Aug 6, 2024

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/60915/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

atlowChemi
atlowChemi approved these changes Aug 7, 2024
View reviewed changes
@aduh95 aduh95 removed the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Aug 7, 2024
@aduh95

aduh95 commented Aug 7, 2024

Copy link
Copy Markdown
Contributor Author

So it breaks on Windows – but IMO that's because the tests are wrong, importing a Windows path is not supposed to work.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cjihrig

cjihrig commented Aug 7, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

importing a Windows path is not supposed to work

I ran into a similar issue when originally developing the feature. The tests only broke on Windows when I tried to implement things more cleanly. I assumed that importing a Windows path was supposed to work because in the tests, code like this works without any mocks configured:

const fixture = fixtures.path('module-mocking', 'basic-esm.mjs');
const original = await import(fixture);

Maybe it's possible that the code only loads because the mock loader is enabled, even with no mocks configured. If the test is wrong and the code doesn't load without the mock loader, then we should drop the invalid test(s) or even add a test to make sure it never works with the mock loader.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aduh95

aduh95 commented Aug 7, 2024

Copy link
Copy Markdown
Contributor Author

I assumed that importing a Windows path was supposed to work because in the tests, code like this works without any mocks configured:

const fixture = fixtures.path('module-mocking', 'basic-esm.mjs');
const original = await import(fixture);

Are you sure? It seems like somthing that shouldn't work. IIRC you should get Unknown protocol c: or something like that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cjihrig

cjihrig commented Aug 7, 2024

Copy link
Copy Markdown
Contributor

I edited my comment, but what might be happening is that the mock loader is enabled and even though no modules are mocked, the resolve() function is allowing those Windows paths to be loaded. I don't have access to Windows to verify, but if you're confident that it shouldn't work, I'm OK with updating those tests.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aduh95

aduh95 commented Aug 7, 2024

Copy link
Copy Markdown
Contributor Author

Let me validate that once I get a hand on a Windows machine.

cjihrig reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aduh95

aduh95 commented Aug 7, 2024

Copy link
Copy Markdown
Contributor Author

I tried reproducing on 22.6.0 on Windows. Both node --experimental-test-module-mocks -e "import(path.resolve('file.mjs'))" and node -e "import(path.resolve('file.mjs'))" report the error I was mentioning earlier (Only URLs with a scheme file, data, and node are supported […]. Received protocol 'c:'). I tried adding the test runner to the mix, but that didn't change anything. Mocking the path would then make the import succeeds, but that's because the mock loader interferes with the usual resolve algorithm.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aduh95

aduh95 commented Aug 7, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

I suggest we land #54233, then I can add a Windows-only test case in this PR that validates passing a path to import results in a rejected promise.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cjihrig

cjihrig commented Aug 7, 2024

Copy link
Copy Markdown
Contributor

SGTM. Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot

nodejs-github-bot commented Aug 9, 2024

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/60985/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

MoLow
MoLow approved these changes Aug 9, 2024
View reviewed changes
jasnell
jasnell approved these changes Aug 11, 2024
View reviewed changes
@cjihrig

cjihrig commented Aug 16, 2024

Copy link
Copy Markdown
Contributor

@aduh95 I think this is ready for the change you proposed in #54223 (comment).

MoLow reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aduh95
aduh95 force-pushed the refactor-mock_loader branch from e5acdee to db0e8e2 Compare August 16, 2024 21:15
@aduh95

aduh95 commented Aug 16, 2024

Copy link
Copy Markdown
Contributor Author

It looks like it's not possible to support t.mock.module(somePath) with my changes (at least on Windows). IMO we should make it support only URLs, that way it's consistent on all OSes, and consistent with import() – and let's be real, for most cases, URLs and paths are indistinguishable from each other. I'll open a separate PR for that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aduh95 aduh95 mentioned this pull request Aug 16, 2024
test_runner: make mock.module's specifier consistent with import() #54416
Merged
aduh95
aduh95 commented Aug 16, 2024
View reviewed changes
Comment thread test/parallel/test-runner-module-mocking.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

aduh95 added 2 commits August 20, 2024 19:41
@aduh95
test_runner: refactor mock_loader
e444e5b
@aduh95
squash! add Windows-only test
ac0933b
@aduh95
aduh95 force-pushed the refactor-mock_loader branch from 64babf7 to ac0933b Compare August 20, 2024 17:47
@aduh95 aduh95 added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. labels Aug 20, 2024
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Aug 20, 2024
@nodejs-github-bot

nodejs-github-bot commented Aug 20, 2024

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/61294/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cjihrig
cjihrig approved these changes Aug 21, 2024
View reviewed changes

@cjihrig cjihrig left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Still LGTM since I guess the commit queue needs a reapproval.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@aduh95 aduh95 added the commit-queue PRs queued for automated landing through the Commit Queue. label Aug 21, 2024
@nodejs-github-bot nodejs-github-bot added commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. and removed commit-queue PRs queued for automated landing through the Commit Queue. labels Aug 21, 2024
@nodejs-github-bot

nodejs-github-bot commented Aug 21, 2024

Copy link
Copy Markdown
Collaborator
Commit Queue failed
- Loading data for nodejs/node/pull/54223
✔  Done loading data for nodejs/node/pull/54223
----------------------------------- PR info ------------------------------------
Title      test_runner: refactor `mock_loader` (#54223)
   ⚠  Could not retrieve the email or name of the PR author's from user's GitHub profile!
Branch     aduh95:refactor-mock_loader -> nodejs:main
Labels     test, author ready, needs-ci, test_runner
Commits    2
 - test_runner: refactor `mock_loader`
 - squash! add Windows-only test
Committers 1
 - Antoine du Hamel <[email protected]>
PR-URL: https://github.com/nodejs/node/pull/54223
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: James M Snell <[email protected]>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/54223
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: James M Snell <[email protected]>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Tue, 06 Aug 2024 09:37:08 GMT
   ✔  Approvals: 5
   ✔  - Yagiz Nizipli (@anonrig) (TSC): https://github.com/nodejs/node/pull/54223#pullrequestreview-2221307097
   ✔  - Colin Ihrig (@cjihrig): https://github.com/nodejs/node/pull/54223#pullrequestreview-2250956747
   ✔  - Chemi Atlow (@atlowChemi): https://github.com/nodejs/node/pull/54223#pullrequestreview-2222805845
   ✔  - Moshe Atlow (@MoLow) (TSC): https://github.com/nodejs/node/pull/54223#pullrequestreview-2230052660
   ✔  - James M Snell (@jasnell) (TSC): https://github.com/nodejs/node/pull/54223#pullrequestreview-2231864401
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2024-08-20T17:54:28Z: https://ci.nodejs.org/job/node-test-pull-request/61294/
- Querying data for job/node-test-pull-request/61294/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  No git cherry-pick in progress
   ✔  No git am in progress
   ✔  No git rebase in progress
--------------------------------------------------------------------------------
- Bringing origin/main up to date...
From https://github.com/nodejs/node
 * branch                  main       -> FETCH_HEAD
✔  origin/main is now up-to-date
- Downloading patch for 54223
From https://github.com/nodejs/node
 * branch                  refs/pull/54223/merge -> FETCH_HEAD
✔  Fetched commits as cc26951180e6..ac0933b2ab3b
--------------------------------------------------------------------------------
[main f1e95b31b5] test_runner: refactor `mock_loader`
 Author: Antoine du Hamel <[email protected]>
 Date: Sat Aug 3 14:30:47 2024 +0200
 2 files changed, 16 insertions(+), 42 deletions(-)
[main d4e46cdcd7] squash! add Windows-only test
 Author: Antoine du Hamel <[email protected]>
 Date: Fri Aug 16 23:57:03 2024 +0200
 1 file changed, 6 insertions(+)
   ✔  Patches applied
Please run the following commands to complete landing

$ git rebase origin/main --no-keep-empty -i -x "git node land --amend" --autosquash
$ git node land --continue

https://github.com/nodejs/node/actions/runs/10492400336

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aduh95 aduh95 added commit-queue PRs queued for automated landing through the Commit Queue. commit-queue-squash PRs the Commit Queue should land as one squashed commit. and removed commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. labels Aug 21, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Aug 21, 2024
@nodejs-github-bot
nodejs-github-bot merged commit b4344cf into nodejs:main Aug 21, 2024
@nodejs-github-bot

nodejs-github-bot commented Aug 21, 2024

Copy link
Copy Markdown
Collaborator

Landed in b4344cf

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aduh95
aduh95 deleted the refactor-mock_loader branch August 21, 2024 15:46
RafaelGSS pushed a commit that referenced this pull request Aug 25, 2024
@aduh95 @RafaelGSS
test_runner: refactor mock_loader
eb0fe19
PR-URL: #54223
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: James M Snell <[email protected]>
@RafaelGSS RafaelGSS mentioned this pull request Aug 25, 2024
v22.8.0 proposal #54560
Merged
RafaelGSS pushed a commit that referenced this pull request Aug 30, 2024
@aduh95 @RafaelGSS
test_runner: refactor mock_loader
0cf78aa
PR-URL: #54223
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: James M Snell <[email protected]>
@targos targos added the dont-land-on-v20.x label Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jasnell jasnell jasnell approved these changes
@anonrig anonrig anonrig approved these changes
@cjihrig cjihrig cjihrig approved these changes
@MoLow MoLow MoLow approved these changes
@atlowChemi atlowChemi atlowChemi approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue-squash PRs the Commit Queue should land as one squashed commit. needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. test Issues and PRs related to Node.js core tests and test infrastructure.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@aduh95 @nodejs-github-bot @cjihrig @jasnell @anonrig @MoLow @atlowChemi @targos
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.