Skip to content

Commit 4d14422

Browse files
authored
fix(tests): use node:fs when globals are not initialized #5772
## Problem In https://github.com/aws/aws-toolkit-vscode/blob/16aa3684f479566bfcf6a9e33f88e70039831e9a/packages/core/src/test/globalSetup.test.ts#L48, we use our fs.fs module which accesses globals.isWeb, but globals are not initialized yet. ``` Exception has occurred: Error: ToolkitGlobals accessed before initialize() at Object.get (/Volumes/workplace/aws-toolkit-vscode/packages/core/src/shared/extensionGlobals.ts:109:23) at FileSystem.get isWeb [as isWeb] (/Volumes/workplace/aws-toolkit-vscode/packages/core/src/shared/fs/fs.ts:689:24) at FileSystem.mkdir (/Volumes/workplace/aws-toolkit-vscode/packages/core/src/shared/fs/fs.ts:94:63) at Runner.<anonymous> (/Volumes/workplace/aws-toolkit-vscode/packages/core/src/test/globalSetup.test.ts:48:18) ``` To reproduce, go into master, run any test file individually with "Extension Tests (current file) (amazonq)". ## Solution Use node's fs when setting up tests since we must wait for global context to be initialized to use our fs.
1 parent 7dd3dd7 commit 4d14422

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

packages/core/src/test/globalSetup.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import { GlobalState } from '../shared/globalState'
2626
import { FeatureConfigProvider } from '../shared/featureConfig'
2727
import { mockFeatureConfigsData } from './fake/mockFeatureConfigData'
2828
import { fs } from '../shared'
29+
import { promises as nodefs } from 'fs' //eslint-disable-line no-restricted-imports
2930

3031
disableAwsSdkWarning()
31-
3232
const testReportDir = join(__dirname, '../../../../../.test-reports') // Root project, not subproject
3333
const testLogOutput = join(testReportDir, 'testLog.log')
3434
const globalSandbox = sinon.createSandbox()
@@ -42,10 +42,9 @@ let openExternalStub: sinon.SinonStub<Parameters<(typeof vscode)['env']['openExt
4242
export async function mochaGlobalSetup(extensionId: string) {
4343
return async function (this: Mocha.Runner) {
4444
// Clean up and set up test logs
45-
try {
46-
await fs.delete(testLogOutput)
47-
} catch (e) {}
48-
await fs.mkdir(testReportDir)
45+
// Use nodefs instead of our fs module (which uses globals.isWeb, which is not initialized yet).
46+
await nodefs.rm(testLogOutput, { force: true })
47+
await nodefs.mkdir(testReportDir, { recursive: true })
4948

5049
sinon.stub(FeatureConfigProvider.prototype, 'listFeatureEvaluations').resolves({
5150
featureEvaluations: mockFeatureConfigsData,

0 commit comments

Comments
 (0)