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

Modules with alias are not mocked #3105

Closed
6 tasks done
BrevAlessio opened this issue Mar 30, 2023 · 2 comments
Closed
6 tasks done

Modules with alias are not mocked #3105

BrevAlessio opened this issue Mar 30, 2023 · 2 comments

Comments

@BrevAlessio
Copy link

BrevAlessio commented Mar 30, 2023

Describe the bug

If I try to mock a module imported by another module, by providing an alias as key, such as vi.mock('@module-alias'), the module is not mocked as expected.
If I provide the relative path as key, such as vi.mock('..src/MyModule'), the module is mocked as expected.

// MyInnerModule
export const MyInnerModule = { key: undefined }
// MyModule
import { MyInnerModule } from '@/MyInnerModule'
export const MyModule = { key: MyInnerModule.key }
// MyModule.test
import { MyModule } from '@/MyModule';
import { vi, describe, it, expect } from 'vitest';

// Try with relative path ../src/MyInnerModule, it works
vi.mock('@/MyInnerModule', () => {
  return {
    MyInnerModule: {
      key: 1,
    },
  };
});

describe('MyModule', () => {
  it('should have key', () => {
    expect.assertions(1);
    expect(MyModule.key).toBe(1); // Got undefined
  });
});

This issue may be related to #2777 but it didn't solve my case.

Reproduction

Stackblitz, follow the instruction on line 4 to switch between non working and working
https://stackblitz.com/edit/vitest-dev-vitest-qj31uc?file=test/MyModule.test.ts

System Info

All the infos are on stackblitz:

  "devDependencies": {
    "@vitest/ui": "latest",
    "vite": "latest",
    "vitest": "0.29.8"
  },

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

Don't use relative paths in aliases: https://vitejs.dev/config/shared-options.html#resolve-alias

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Mar 30, 2023
@BrevAlessio
Copy link
Author

Sorry, my bad, I totally missed that.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 4, 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

No branches or pull requests

2 participants