Skip to content

feat(vite): add Vite build support for on-device Vitest runner - #87

Open
codeprefect wants to merge 1 commit into
NativeScript:mainfrom
codeprefect:feature/vite-build-support
Open

codeprefect wants to merge 1 commit into
NativeScript:mainfrom
codeprefect:feature/vite-build-support

Conversation

@codeprefect

Copy link
Copy Markdown

Disclaimer: This is AI-assisted, but manually tested by a human

Overview

This PR adds Vite build support to @nativescript/unit-test-runner alongside the existing Webpack build pipeline, enabling developers using @nativescript/vite to execute on-device Vitest suites.

It introduces automatic Vite integration via @nativescript/unit-test-runner/vite, creates standard entrypoint redirection and shims for test builds, provides a dedicated createViteTestRegistry helper for import.meta.glob lazy imports, and adds full Istanbul code coverage integration for Vite.


Motivation

With the introduction of @nativescript/vite, developers migrating NativeScript projects away from Webpack needed first-class support for ns test. Vite resolves entrypoints, glob imports, and module dependencies differently from Webpack:

  • require.context is specific to Webpack; Vite uses import.meta.glob.
  • Test bundles in Vite need redirection to test.ts only when building for testing (ns test), while excluding test specs from regular application production/development builds.
  • Dynamic imports in Vite return Promises that need to be awaited during on-device module evaluation.

Key Changes

1. Vite Plugin Integration (nativescript.vite.mjs & src/vite.ts)

  • Plugin Auto-discovery: Added nativescript.vite.mjs at the repository root, which is automatically picked up and loaded by @nativescript/vite via externalConfigsPlugin.
  • Options & Typing: Exported unitTestRunner(options) from @nativescript/unit-test-runner/vite (src/vite.ts) with type definitions for:
    • include: Glob patterns defining test specs.
    • entryPath: Path to the test entry file (defaults to src/test.ts or app/test.ts).
    • coverage: Istanbul coverage instrumentation toggle.
  • Entrypoint Redirection: When tests are enabled, the entrypoint is automatically resolved to test.ts across standard project structures (src/test.ts, app/test.ts, etc.).
  • Production Spec Filtering: Automatically excludes unit test files matching include from non-test production/development builds.
  • Istanbul Code Coverage: Adds @rollup/plugin-istanbul integration during test builds when coverage is enabled.
  • Vitest Shims: Configures Vite aliases pointing on-device Vitest calls to @nativescript/unit-test-runner/shims/vitest.mjs.

2. Runtime Test Module Registry (src/runtime/registry.ts)

  • Added createViteTestRegistry: Accepts the module loader map produced by import.meta.glob('./**/*.test.{js,ts}').
  • Async Promise Handling: Updated executeWrappedModule to unwrap Promise instances returned by Vite's lazy dynamic module loaders (() => import(...)).
  • Ambient Types: Added declare global { interface ImportMeta { glob(...) } } so TypeScript projects do not error if vite/client references are not globally present.
  • Backward Compatibility:
    • createNativeScriptTestRegistry is preserved as an alias for createViteTestRegistry.
    • Added guards so createWebpackTestRegistry and createViteTestRegistry can gracefully handle contexts or module maps passed to either function.

3. Exports & Package Setup (package.json, src/runtime/index.ts)

  • Added "./vite" export map pointing to dist/src/vite.js and src/vite.ts.
  • Exported createViteTestRegistry from @nativescript/unit-test-runner/runtime.

4. Tests & Documentation

  • Plugin Unit Tests (src/node/vite-plugin.spec.ts): Added comprehensive test suite verifying:
    • Entrypoint rewriting for test builds vs. default app entry preservation for normal builds.
    • Test file exclusion from non-test builds.
    • Istanbul coverage plugin enablement.
    • Vitest alias configuration.
  • Registry Unit Tests (src/runtime/registry.spec.ts): Added tests covering lazy loaders, Promise evaluation, path normalization, and error handling for missing modules.
  • Documentation (README.md): Updated the setup guide with:
    • Vite test.ts setup using createViteTestRegistry(import.meta.glob(...)).
    • NativeScript-Vue specific requirements (calling setRootApp(app) before mounting components).
    • Explicit instructions contrasting Vite vs. Webpack registry setups.

How to Test

1. Automated Tests

npm run test
npm run build

Verify that all 85 unit tests pass and compilation succeeds.

2. Manual Verification in a Vite Project

  1. In a NativeScript + Vite project, set up src/test.ts:
    import { Application } from '@nativescript/core';
    import {
      createViteTestRegistry,
      NativeScriptVitestCoordinator,
    } from '@nativescript/unit-test-runner/runtime';
    import { createVitestHostPage } from '@nativescript/unit-test-runner/testing';
    import '@valor/nativescript-websockets';
    
    const coordinator = new NativeScriptVitestCoordinator({
      registry: createViteTestRegistry(
        import.meta.glob('./**/*.test.{js,ts}'),
      ),
    });
    
    void coordinator.start();
    Application.run({ create: () => createVitestHostPage(coordinator) });
  2. Run on device / simulator:
    ns test ios
    # or
    ns test android
  3. Ensure specs are discovered, loaded on device, executed, and results are reported back to the CLI.

@coderabbitai

coderabbitai Bot commented Oct 6, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d388d436-aea5-4eb0-b82d-99b95d9808f8

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

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.

1 participant