Skip to content

Commit

Permalink
Try to use ESM mocks for jest test
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed Oct 5, 2023
1 parent 619f838 commit a7bff76
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions code/builders/builder-vite/src/vite-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { Options, Presets } from '@storybook/types';
import { loadConfigFromFile } from 'vite';
import { jest } from '@jest/globals';
import { commonConfig } from './vite-config';

jest.mock('vite', () => ({
...jest.requireActual('vite'),
jest.unstable_mockModule('vite', () => ({
loadConfigFromFile: jest.fn(async () => ({})),
}));
const loadConfigFromFileMock = jest.mocked(loadConfigFromFile);

const loadConfigFromFileMock = async () => {
return jest.mocked((await import('vite')).loadConfigFromFile);
};

const dummyOptions: Options = {
configType: 'DEVELOPMENT',
Expand All @@ -30,7 +32,7 @@ const dummyOptions: Options = {

describe('commonConfig', () => {
it('should preserve default envPrefix', async () => {
loadConfigFromFileMock.mockReturnValueOnce(
(await loadConfigFromFileMock()).mockReturnValueOnce(
Promise.resolve({
config: {},
path: '',
Expand All @@ -42,7 +44,7 @@ describe('commonConfig', () => {
});

it('should preserve custom envPrefix string', async () => {
loadConfigFromFileMock.mockReturnValueOnce(
(await loadConfigFromFileMock()).mockReturnValueOnce(
Promise.resolve({
config: { envPrefix: 'SECRET_' },
path: '',
Expand All @@ -54,7 +56,7 @@ describe('commonConfig', () => {
});

it('should preserve custom envPrefix array', async () => {
loadConfigFromFileMock.mockReturnValueOnce(
(await loadConfigFromFileMock()).mockReturnValueOnce(
Promise.resolve({
config: { envPrefix: ['SECRET_', 'VUE_'] },
path: '',
Expand Down

0 comments on commit a7bff76

Please sign in to comment.