Skip to content

Commit c089c26

Browse files
Copilotalexr00
andcommitted
Generate individual *.test.js files from webpack compilation
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 9735730 commit c089c26

2 files changed

Lines changed: 38 additions & 21 deletions

File tree

src/test/index.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,29 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
// @ts-nocheck
7-
86
// This file is providing the test runner to use when running extension tests.
97
import * as path from 'path';
108
import * as vscode from 'vscode';
9+
import glob from 'glob';
1110
import Mocha from 'mocha';
1211
import { mockWebviewEnvironment } from './mocks/mockWebviewEnvironment';
1312
import { EXTENSION_ID } from '../constants';
1413

14+
function addTests(mocha: Mocha, root: string): Promise<void> {
15+
return new Promise((resolve, reject) => {
16+
glob('**/**.test.js', { cwd: root }, (error, files) => {
17+
if (error) {
18+
return reject(error);
19+
}
20+
21+
for (const testFile of files) {
22+
mocha.addFile(path.join(root, testFile));
23+
}
24+
resolve();
25+
});
26+
});
27+
}
28+
1529
async function runAllExtensionTests(testsRoot: string, clb: (error: Error | null, failures?: number) => void): Promise<any> {
1630
// Ensure the dev-mode extension is activated
1731
await vscode.extensions.getExtension(EXTENSION_ID)!.activate();
@@ -22,23 +36,10 @@ async function runAllExtensionTests(testsRoot: string, clb: (error: Error | null
2236
ui: 'bdd',
2337
color: true
2438
});
39+
mocha.addFile(path.resolve(testsRoot, 'globalHooks.js'));
2540

26-
// Load globalHooks if it exists
27-
try {
28-
mocha.addFile(path.resolve(testsRoot, 'globalHooks.js'));
29-
} catch (e) {
30-
// globalHooks might not exist in webpack bundle, ignore
31-
}
32-
33-
// Import all test files using webpack's require.context
34-
try {
35-
// Load tests from src/test directory only
36-
// Webview tests are compiled separately with the webview configuration
37-
const importAll = (r: __WebpackModuleApi.RequireContext) => r.keys().forEach(r);
38-
importAll(require.context('./', true, /\.test$/));
39-
} catch (e) {
40-
console.log('Error loading tests:', e);
41-
}
41+
await addTests(mocha, testsRoot);
42+
await addTests(mocha, path.resolve(testsRoot, '../../../webviews/'));
4243

4344
if (process.env.TEST_JUNIT_XML_PATH) {
4445
mocha.reporter('mocha-multi-reporters', {

webpack.config.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,12 @@ async function getWebviewConfig(mode, env, entry) {
156156
*/
157157
async function getExtensionConfig(target, mode, env) {
158158
const basePath = path.join(__dirname, 'src');
159+
const glob = require('glob');
159160

160161
/**
161162
* @type WebpackConfig['plugins'] | any
162163
*/
163164
const plugins = [
164-
new webpack.optimize.LimitChunkCountPlugin({
165-
maxChunks: 1
166-
}),
167165
new ForkTsCheckerPlugin({
168166
async: false,
169167
formatter: 'basic',
@@ -190,10 +188,28 @@ async function getExtensionConfig(target, mode, env) {
190188
const entry = {
191189
extension: './src/extension.ts',
192190
};
191+
192+
// Add test entry points
193193
if (target === 'webworker') {
194194
entry['test/index'] = './src/test/browser/index.ts';
195195
} else if (target === 'node') {
196+
// Add main test runner
196197
entry['test/index'] = './src/test/index.ts';
198+
199+
// Add individual test files as separate entry points
200+
const testFiles = glob.sync('src/test/**/*.test.ts', { cwd: __dirname });
201+
testFiles.forEach(testFile => {
202+
// Convert src/test/github/utils.test.ts -> test/github/utils.test
203+
const entryName = testFile.replace('src/', '').replace('.ts', '');
204+
entry[entryName] = `./${testFile}`;
205+
});
206+
}
207+
208+
// Don't limit chunks for node target when we have individual test files
209+
if (target !== 'node' || !('test/index' in entry && Object.keys(entry).some(key => key.endsWith('.test')))) {
210+
plugins.unshift(new webpack.optimize.LimitChunkCountPlugin({
211+
maxChunks: 1
212+
}));
197213
}
198214

199215
return {

0 commit comments

Comments
 (0)