Skip to content

Commit 84e7e00

Browse files
authored
Merge pull request GSA-TTS#192 from GSA-TTS/unstub-global
Automatically Unstub Globals
2 parents 532f1ee + a1bc2be commit 84e7e00

2 files changed

Lines changed: 7 additions & 10 deletions

File tree

ui/src/pages/UploadPage/uploadPageController.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it, vi } from 'vitest';
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
22
import { callCreateDocumentApi } from './uploadPageController';
33
import * as api from '../../utils/api';
44

@@ -28,10 +28,12 @@ class MockFileReader {
2828
}
2929
}
3030

31-
// allow `new FileReader()` to work even in a node (test) environment
32-
vi.stubGlobal('FileReader', MockFileReader);
33-
3431
describe('callCreateDocumentApi', () => {
32+
beforeEach(() => {
33+
// allow `new FileReader()` to work even in a node (test) environment
34+
vi.stubGlobal('FileReader', MockFileReader);
35+
});
36+
3537
const mockAuthorizedFetch = api.authorizedFetch as ReturnType<typeof vi.fn>;
3638

3739
// Create a mock File object
@@ -165,10 +167,8 @@ describe('callCreateDocumentApi', () => {
165167

166168
it('should handle file reading errors', async () => {
167169
// Set the MockFileReader to fail
168-
const originalFileReader = globalThis.FileReader;
169170
const mockFileReaderInstance = new MockFileReader();
170171
mockFileReaderInstance.readSuccess = false;
171-
172172
// Override the FileReader constructor to return our configured instance
173173
vi.stubGlobal('FileReader', function () {
174174
return mockFileReaderInstance;
@@ -181,9 +181,6 @@ describe('callCreateDocumentApi', () => {
181181
failure: 'other',
182182
responseData: undefined,
183183
});
184-
185-
// Restore the original FileReader
186-
vi.stubGlobal('FileReader', originalFileReader);
187184
});
188185

189186
it('should handle empty files', async () => {

ui/vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ export default defineConfig({
55
environment: 'node',
66
restoreMocks: true,
77
unstubEnvs: true,
8-
// unstubGlobals: true,
8+
unstubGlobals: true,
99
},
1010
});

0 commit comments

Comments
 (0)