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

Vitest 0.31 Breaks Importing Default and Namespace In One Line while Mocking Module #3546

Closed
6 tasks done
alexlafroscia opened this issue Jun 8, 2023 · 1 comment · Fixed by #3547
Closed
6 tasks done
Milestone

Comments

@alexlafroscia
Copy link

alexlafroscia commented Jun 8, 2023

Describe the bug

Prior to Vitest 0.31, the following code worked just fine

import defaultExport, * as EntireModule from '../whatever';

vi.mock('../whatever', (importOriginal) => importOriginal());

However, after Vitest 0.31, this breaks; EntireModule is no longer defined.

If you remove the vi.mock call, the module is defined (but of course, you lose your mock behavior).

Separating the import of the default and entire module into two separate lines continues to work in Vitest 0.31 as well, but the syntax of importing both in one line of code is valid syntax that should work.

This bug is expressed in Vitest 0.32 as well, but the issue appeared in our codebase while upgrading from 0.30.1 to 0.31.4

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-qerkpn?file=test%2Fadd.test.ts

The following test code results in a failure (shown in the attached screenshot)

import { expect, test, vi } from 'vitest';
import add, * as AddModule from '../src/add';

vi.mock('../src/add', (importOriginal) => {
  return importOriginal();
});

test('the default export', () => {
  expect(add(1, 2)).toBe(3);
});

test('the namespace export', () => {
  expect(AddModule.default(1, 2)).toBe(3);
});
CleanShot 2023-06-08 at 11 05 10@2x

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    @vitest/ui: latest => 0.32.0 
    vite: latest => 4.3.9 
    vitest: latest => 0.32.0

Used Package Manager

npm

Validations

@stackblitz
Copy link

stackblitz bot commented Jun 8, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sheremet-va sheremet-va added this to the 0.32.1 milestone Jun 9, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jun 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants