Cannot Alter values of Vi.Mock for different test cases #5676
Unanswered
shreyan1993
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Describe the bug
Hi Guys,
i am trying to use vi mock for the below code, but i am unable to alter the value of isProduction . It is always defaulting to the first case and i cannot mockresolvevalue as its not really a function.
`import { ThemeProvider } from '@mui/material/styles';
import { render, screen } from '@testing-library/react';
import React from 'react';
import { IEnvironment } from 'src/interfaces';
import { lang } from 'src/utils';
import { theme } from 'src/utils/theme';
import { vi } from 'vitest';
import { About } from './About';
describe('About Component', () => {
beforeEach(() => {
vi.mock('src/hooks', () => ({
useGetProjectConfig: () => ({
data: {
gcf: {
apps: {
player: {
version: '1.0.0',
},
cod: {
version: '2.0.0',
},
components: {
version: '3.0.0',
},
},
},
},
}),
}));
});
afterEach(() => {
vi.clearAllMocks();
vi.resetModules();
});
it('renders about component with correct versions when isProduction is true', () => {
vi.mock('src/utils/environment', () => ({
isProduction: true,
SB_API_URL: '',
CCBE_API_URL: '',
}));
});
it('renders about component with correct versions when isProduction is false', () => {
vi.resetModules();
vi.mock('src/utils/environment', () => ({
isProduction: false,
SB_API_URL: '',
CCBE_API_URL: '',
}));
});
});
`
Reproduction
`import { ThemeProvider } from '@mui/material/styles';
import { render, screen } from '@testing-library/react';
import React from 'react';
import { IEnvironment } from 'src/interfaces';
import { lang } from 'src/utils';
import { theme } from 'src/utils/theme';
import { vi } from 'vitest';
import { About } from './About';
describe('About Component', () => {
beforeEach(() => {
vi.mock('src/hooks', () => ({
useGetProjectConfig: () => ({
data: {
gcf: {
apps: {
player: {
version: '1.0.0',
},
cod: {
version: '2.0.0',
},
components: {
version: '3.0.0',
},
},
},
},
}),
}));
});
afterEach(() => {
vi.clearAllMocks();
vi.resetModules();
});
it('renders about component with correct versions when isProduction is true', () => {
vi.mock('src/utils/environment', () => ({
isProduction: true,
SB_API_URL: '',
CCBE_API_URL: '',
}));
});
it('renders about component with correct versions when isProduction is false', () => {
vi.resetModules();
vi.mock('src/utils/environment', () => ({
isProduction: false,
SB_API_URL: '',
CCBE_API_URL: '',
}));
});
});
`
System Info
Used Package Manager
yarn
Validations
Beta Was this translation helpful? Give feedback.
All reactions