Skip to content

Commit cd85e10

Browse files
authored
test(core): Use a fake global state for integ/e2e tests (#6565)
## Problem Features that depend on global state can cause test failures locally because the tests will use your current global state settings instead of a fake state for integ/e2e tests ## Solution integ/e2e tests should use a fake global state instead that automatically gets reset after each test --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent ce99b61 commit cd85e10

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import * as tokenProvider from '../auth/sso/ssoAccessTokenProvider'
1616
import * as testUtil from '../test/testUtil'
1717
import { DeviceFlowAuthorization } from '../auth/sso/ssoAccessTokenProvider'
1818
import { globals } from '../shared'
19+
import { GlobalState } from '../shared/globalState'
20+
import { FakeExtensionContext } from '../test/fakeExtensionContext'
1921

2022
// ASSUMPTION: Tests are not run concurrently
2123

@@ -48,6 +50,15 @@ export async function mochaGlobalSetup(extensionId: string) {
4850
if (getLogger() instanceof ToolkitLogger) {
4951
;(getLogger() as ToolkitLogger).logToConsole()
5052
}
53+
54+
const fakeContext = await FakeExtensionContext.create()
55+
const testFolder = await testUtil.TestFolder.create()
56+
fakeContext.globalStorageUri = vscode.Uri.parse(testFolder.pathFrom('.'))
57+
fakeContext.extensionPath = globals.context.extensionPath
58+
Object.assign(globals, {
59+
// eslint-disable-next-line aws-toolkits/no-banned-usages
60+
globalState: new GlobalState(fakeContext.globalState),
61+
})
5162
}
5263
}
5364

@@ -58,9 +69,12 @@ export async function mochaGlobalTeardown(this: Mocha.Context) {
5869
}
5970

6071
export const mochaHooks = {
61-
beforeEach(this: Mocha.Context) {
72+
async beforeEach(this: Mocha.Context) {
6273
globals.telemetry.clearRecords()
6374
globals.telemetry.logger.clear()
75+
76+
// mochaGlobalSetup() set this to a fake, so it's safe to clear it here.
77+
await globals.globalState.clear()
6478
},
6579
afterEach(this: Mocha.Context) {
6680
patchWindow()

0 commit comments

Comments
 (0)