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

Mocking module react-router-dom returns undefined #2555

Closed
6 tasks done
yukinami opened this issue Dec 23, 2022 · 0 comments · Fixed by #2559
Closed
6 tasks done

Mocking module react-router-dom returns undefined #2555

yukinami opened this issue Dec 23, 2022 · 0 comments · Fixed by #2559
Assignees

Comments

@yukinami
Copy link

yukinami commented Dec 23, 2022

Describe the bug

Mocking module react-router-dom returns undefined after I set environment to jsdom.
If the environment is set to node. Everything works fine. And this only happens to react-router-dom, mocking other module also works fine.

Reproduction

import { renderHook } from '@testing-library/react';
import reactRouter from 'react-router-dom';
import { describe, expect, it, vi } from 'vitest';

import { useRouterAuthorize } from '../use-router-authorize';

vi.mock('react-router-dom');
describe('useRouterAuthorize', () => {
  it('should', async () => {
    const useMatches = vi.mocked(reactRouter.useMatches);  //reactRouter is undefined 
    useMatches.mockReturnValue([
      {
        id: '',
        pathname: '',
        params: {},
        data: null,
        handle: {
          access: ['admin'],
        },
      },
    ]);

    const { result } = renderHook(() => useRouterAuthorize(['admin']));
    expect(result.current).toBeTruthy();
  });
});

System Info

System:
    OS: macOS 12.0
    CPU: (8) arm64 Apple M1 Pro
    Memory: 91.52 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 18.9.1 - ~/.nvm/versions/node/v18.9.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.9.1/bin/yarn
    npm: 8.19.1 - ~/.nvm/versions/node/v18.9.1/bin/npm
  Browsers:
    Chrome: 108.0.5359.124
    Safari: 15.0
  npmPackages:
    @vitejs/plugin-react: ^2.1.0 => 2.2.0 
    vite: ^3.1.0 => 3.2.3 
    vitest: ^0.26.0 => 0.26.0

Used Package Manager

npm

Validations

@yukinami yukinami changed the title Mock module react-router-dom return undefined Mocking module react-router-dom returns undefined Dec 23, 2022
@sheremet-va sheremet-va self-assigned this Dec 23, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 8, 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