From a7bff7690eb7244e7dd219b9dc473d3b2d304119 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Thu, 5 Oct 2023 14:52:41 -0400 Subject: [PATCH] Try to use ESM mocks for jest test --- .../builder-vite/src/vite-config.test.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/code/builders/builder-vite/src/vite-config.test.ts b/code/builders/builder-vite/src/vite-config.test.ts index c4f2f212be48..922c50cb90ac 100644 --- a/code/builders/builder-vite/src/vite-config.test.ts +++ b/code/builders/builder-vite/src/vite-config.test.ts @@ -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', @@ -30,7 +32,7 @@ const dummyOptions: Options = { describe('commonConfig', () => { it('should preserve default envPrefix', async () => { - loadConfigFromFileMock.mockReturnValueOnce( + (await loadConfigFromFileMock()).mockReturnValueOnce( Promise.resolve({ config: {}, path: '', @@ -42,7 +44,7 @@ describe('commonConfig', () => { }); it('should preserve custom envPrefix string', async () => { - loadConfigFromFileMock.mockReturnValueOnce( + (await loadConfigFromFileMock()).mockReturnValueOnce( Promise.resolve({ config: { envPrefix: 'SECRET_' }, path: '', @@ -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: '',