|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * The following was influenced by this guide: https://code.visualstudio.com/api/extension-guides/web-extensions |
| 8 | + */ |
| 9 | + |
| 10 | +import { VSCODE_EXTENSION_ID } from 'aws-core-vscode/utils' |
| 11 | +import 'mocha' // Imports mocha for the browser, defining the `mocha` global. |
| 12 | +import * as vscode from 'vscode' |
| 13 | + |
| 14 | +export async function run(): Promise<void> { |
| 15 | + await activateToolkitExtension() |
| 16 | + return new Promise(async (resolve, reject) => { |
| 17 | + setupMocha() |
| 18 | + gatherTestFiles() |
| 19 | + |
| 20 | + try { |
| 21 | + runMochaTests(resolve, reject) |
| 22 | + } catch (err) { |
| 23 | + console.error(err) |
| 24 | + reject(err) |
| 25 | + } |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +function setupMocha() { |
| 30 | + mocha.setup({ |
| 31 | + ui: 'bdd', |
| 32 | + reporter: undefined, |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +function gatherTestFiles() { |
| 37 | + // Bundles all files in the current directory matching `*.test` |
| 38 | + const importAll = (r: __WebpackModuleApi.RequireContext) => r.keys().forEach(r) |
| 39 | + importAll(require.context('.', true, /\.test$/)) |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Typically extensions activate depending on their configuration in `package.json#activationEvents`, but in tests |
| 44 | + * there is a race condition for when the extension has finished activating and when we start the tests. |
| 45 | + * |
| 46 | + * So this function ensures the extension has fully activated. |
| 47 | + */ |
| 48 | +async function activateToolkitExtension() { |
| 49 | + await vscode.extensions.getExtension(VSCODE_EXTENSION_ID.amazonq)?.activate() |
| 50 | +} |
| 51 | + |
| 52 | +function runMochaTests(resolve: (value: void | PromiseLike<void>) => void, reject: (reason?: any) => void) { |
| 53 | + mocha.run((failures) => { |
| 54 | + if (failures > 0) { |
| 55 | + reject(new Error(`${failures} tests failed.`)) |
| 56 | + } else { |
| 57 | + resolve() |
| 58 | + } |
| 59 | + }) |
| 60 | +} |
0 commit comments