Skip to content
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

feat: implement module mocking in browser mode #5765

Merged
merged 33 commits into from
May 31, 2024

Conversation

sheremet-va
Copy link
Member

@sheremet-va sheremet-va commented May 23, 2024

Description

This PR implements vi.mock API running in the browser.

Fixes #3046
Fixes #4097
Fixes #3138

The only limitation is we don't support automocking modules that have export * inside because we cannot statically analyze all exported names without transforming imported module.

// automocks a module
vi.mock(import('./some-module.js'))

// aliases to ./src/__mocks__/some-module.js
vi.mock(import('./src/some-module.js'))

// exposes exports through a factory
vi.mock(import('./src/some-module.js'), () => {
  return { namedExport: true }
})

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@sheremet-va sheremet-va force-pushed the feat/mocking-browser branch 2 times, most recently from 7a79fa0 to d9786b0 Compare May 28, 2024 09:19
@sheremet-va sheremet-va changed the title feat: implement module mocking in the browser feat: implement module mocking in browser mode May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant