Skip to content

Commit

Permalink
fix: use async/await with glob for file resolution in integration tests
Browse files Browse the repository at this point in the history
Refactored the test runner to handle promises returned by glob using async/await.
Previously, the glob function's callback pattern was used, which did not properly handle
the promises.
  • Loading branch information
cat2608 committed Dec 7, 2023
1 parent 937bc9a commit 3cdc749
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions src/test/integration/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path';
import { glob } from 'glob';
import Mocha from 'mocha';
import glob from 'glob';
import path from 'path';

export function run(): Promise<void> {
export async function run(): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: 'tdd',
Expand All @@ -12,28 +12,22 @@ export function run(): Promise<void> {

const testsRoot = path.resolve(__dirname, '.');

return new Promise((c, e) => {
glob('./**/**.test.js', { cwd: testsRoot }, (err, files) => {
if (err) {
return e(err);
}
try {
const files = await glob('./**/**.test.js', { cwd: testsRoot });

// Add files to the test suite
files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));
files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));

try {
// Run the mocha test
mocha.run(failures => {
if (failures > 0) {
e(new Error(`${failures} tests failed.`));
} else {
c();
}
});
} catch (err) {
console.error(err);
e(err);
}
return new Promise((resolve, reject) => {
mocha.run(failures => {
if (failures > 0) {
reject(new Error(`${failures} tests failed.`));
} else {
resolve();
}
});
});
});
} catch (err) {
console.error(err);
throw err; // Rethrow the error for the caller to handle
}
}

0 comments on commit 3cdc749

Please sign in to comment.