@@ -22,7 +22,8 @@ import { activateExtension } from '../shared/utilities/vsCodeUtils'
22
22
import { FakeExtensionContext , FakeMemento } from './fakeExtensionContext'
23
23
import { TestLogger } from './testLogger'
24
24
import * as testUtil from './testUtil'
25
- import { printPendingUiElements , getTestWindow , resetTestWindow } from './shared/vscode/window'
25
+ import { getTestWindow , resetTestWindow } from './shared/vscode/window'
26
+ import { setRunnableTimeout } from './setupUtil'
26
27
27
28
const testReportDir = join ( __dirname , '../../../.test-reports' )
28
29
const testLogOutput = join ( testReportDir , 'testLog.log' )
@@ -33,7 +34,7 @@ const maxTestDuration = 30_000
33
34
let testLogger : TestLogger | undefined
34
35
let openExternalStub : sinon . SinonStub < Parameters < typeof vscode [ 'env' ] [ 'openExternal' ] > , Thenable < boolean > >
35
36
36
- before ( async function ( ) {
37
+ export async function mochaGlobalSetup ( this : Mocha . Context ) {
37
38
// Clean up and set up test logs
38
39
try {
39
40
await remove ( testLogOutput )
@@ -48,68 +49,51 @@ before(async function () {
48
49
fakeContext . globalStorageUri = ( await testUtil . createTestWorkspaceFolder ( 'globalStoragePath' ) ) . uri
49
50
fakeContext . extensionPath = globals . context . extensionPath
50
51
Object . assign ( globals , { context : fakeContext } )
51
- } )
52
+ }
52
53
53
- after ( async function ( ) {
54
+ export async function mochaGlobalTeardown ( this : Mocha . Context ) {
54
55
testUtil . deleteTestTempDirs ( )
55
- } )
56
-
57
- beforeEach ( async function ( ) {
58
- // Set every test up so that TestLogger is the logger used by toolkit code
59
- testLogger = setupTestLogger ( )
60
- globals . templateRegistry = new CloudFormationTemplateRegistry ( )
61
- globals . codelensRootRegistry = new CodelensRootRegistry ( )
62
-
63
- // In general, we do not want to "fake" the `vscode` API. The only exception is for things
64
- // that _require_ user input apart of a workflow. Even then, these replacements are intended
65
- // to be minimally intrusive and as close to the real thing as possible.
66
- globalSandbox . replace ( vscode , 'window' , getTestWindow ( ) )
67
- openExternalStub = globalSandbox . stub ( vscode . env , 'openExternal' )
68
- openExternalStub . rejects (
69
- new Error ( 'No return value has been set. Use `getOpenExternalStub().resolves` to set one.' )
70
- )
56
+ }
71
57
72
- // Wraps the test function to bubble up errors that occurred in events from `TestWindow`
73
- if ( this . currentTest ?. fn ) {
74
- const testFn = this . currentTest . fn
75
- this . currentTest . fn = async function ( done ) {
76
- return Promise . race ( [
77
- testFn . call ( this , done ) ,
78
- new Promise < void > ( ( _ , reject ) => {
79
- getTestWindow ( ) . onError ( ( { event, error } ) => {
80
- event . dispose ( )
81
- reject ( error )
82
- } )
83
-
84
- // Set a hard time limit per-test so CI doesn't hang
85
- // Mocha's `timeout` method isn't used because we want to emit a custom message
86
- setTimeout ( ( ) => {
87
- const message = `Test length exceeded max duration\n${ printPendingUiElements ( ) } `
88
- reject ( new Error ( message ) )
89
- } , maxTestDuration )
90
- } ) ,
91
- ] )
58
+ export const mochaHooks = {
59
+ async beforeEach ( this : Mocha . Context ) {
60
+ // Set every test up so that TestLogger is the logger used by toolkit code
61
+ testLogger = setupTestLogger ( )
62
+ globals . templateRegistry = new CloudFormationTemplateRegistry ( )
63
+ globals . codelensRootRegistry = new CodelensRootRegistry ( )
64
+
65
+ // In general, we do not want to "fake" the `vscode` API. The only exception is for things
66
+ // that _require_ user input apart of a workflow. Even then, these replacements are intended
67
+ // to be minimally intrusive and as close to the real thing as possible.
68
+ globalSandbox . replace ( vscode , 'window' , getTestWindow ( ) )
69
+ openExternalStub = globalSandbox . stub ( vscode . env , 'openExternal' )
70
+ openExternalStub . rejects (
71
+ new Error ( 'No return value has been set. Use `getOpenExternalStub().resolves` to set one.' )
72
+ )
73
+
74
+ // Wraps the test function to bubble up errors that occurred in events from `TestWindow`
75
+ if ( this . currentTest ?. fn ) {
76
+ setRunnableTimeout ( this . currentTest , maxTestDuration )
92
77
}
93
- }
94
-
95
- // Enable telemetry features for tests. The metrics won't actually be posted.
96
- globals . telemetry . telemetryEnabled = true
97
- globals . telemetry . clearRecords ( )
98
- globals . telemetry . logger . clear ( )
99
- ; ( globals . context as FakeExtensionContext ) . globalState = new FakeMemento ( )
100
-
101
- await testUtil . closeAllEditors ( )
102
- } )
103
-
104
- afterEach ( function ( ) {
105
- // Prevent other tests from using the same TestLogger instance
106
- teardownTestLogger ( this . currentTest ?. fullTitle ( ) as string )
107
- testLogger = undefined
108
- resetTestWindow ( )
109
- globals . templateRegistry . dispose ( )
110
- globals . codelensRootRegistry . dispose ( )
111
- globalSandbox . restore ( )
112
- } )
78
+
79
+ // Enable telemetry features for tests. The metrics won't actually be posted.
80
+ globals . telemetry . telemetryEnabled = true
81
+ globals . telemetry . clearRecords ( )
82
+ globals . telemetry . logger . clear ( )
83
+ ; ( globals . context as FakeExtensionContext ) . globalState = new FakeMemento ( )
84
+
85
+ await testUtil . closeAllEditors ( )
86
+ } ,
87
+ afterEach ( this : Mocha . Context ) {
88
+ // Prevent other tests from using the same TestLogger instance
89
+ teardownTestLogger ( this . currentTest ?. fullTitle ( ) as string )
90
+ testLogger = undefined
91
+ resetTestWindow ( )
92
+ globals . templateRegistry . dispose ( )
93
+ globals . codelensRootRegistry . dispose ( )
94
+ globalSandbox . restore ( )
95
+ } ,
96
+ }
113
97
114
98
/**
115
99
* Provides the TestLogger to tests that want to access it.
0 commit comments