diff --git a/test/runTests.test.js b/test/runTests.test.js index ae9efd2..281623d 100644 --- a/test/runTests.test.js +++ b/test/runTests.test.js @@ -52,11 +52,19 @@ if (process.platform === "win32") { // }); // Run tests in Docker container -describe("Run tests sucessfully", async function () { +describe("Run tests successfully", async function () { // Set indefinite timeout this.timeout(0); - it("All specs pass", async () => { return new Promise((resolve, reject) => { - let hasExited = false; + it("All specs pass", async () => { + return new Promise((resolve, reject) => { + let hasCompleted = false; + + const handleCompletion = (callback) => { + if (hasCompleted) return; + hasCompleted = true; + callback(); + }; + const runTests = exec( `docker run --rm -v "${artifactPath}:${internalPath}" docdetective/docdetective:${version}-${os} -c ./config.json -i . -o ./results.json` ); @@ -71,34 +79,30 @@ describe("Run tests sucessfully", async function () { runTests.on("error", (error) => { console.error(`Error: ${error.message}`); - if (!hasExited) { - hasExited = true; - reject(error); - } + handleCompletion(() => reject(error)); }); - runTests.on("exit", (code) => { - if (hasExited) return; - hasExited = true; - - console.log(`Child process exited with code ${code}`); - - if (code !== null && code !== 0) { - reject(new Error(`Docker process exited with code ${code}`)); - return; - } - - try { - const result = JSON.parse( - fs.readFileSync(outputFile, { encoding: "utf8" }) - ); - console.log(JSON.stringify(result, null, 2)); - assert.equal(result.summary.specs.fail, 0); - fs.unlinkSync(outputFile); - resolve(); - } catch (error) { - reject(error); - } + runTests.on("close", (code) => { + handleCompletion(() => { + console.log(`Child process closed with code ${code}`); + + if (code !== null && code !== 0) { + reject(new Error(`Docker process exited with code ${code}`)); + return; + } + + try { + const result = JSON.parse( + fs.readFileSync(outputFile, { encoding: "utf8" }) + ); + console.log(JSON.stringify(result, null, 2)); + assert.equal(result.summary.specs.fail, 0); + fs.unlinkSync(outputFile); + resolve(); + } catch (error) { + reject(error); + } + }); }); }); });