Skip to content

feat: migrate test suite from Mocha to Vitest #2815

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

aewing
Copy link

@aewing aewing commented Aug 7, 2025

Summary

Completes a comprehensive migration of the entire test suite from Mocha to Vitest, modernizing our testing infrastructure across both the language server and svelte2tsx packages.

Changes Made

Core Testing Infrastructure

  • Replaced Mocha with Vitest: Migrated all test files from Mocha to Vitest
  • Snapshot Testing: Converted JSON fixture files to Vitest's native snapshot system, eliminating individual JSON files

Multi-Version Svelte Support

  • Dual Testing Setup: Maintained compatibility testing for both Svelte 4 and Svelte 5 using Vitest's project configuration. I attempted to set up vitest projects with an alias for svelte, but language-tools ignored it. Needs more research.

Test Modernization

  • Helper Utilities: Created shared test helpers in test-helpers.ts for common setup patterns
  • Async Handling: Improved async test patterns using Vitest's native promises support

Compromises & Technical Decisions

  • Sequential Performance Tests: Some performance-sensitive tests marked as it.sequential() to prevent timing conflicts in CI
  • Snapshot Migration: Moved to Vitest snapshots from individual JSON expectation files for better maintainability
  • Dependency Updates: Removed cross-env and Mocha-related dependencies, added Vitest

Testing & Validation

  • ✅ All tests pass under Vitest with (I believe) identical behavior to Mocha
  • ✅ CI pipeline validates both Svelte 4 and Svelte 5 compatibility
  • ✅ Snapshot tests ensure consistent diagnostic and completion behavior
  • ✅ Performance tests maintain timing accuracy with sequential execution

aewing added 10 commits August 6, 2025 19:21
- Replace Mocha with Vitest v3.2.4 across all test suites
- Convert all assertions from assert.* to expect().* syntax
- Migrate custom JSON snapshot system to Vitest's built-in snapshots
- Remove ~200+ individual JSON files, replace with 3 .snap files
- Add UPDATE_SNAPSHOTS env var support for snapshot updates
- Fix all test imports and syntax for Vitest compatibility
- Update TypeScript diagnostic snapshots for TS 5.8.2
- All 635 tests passing with 100% pass rate

BREAKING CHANGE: Test framework changed from Mocha to Vitest. Run tests with 'pnpm test' or 'vitest' instead of mocha.
- No longer needed after migrating from Mocha to Vitest
- Add environment-based Svelte 5 alias configuration in vitest.config.ts
- Simplify CI workflow for Svelte 5 testing using SVELTE_VERSION env var
- Add svelte5 package alias for cleaner dependency management
- Remove unnecessary workspace configuration files
- Svelte 5.38.0 as default 'svelte' dependency (current version)
- Svelte 4.2.20 as 'svelte4' alias for backwards compatibility
- Vitest workspace configuration runs tests against both versions
- No runtime patching or installation needed
- CI simplified to just run 'pnpm test:workspace'
- Create test-helpers module for centralized Svelte version detection
- Update all tests to use consistent version checking
- Fix HTML plugin test to handle both Svelte versions correctly
- Workspace configuration now properly aliases svelte/svelte4 imports
- Tests correctly detect runtime Svelte version via aliases
- Add dynamic isSvelte5Plus() function to avoid cached module-level values
- Update all tests to use function calls instead of cached constants
- Fix test expectations to handle Svelte 5 being the default
- Unskip previously blocked test to check if upstream issue is fixed
- Note: Vitest aliases only apply to test code, not source code
- Remove .npmrc with deprecated resolution-mode setting
- Replace deprecated vitest workspace file with test.projects configuration
- Fix deprecated Vitest timeout syntax using vi.setConfig()
- Update resolve.alias configuration for proper Svelte version handling
- Eliminate all deprecation warnings from test output
- Maintain dual Svelte 4/5 testing capability
- Remove complex Vitest projects setup that wasn't working with aliases
- Restore CI to use separate jobs for Svelte 4 and Svelte 5 testing
- Clean up unsuccessful dual-version scripts and configurations
- Maintain simple, reliable testing approach
The Position import was missing causing ReferenceError in diagnostic tests
Finalizes the testing framework migration by updating CI workflows to use simplified test commands and ensuring performance tests run sequentially to avoid conflicts.
@aewing aewing marked this pull request as ready for review August 7, 2025 04:44
Removes leftover index-vitest-snapshots.test.ts.snap from earlier migration attempt. The test suite now uses the standard index.test.ts.snap file.
@aewing
Copy link
Author

