Skip to content

fix(tegg-vitest): reuse runner module graph for prototypes#6029

Merged
elrrrrrrr merged 5 commits into
eggjs:nextfrom
elrrrrrrr:codex/tegg-vitest-module-importer
Jul 12, 2026
Merged

fix(tegg-vitest): reuse runner module graph for prototypes#6029
elrrrrrrr merged 5 commits into
eggjs:nextfrom
elrrrrrrr:codex/tegg-vitest-module-importer

Conversation

@elrrrrrrr

@elrrrrrrr elrrrrrrr commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • install a default __EGG_MODULE_IMPORTER__ in the TEGG Vitest runner
  • route prototype discovery through the current Vitest module graph
  • verify loader imports reuse the exact class reference already imported by a test

Root cause

TEGG discovers modules during Egg boot. Without a runner-provided importer, application loading can evaluate a prototype through Node while Vitest evaluates the same file in its own module graph. The two class identities then produce duplicate prototype registration in larger applications.

The runner now uses the normal collection import path. Vitest setup imports are intentionally avoided because they invalidate the evaluated-module cache.

Validation

  • tegg/core/vitest: fixture app test passed
  • tegg/core/vitest: hook tests passed when run independently
  • tegg/core/vitest: ut run typecheck
  • tegg/core/loader: 14 loader tests passed
  • published beta artifacts: @eggjs/tegg-loader@4.0.2-beta.21, @eggjs/tegg-vitest@4.0.2-beta.21

Summary by CodeRabbit

  • New Features

    • Added automatic test setup support for loading both ESM and CommonJS modules.
    • Exposed the test setup module for direct configuration in Vitest projects.
    • Automatically includes the setup module when using the Vitest runner.
  • Bug Fixes

    • Improved consistency between dynamically imported modules and statically imported references.
  • Tests

    • Added coverage verifying module imports resolve to the expected instances.

Copilot AI review requested due to automatic review settings July 11, 2026 17:35
@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4dafadc4-de2d-4d70-87ab-ebbb24499b39

📥 Commits

Reviewing files that changed from the base of the PR and between 7cf3265 and 0a4c757.

📒 Files selected for processing (4)
  • tegg/core/vitest/package.json
  • tegg/core/vitest/src/setup.ts
  • tegg/core/vitest/vitest.config.ts
  • tools/egg-bin/src/commands/test.ts

📝 Walkthrough

Walkthrough

Vitest gains a published setup module that defines a global ESM/CommonJS-aware module importer. Vitest configuration and egg-bin wiring load the setup, while a fixture test verifies dynamically imported services match static imports.

Changes

Vitest importer integration

Layer / File(s) Summary
Module importer implementation
tegg/core/vitest/src/setup.ts, tegg/core/vitest/test/fixture_app.test.ts
Adds cached ESM/CommonJS detection and the __EGG_MODULE_IMPORTER__ hook, with a test asserting dynamic and static HelloService references are identical.
Setup export and execution wiring
tegg/core/vitest/package.json, tegg/core/vitest/vitest.config.ts, tools/egg-bin/src/commands/test.ts
Publishes the setup entry, loads it in Vitest, and prepends the resolved setup module when the Tegg runner is available.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant FixtureTest
  participant EggModuleImporter
  participant TargetModule
  FixtureTest->>EggModuleImporter: Call __EGG_MODULE_IMPORTER__(filePath)
  EggModuleImporter->>EggModuleImporter: Detect ESM or CommonJS format
  EggModuleImporter->>TargetModule: import() or require()
  TargetModule-->>FixtureTest: Return HelloService module
  FixtureTest->>FixtureTest: Compare dynamic and static HelloService references
Loading

Possibly related PRs

  • eggjs/egg#5989: Defines and wires the same globalThis.__EGG_MODULE_IMPORTER__ hook into loader behavior.
  • eggjs/egg#5992: Uses the __EGG_MODULE_IMPORTER__ hook from importModule().
  • eggjs/egg#6002: Documents and tests the importer hook contract, including ESM loading.

