-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
v1.1.2 Causes "Expression expected" errors #4872
Comments
Looks like this is a regression from #4664. The code that mocker produces is invalid (mock factory is moved too soon?): const { vi } = await import('vitest')
vi.mock("somemodule", async () => {
return {
getServerSession: vi.fn().mockImplementation(() => ({
user
}))
};
});
const __vi_import_0__ = await import('./user')
: __vi_import_0__.default cc @Dunqing |
I also have similar problems in Lerna-Lite but with different output, it was working fine with v1.1.1 FAIL packages/filter-packages/src/__tests__/get-filtered-packages.spec.ts [ packages/filter-packages/src/__tests__/get-filtered-packages.spec.ts ]
Error: [vitest] Cannot mock "npmlog" because it is already loaded. Did you import it in a setup file?
Please, remove the import if you want static imports to be mocked, or clear module cache by calling "vi.resetModules()" before mocking if you are going to import the file again. See: https://vitest.dev/guide/common-errors.html#cannot-mock-mocked-file.js-because-it-is-already-loaded
❯ packages/filter-packages/src/__tests__/get-filtered-packages.spec.ts:21:25 and I have this mock on top of my test (see real test here) import { beforeAll, expect, test, vi } from 'vitest';
const { mockNotice } = vi.hoisted(() => ({ mockNotice: vi.fn() }));
vi.mock('npmlog', async () => ({
...(await vi.importActual<any>('npmlog')),
notice: mockNotice,
})); For reference, the failing GitHub Action job and the unit test |
It doesn't matter where you put it, it is always hoisted. This is not a bug, you import |
I'm not sure if it's the same issue, but I'm also seeing errors related to mocking. I use this snippet to mock React 18 features when using React 17. But apparently, this import { useMemo, version } from "react";
if (version.startsWith("17")) {
vi.mock("react", async () => {
const actual = await vi.importActual<typeof import("react")>("react");
let id = 0;
const mocks = {
startTransition: (v: () => any) => v(),
useDeferredValue: <T>(v: T) => v,
useTransition: () => [false, (v: () => any) => v()],
useId: () => useMemo(() => `id-${id++}`, []),
};
return { ...mocks, ...actual };
});
}
Edit: Changing it to |
Yes, sorry about that. The import was modified before we moved the |
We might need to improve the check to target this only for cases when this would fail but it's a bit expensive to do for each import. |
Should it be fixed in v1.2.0? Because we are still getting this issue with v1.2.0: // <project-root>/vitest.setup.ts
import { mocks } from './src/testing';
console.log(mocks.someMock);
// <project-root>/src/testing.ts
import { vi } from 'vitest';
export const mocks = vi.hoisted(() => ({
someMock: vi.fn(),
})); When running the tests:
|
Why do you use |
We have a set of mocks in a separate file (in my example the We wanted to migrate from vitest 0.34 where this structure worked to v1.2.0 but we are getting this error now. |
Yes, but why are they wrapped in |
We are also using it in the file itself. I'll expand the example a bit to show how we have it structured: In the setup file we are setting the mocks: // <project-root>/vitest.setup.ts
import { vi } from 'vitest';
import { mocks } from './src/testing';
vi.mock('@/someLib', () => ({
someFunc: mocks.someFunc,
})); In the separate file we define the mocks and also have some helper functions for our tests in which we also use the mocks: // <project-root>/src/testing.ts
import { vi } from 'vitest';
export const mocks = vi.hoisted(() => ({
someFunc: vi.fn(),
}));
export const setSomeFuncReturnValue = (a: string) => {
mocks.someFunc.mockReturnValue({
a,
b: 'Some Default Value',
});
}; Here is how a test looks like: // example test
describe('some test', () => {
it('tests', () => {
setSomeFuncReturnValue('Some other Value');
// expectations...
})
}) |
You are just showing how you use it. I am asking why you are using In the first example you should even get a syntax error because |
We are not really using it due to any specific reason, other than finding code examples in the documentation and using them, I think.. In vitest 0.34 this worked in general, no syntax error too. But what I understand from you is that this is not expected to work anyway and we should restructure our approach and not use |
Yes, you can just remove |
Getting the same error for v1.2.1:
|
Describe the bug
After upgrading from v1.1.1 to v1.1.2 all my tests are failing with:
It seems to happen if you have:
mockImplementation
methodReally weird...
Reproduction
Was pretty easy to create a repro:
https://stackblitz.com/edit/vitest-dev-vitest-3w2rlj?file=setupFile.ts
System Info
Used Package Manager
pnpm
Validations
The text was updated successfully, but these errors were encountered: