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

Path options do not work with vitest vi.mock #99

Closed
haakonjackfloat opened this issue Feb 1, 2023 · 5 comments
Closed

Path options do not work with vitest vi.mock #99

haakonjackfloat opened this issue Feb 1, 2023 · 5 comments

Comments

@haakonjackfloat
Copy link

haakonjackfloat commented Feb 1, 2023

Given a tsconfig.json like so:

{
  "compilerOptions": {
    "baseUrl": "./app",
    "paths": {
      "@c/*": ["../../common/src/*"],
    }
  },
  "include": ["app/**/*"],
}

And a vite.config.ts:

import path from 'path';
import { defineConfig } from 'vitest/config';
import tsconfigPaths from 'vite-tsconfig-paths';

export default defineConfig({
  test: {
    globals: true,
    setupFiles: path.resolve(__dirname, 'testSetup'),
  },
  plugins: [tsconfigPaths() as any],
});

In a vitest spec file:

import { someMethod } from '@c/some/lib';

// This does not work, `someMethod` will not be a spy
vi.mock('@c/some/lib', () => {
  return {
    someMethod: vi.fn(),
  };
});

// This does work
vi.mock('../../common/src/some/lib', () => {
  return {
    someMethod: vi.fn(),
  };
});
@haakonjackfloat haakonjackfloat changed the title Does not work with vitest vi.mock Path options do not work with vitest vi.mock Feb 1, 2023
@tb-viktor
Copy link

Encountering this problem as well.

In our case we don't have any specific paths declared in our tsconfig, but just use the tsconfigPaths() plugin to shorten import paths from the base url. As above, vi.mock does not resolve it properly. When we change the mock path from e.g. folder1/folder2/file to /baseUrl/folder1/folder2/file explicitly it works, but it is not ideal and makes migration from jest somewhat tedious with a large amount of test files.

@DanHulton
Copy link

This may be related... I have vite-tsconfig-paths set up nearly exactly the same as above (js file for vite.config, but otherwise the same), and I have been unable to get dynamic imports to work when running vitest.

import { configurePassport } from '#api/Passport'; works, but const { configurePassport} = import('#api/Passport'); will not, with the error Error: Failed to load url #api/Passport (resolved id: #api/Passport). Does the file exist?

These imports work just fine when running the app via vite-node, it's just in vitest that they no longer resolve.

Potentially the same code path doing the resolving for dynamic imports as trying to resolve mocks? I haven't seen much discussion of vitest resolution issues outside of this specific issue.

@cayter
Copy link

cayter commented Feb 3, 2023

It's a vitest bug and they just reverted the commit in v0.28.4 about an hour ago.

@DanHulton
Copy link

TYVM, @cayter! It turns it these two were related. Updated to vitest 0.28.4 and the issue with dynamic imports is fixed as well!

@haakonjackfloat
Copy link
Author

Indeed, looks fixed! Thanks for the heads up @cayter!

@aleclarson aleclarson added upstream bug Bug caused by a dependency and removed upstream bug Bug caused by a dependency labels Apr 10, 2023
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

No branches or pull requests

5 participants