Skip to content

fix: support import rstest in node_modules#547

Merged
9aoy merged 4 commits intomainfrom
import-rstest
Sep 5, 2025
Merged

fix: support import rstest in node_modules#547
9aoy merged 4 commits intomainfrom
import-rstest

Conversation

@9aoy
Copy link
Copy Markdown
Collaborator

@9aoy 9aoy commented Sep 4, 2025

Summary

Support use rstest test API in node_modules.

Previously, Rstest only exported type definitions for the test API and did not export the actual implementation. When running tests, it relies on Rspack to bundle and replace @rstest/core with global['@rstest/core']. If the dependencies in node_modules are not bundled, an error will occur.

Related Links

fix: #544

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Copilot AI review requested due to automatic review settings September 4, 2025 13:18
@netlify
Copy link
Copy Markdown

netlify bot commented Sep 4, 2025

Deploy Preview for rstest-dev ready!

Name Link
🔨 Latest commit a4e1552
🔍 Latest deploy log https://app.netlify.com/projects/rstest-dev/deploys/68ba5350ed2c4c0008230c99
😎 Deploy Preview https://deploy-preview-547--rstest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 fixes support for importing rstest from node_modules by implementing a runtime API system that allows the rstest functions to be accessed even when imported as a dependency rather than running directly in the rstest environment.

  • Replaces static type declarations with dynamic API wrappers that check for runtime availability
  • Implements global API registration system to make rstest functions available across module boundaries
  • Adds comprehensive e2e tests to verify the node_modules import functionality

Reviewed Changes

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

Show a summary per file
File Description
packages/core/src/runtime/api/public.ts Replaces static declarations with dynamic API wrappers and runtime checks
packages/core/src/runtime/api/index.ts Registers the runtime API globally for cross-module access
e2e/runner/test/runner.test.ts Adds e2e test for node_modules import scenario
e2e/runner/test/fixtures/test-rstest-import/package.json Test fixture package configuration
e2e/runner/test/fixtures/test-rstest-import/index.js Test fixture demonstrating rstest import usage
e2e/runner/test/fixtures/runner.test.ts Test runner that imports and uses the fixture
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported
Comments suppressed due to low confidence (1)

packages/core/src/runtime/api/public.ts:1

  • The @ts-expect-error comment lacks explanation. Consider using a more specific type assertion or adding a comment explaining why the type error is expected and safe to ignore.
import type { Rstest, RstestUtilities } from '../../types';

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +23 to +27
return globalThis.RSTEST_API![name].call(
globalThis.RSTEST_API![name],
// @ts-expect-error
...args,
);
Copy link

Copilot AI Sep 4, 2025

Choose a reason for hiding this comment

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

The first argument to .call() should be this context, but you're passing the function itself. This should likely be globalThis.RSTEST_API or null for the context, and the function should be called directly with globalThis.RSTEST_API![name]!(...args).

Suggested change
return globalThis.RSTEST_API![name].call(
globalThis.RSTEST_API![name],
// @ts-expect-error
...args,
);
// @ts-expect-error
return globalThis.RSTEST_API![name]!(...args);

Copilot uses AI. Check for mistakes.
@9aoy 9aoy merged commit 94c3159 into main Sep 5, 2025
16 checks passed
@9aoy 9aoy deleted the import-rstest branch September 5, 2025 06:04
@9aoy 9aoy mentioned this pull request Sep 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: The requested module '@rstest/core' does not provide an export named 'beforeAll'

2 participants