Skip to content

Commit 8c5d73b

Browse files
committed
Simplify tests index-template
Instead of using the `glob` library and a custom promise, this will use `glob-promise` which is used by other parts of the codebase already. This reduces the amount of code which manually needs to call `reject` and makes it easier to read.
1 parent 81b53c9 commit 8c5d73b

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

extensions/ql-vscode/src/vscode-tests/index-template.ts

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'path';
22
import * as Mocha from 'mocha';
3-
import * as glob from 'glob';
3+
import * as glob from 'glob-promise';
44
import { ensureCli } from './ensureCli';
55
import { env } from 'vscode';
66

@@ -57,44 +57,39 @@ export async function runTestsInDirectory(testsRoot: string, useCli = false): Pr
5757

5858
await ensureCli(useCli);
5959

60-
return new Promise((resolve, reject) => {
61-
console.log(`Adding test cases and helpers from ${testsRoot}`);
62-
glob('**/**.js', { cwd: testsRoot }, (err, files) => {
63-
if (err) {
64-
return reject(err);
65-
}
60+
console.log(`Adding test cases and helpers from ${testsRoot}`);
61+
62+
const files = await glob('**/**.js', { cwd: testsRoot });
63+
64+
// Add test files to the test suite
65+
files
66+
.filter(f => f.endsWith('.test.js'))
67+
.forEach(f => {
68+
console.log(`Adding test file ${f}`);
69+
mocha.addFile(path.resolve(testsRoot, f));
70+
});
6671

67-
try {
68-
// Add test files to the test suite
69-
files
70-
.filter(f => f.endsWith('.test.js'))
71-
.forEach(f => {
72-
console.log(`Adding test file ${f}`);
73-
mocha.addFile(path.resolve(testsRoot, f));
74-
});
7572

76-
// Add helpers. Helper files add global setup and teardown blocks
77-
// for a test run.
78-
files
79-
.filter(f => f.endsWith('.helper.js'))
80-
.forEach(f => {
81-
console.log(`Executing helper ${f}`);
82-
// eslint-disable-next-line @typescript-eslint/no-var-requires
83-
const helper = require(path.resolve(testsRoot, f)).default;
84-
helper(mocha);
85-
});
73+
// Add helpers. Helper files add global setup and teardown blocks
74+
// for a test run.
75+
files
76+
.filter(f => f.endsWith('.helper.js'))
77+
.forEach(f => {
78+
console.log(`Executing helper ${f}`);
79+
// eslint-disable-next-line @typescript-eslint/no-var-requires
80+
const helper = require(path.resolve(testsRoot, f)).default;
81+
helper(mocha);
82+
});
8683

87-
// Run the mocha test
88-
mocha.run(failures => {
89-
if (failures > 0) {
90-
reject(new Error(`${failures} tests failed.`));
91-
} else {
92-
resolve();
93-
}
94-
});
95-
} catch (err) {
96-
reject(err);
84+
return new Promise((resolve, reject) => {
85+
// Run the mocha test
86+
mocha.run(failures => {
87+
if (failures > 0) {
88+
reject(new Error(`${failures} tests failed.`));
89+
return;
9790
}
91+
92+
resolve();
9893
});
9994
});
10095
}

0 commit comments

Comments
 (0)