|
1 |
| -// imports mocha for the browser, defining the `mocha` global. |
2 |
| -require('mocha/mocha'); |
| 1 | +// Re-export extension entry point, so that the output from this file |
| 2 | +// when bundled can be used as entry point for extension as well as tests. |
| 3 | +// The same objects/types will be used as the module is only ever loaded once by nodejs. |
| 4 | +import * as extension from '../../../extension.web'; |
| 5 | +import * as vscode from 'vscode'; |
| 6 | +import type { IExtensionApi } from '../../../platform/api'; |
| 7 | +import type { IExtensionContext } from '../../../platform/common/types'; |
| 8 | +import { IExtensionTestApi } from '../../common'; |
| 9 | +import { JVSC_EXTENSION_ID } from '../../../platform/common/constants'; |
3 | 10 |
|
4 |
| -export function run(): Promise<void> { |
5 |
| - return new Promise((c, e) => { |
6 |
| - mocha.setup({ |
7 |
| - ui: 'tdd', |
8 |
| - reporter: undefined |
9 |
| - }); |
| 11 | +let activatedResponse: undefined | IExtensionApi; |
10 | 12 |
|
11 |
| - // bundles all files in the current directory matching `*.test` |
12 |
| - const importAll = (r: __WebpackModuleApi.RequireContext) => r.keys().forEach(r); |
13 |
| - importAll(require.context('.', true, /\.web.test$/)); |
| 13 | +// Basically this is the entry point for the extension. |
| 14 | +export async function activate(context: IExtensionContext): Promise<IExtensionApi> { |
| 15 | + if (activatedResponse) { |
| 16 | + return activatedResponse; |
| 17 | + } |
| 18 | + vscode.commands.registerCommand('jupyter.web.runTests', async () => { |
| 19 | + // imports mocha for the browser, defining the `mocha` global. |
| 20 | + require('mocha/mocha'); |
14 | 21 |
|
15 |
| - try { |
16 |
| - // Run the mocha test |
17 |
| - mocha.run((failures) => { |
18 |
| - if (failures > 0) { |
19 |
| - e(new Error(`${failures} tests failed.`)); |
20 |
| - } else { |
21 |
| - c(); |
22 |
| - } |
| 22 | + return new Promise<void>((resolve, reject) => { |
| 23 | + mocha.setup({ |
| 24 | + ui: 'tdd', |
| 25 | + reporter: undefined |
23 | 26 | });
|
24 |
| - } catch (err) { |
25 |
| - console.error(err); |
26 |
| - e(err); |
27 |
| - } |
| 27 | + |
| 28 | + // bundles all files in the current directory matching `*.test` |
| 29 | + const importAll = (r: __WebpackModuleApi.RequireContext) => r.keys().forEach(r); |
| 30 | + importAll(require.context('.', true, /\.web.test$/)); |
| 31 | + |
| 32 | + try { |
| 33 | + // Run the mocha test |
| 34 | + mocha.run((failures) => { |
| 35 | + if (failures > 0) { |
| 36 | + reject(new Error(`${failures} tests failed.`)); |
| 37 | + } else { |
| 38 | + resolve(); |
| 39 | + } |
| 40 | + }); |
| 41 | + } catch (err) { |
| 42 | + console.error(err); |
| 43 | + reject(err); |
| 44 | + } |
| 45 | + }); |
28 | 46 | });
|
| 47 | + activatedResponse = await extension.activate(context); |
| 48 | + return activatedResponse; |
| 49 | +} |
| 50 | + |
| 51 | +export async function deactivate(): Promise<void> { |
| 52 | + return extension.deactivate(); |
| 53 | +} |
| 54 | + |
| 55 | +export async function run(): Promise<void> { |
| 56 | + // Activate the extension so that the commands are registered. |
| 57 | + // Also this will not slow down the suite-setups. |
| 58 | + const extension = vscode.extensions.getExtension<IExtensionTestApi>(JVSC_EXTENSION_ID)!; |
| 59 | + const api = await extension.activate(); |
| 60 | + await api.ready; |
| 61 | + // Run the tests from within the context of the extension bundle. |
| 62 | + // We achieve this by getting the extension to run the tests (then its guaranteed to use the same context as the extension). |
| 63 | + await vscode.commands.executeCommand('jupyter.web.runTests'); |
29 | 64 | }
|
0 commit comments