Suggested reviewers: fengmk2, killagu

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main change: reusing the Vitest module graph to avoid duplicate prototype registration in tegg-vitest.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request bumps package versions to 4.0.2-beta.21 and introduces a global module importer (globalThis.EGG_MODULE_IMPORTER) in TeggVitestRunner to route prototype file imports through Vitest's module graph, ensuring consistent class instances between application loading and test imports. The feedback suggests casting globalThis to any to prevent potential TypeScript compilation errors, and adding an assertion in the test suite to verify the importer is defined before invocation, preventing a potential TypeError.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread tegg/core/vitest/src/runner.ts Outdated
Comment thread tegg/core/vitest/test/fixture_app.test.ts
@codecov

codecov Bot commented Jul 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.17%. Comparing base (2a60d3b) to head (1f65fe4).
⚠️ Report is 1 commits behind head on next.

Additional details and impacted files
@@           Coverage Diff           @@
##             next    #6029   +/-   ##
=======================================
  Coverage   82.17%   82.17%           
=======================================
  Files         678      678           
  Lines       20838    20842    +4     
  Branches     4154     4155    +1     
=======================================
+ Hits        17123    17127    +4     
  Misses       3207     3207           
  Partials      508      508           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the @eggjs/tegg-vitest runner to install a default globalThis.__EGG_MODULE_IMPORTER__ that routes prototype discovery through Vitest’s module graph, ensuring loader-time imports and test-time imports share identical class references (avoiding duplicate prototype registration).

Changes:

  • Install a default __EGG_MODULE_IMPORTER__ in the custom Vitest runner using Vitest’s importFile(..., 'collect').
  • Add a fixture assertion verifying loader-side imports reuse the exact HelloService class reference imported by the test.
  • Bump @eggjs/tegg-vitest and @eggjs/tegg-loader versions to 4.0.2-beta.21.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tegg/core/vitest/test/fixture_app.test.ts Adds a regression assertion to confirm module/class identity is reused via the importer.
tegg/core/vitest/src/runner.ts Installs a default global module importer that reuses Vitest’s module graph during collection.
tegg/core/vitest/package.json Version bump for @eggjs/tegg-vitest to 4.0.2-beta.21.
tegg/core/loader/package.json Version bump for @eggjs/tegg-loader to 4.0.2-beta.21.

Comment thread tegg/core/vitest/test/fixture_app.test.ts
Comment thread tegg/core/vitest/src/runner.ts Outdated
@socket-security

socket-security Bot commented Jul 11, 2026

Copy link
Copy Markdown

Dependency limit exceeded — report not shown.

This pull request scan exceeded the 10,000-dependency limit applied to this scan, so the results are incomplete and may be inaccurate. To avoid reporting false positives, Socket has not posted a report.

Upgrade your plan to raise the dependency limit and get complete reports, or view the partial scan in the dashboard.

Socket is always free for open source. If this is a non-commercial open source project, contact us to request a free Team account.

Copilot AI review requested due to automatic review settings July 11, 2026 17:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread tegg/core/vitest/test/fixture_app.test.ts
Comment thread tegg/core/vitest/src/runner.ts Outdated
Copilot AI review requested due to automatic review settings July 11, 2026 17:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread tegg/core/vitest/src/runner.ts Outdated
Copilot AI review requested due to automatic review settings July 11, 2026 17:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Comment thread tools/egg-bin/src/commands/test.ts
Comment thread tegg/core/vitest/src/setup.ts

@killagu killagu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

Copilot AI review requested due to automatic review settings July 12, 2026 13:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Comment thread tools/egg-bin/src/commands/test.ts
@elrrrrrrr elrrrrrrr enabled auto-merge July 12, 2026 13:25
@elrrrrrrr elrrrrrrr disabled auto-merge July 12, 2026 13:38
@elrrrrrrr elrrrrrrr added this pull request to the merge queue Jul 12, 2026
Merged via the queue into eggjs:next with commit dfd102c Jul 12, 2026
22 checks passed
@elrrrrrrr elrrrrrrr deleted the codex/tegg-vitest-module-importer branch July 12, 2026 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants