-
Notifications
You must be signed in to change notification settings - Fork 797
Expand file tree
/
Copy pathtestRunner.ts
More file actions
66 lines (58 loc) · 2.03 KB
/
testRunner.ts
File metadata and controls
66 lines (58 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* The following was influenced by this guide: https://code.visualstudio.com/api/extension-guides/web-extensions
*/
import { VSCODE_EXTENSION_ID_CONSTANTS } from 'aws-core-vscode/utils'
import 'mocha' // Imports mocha for the browser, defining the `mocha` global.
import * as vscode from 'vscode'
export async function run(): Promise<void> {
await activateExtension()
return new Promise(async (resolve, reject) => {
setupMocha()
gatherTestFiles()
try {
runMochaTests(resolve, reject)
} catch (err) {
console.error(err)
reject(err)
}
})
}
function setupMocha() {
mocha.setup({
ui: 'bdd',
reporter: undefined,
})
}
function gatherTestFiles() {
// Bundles all files in the current directory matching `*.test`
// eslint-disable-next-line unicorn/no-array-for-each
const importAll = (r: __WebpackModuleApi.RequireContext) => r.keys().forEach(r)
importAll(require.context('.', true, /\.test$/))
}
/**
* Typically extensions activate depending on their configuration in `package.json#activationEvents`, but in tests
* there is a race condition for when the extension has finished activating and when we start the tests.
*
* So this function ensures the extension has fully activated.
*/
async function activateExtension() {
const extId = VSCODE_EXTENSION_ID_CONSTANTS.amazonq
const ext = vscode.extensions.getExtension(extId)
if (!ext) {
throw new Error(`Extension '${extId}' not found, can't activate it to run tests.`)
}
await vscode.extensions.getExtension(VSCODE_EXTENSION_ID_CONSTANTS.amazonq)?.activate()
}
function runMochaTests(resolve: (value: void | PromiseLike<void>) => void, reject: (reason?: any) => void) {
mocha.run((failures) => {
if (failures > 0) {
reject(new Error(`${failures} tests failed.`))
} else {
resolve()
}
})
}