aewing commented Aug 7, 2025

Continuing the work done in #2711

Most of the changes come from the conversion of snapshots to vitest snapshots.

I tooled around with trying to get svelte 4 and svelte 5 test to run simultaneously using an alias and vitest projects, but language-tools didn't seem to want to play nice with the alias. I can spend more time digging into that if desired.

Happy to clean up/make changes as necessary, just let me know what I can do to help get this across the finish line.

@jasonlyu123
Copy link
Member

I don't think merging every expected JSON snapshot into one giant file is a good idea. It makes it very hard to trace the git diff, difficult to map the file back to its original input, and it'll easily cause git conflicts. I am also not sure about the "Dual Testing Setup" part, at least that the svelte5 install should be in devDependencies. The test timeout customisation shouldn't be removed either.

@aewing aewing changed the title Migrate test suite from Mocha to Vitest chore: migrate test suite from Mocha to Vitest Aug 7, 2025
@aewing aewing changed the title chore: migrate test suite from Mocha to Vitest feat: migrate test suite from Mocha to Vitest Aug 7, 2025
@aewing
Copy link
Author

aewing commented Aug 7, 2025

I don't think merging every expected JSON snapshot into one giant file is a good idea.

Ok, I can organize these into files that (mostly) align with the files we had before using toMatchFileSnapshot.

I am also not sure about the "Dual Testing Setup" part, at least that the svelte5 install should be in devDependencies.

This is an artifact from when I was trying to make both versions test in the same CI and context using vitest projects. I still think this can be made to work, but was having trouble with it. I'll remove the artifacts, my bad.

The test timeout customisation shouldn't be removed either.

Will fix this as well and take another pass on unnecessary changes that slipped through.

aewing added 2 commits August 6, 2025 22:36
…ration

- Switch from inline Vitest snapshots to toMatchFileSnapshot with clean JSON files
- Restore default Svelte version from 5.x back to 4.x in language-server package
- Remove unnecessary vitest setup file that wasn't in master
- Set proper test timeout (25s) in vitest config to match original Mocha behavior
- Clean up one-off comments and maintain sequential performance tests
- Add snapshot files to prettierignore to handle JSON formatting

All tests passing with improved snapshot organization for easier diffing.
…approach

- Replace dynamic timeout adjustment with explicit performance assertions
- Maintain adaptive time limits: fast machines get stricter limits, slow machines get generous limits
- Use expect().toBeLessThan() for immediate clear failure messages instead of timeout errors
- Clean up comments to focus on current implementation rather than comparisons

Performance behavior is preserved while being more compatible with Vitest's testing model.
@aewing
Copy link
Author

aewing commented Aug 7, 2025

The test timeout customisation shouldn't be removed either.

Will fix this as well and take another pass on unnecessary changes that slipped through.

I wasn't able to exactly reproduce the same behavior as I couldn't find a way to dynamically set and get timeout in the test like mocha (if you're aware of one let me know), but I did my best to recreate the same regression proofing with expect. Let me know if you'd like to see another approach here.

The other issues have been addressed, I believe.

Edit: benchmark output is kinda odd in vite right now, I wrote some benchmarks but need to spend more time to understand how to make the output useful. will have to follow up with benchmarks.

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.

2 participants