componium-test
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -7,3 +7,8 @@ { | ||
"release": true | ||
}, | ||
"plugins": { | ||
"@release-it/keep-a-changelog": { | ||
"filename": "CHANGELOG.md" | ||
} | ||
} | ||
} |
{ | ||
"name": "componium-test", | ||
"description": "Componium Test - JavaScript testing framework", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"main": "index.js", | ||
@@ -18,4 +18,7 @@ "directories": { | ||
"bin": { | ||
"ct": "bin/ct.js" | ||
"ct": "./packages/bin/ct.js" | ||
}, | ||
"engines": { | ||
"node": ">=20" | ||
}, | ||
"type": "module", | ||
@@ -33,2 +36,3 @@ "author": "", | ||
"devDependencies": { | ||
"@release-it/keep-a-changelog": "^3.1.0", | ||
"prettier": "2.8.7", | ||
@@ -35,0 +39,0 @@ "release-it": "^15.10.1" |
@@ -11,2 +11,3 @@ import CliServer from "./server.js"; | ||
headless: options.keepAlive ? false : "new", | ||
devtools: true, | ||
}; | ||
@@ -13,0 +14,0 @@ } |
@@ -15,27 +15,35 @@ import { Worker } from "node:worker_threads"; | ||
console.log("Testing in Node.js", process.version); | ||
return new Promise(async (resolve) => { | ||
return new Promise(async () => { | ||
const workers = []; | ||
this.targets.forEach((target) => { | ||
// Create the worker. | ||
const worker = new Worker("./" + target); | ||
// Listen for messages from the worker and print them. | ||
worker.on("message", (msg) => { | ||
if (msg === ComponiumStatus.Fail) { | ||
failed = true; | ||
} | ||
if (msg === ComponiumStatus.Pass || msg === ComponiumStatus.Fail) { | ||
testSuites--; | ||
} | ||
if (testSuites === 0) { | ||
if (failed) { | ||
return resolve({ | ||
status: ComponiumStatus.Fail, | ||
}); | ||
} else { | ||
return resolve({ | ||
status: ComponiumStatus.Pass, | ||
}); | ||
const workerPromise = new Promise((res) => { | ||
const worker = new Worker("./" + target); | ||
// Listen for messages from the worker and print them. | ||
worker.on("message", (msg) => { | ||
if (msg === ComponiumStatus.Fail) { | ||
failed = true; | ||
} | ||
} | ||
if (msg === ComponiumStatus.Pass || msg === ComponiumStatus.Fail) { | ||
testSuites--; | ||
} | ||
res(msg); | ||
}); | ||
}); | ||
workers.push(workerPromise); | ||
}); | ||
Promise.allSettled(workers).then(() => { | ||
if (testSuites === 0) { | ||
if (failed) { | ||
return resolve({ | ||
status: ComponiumStatus.Fail, | ||
}); | ||
} else { | ||
return resolve({ | ||
status: ComponiumStatus.Pass, | ||
}); | ||
} | ||
} | ||
}); | ||
}); | ||
@@ -42,0 +50,0 @@ } |
@@ -5,3 +5,3 @@ import makeDebug from "debug"; | ||
import path from "node:path"; | ||
import { ComponiumStatus, Enviroments as Environments } from "../util/env.js"; | ||
import { Enviroments as Environments, getExitStatus } from "../util/env.js"; | ||
import NodeExecutor from "../node/executor.js"; | ||
@@ -13,26 +13,23 @@ import BrowserExecutor from "../browser/executor.js"; | ||
* | ||
* @param targetDirectory | ||
* @param environment | ||
* @param options | ||
* @param {string[]} targetDirectory - The directories containing the test files to run. | ||
* @param {string} environment - The environment in which to run the tests (either Node or Browser). | ||
* @param {object} options - Additional options for the executor. | ||
* @returns {Promise<void>} | ||
*/ | ||
async function runner(targetDirectory, environment, options) { | ||
async function runner(targetDirectory = ["tests"], environment, options) { | ||
debug("Calling runner: ", targetDirectory, environment); | ||
// currently we support one directory at a time | ||
const testFiles = await glob(path.join(targetDirectory[0], "**/*.js"), { | ||
ignore: "node_modules/**", | ||
}); | ||
let executor; | ||
if (environment === Environments.Node) { | ||
executor = new NodeExecutor(testFiles, options); | ||
} else { | ||
executor = new BrowserExecutor(testFiles, options); | ||
const testFiles = await getTestFiles(targetDirectory[0]); | ||
if (testFiles.length === 0) { | ||
console.log("No tests found..."); | ||
return process.exit(1); | ||
} | ||
// Create an executor based on the environment | ||
const executor = createExecutor(environment, testFiles, options); | ||
const result = await executor.execute(); | ||
debug("result", result); | ||
let status = 0; | ||
if (result.status === ComponiumStatus.Fail) { | ||
status = 1; | ||
} | ||
if (result.keepAlive) { | ||
// Determine the exit status based on the test result | ||
const status = getExitStatus(result); | ||
if (!result.keepAlive) { | ||
process.exit(status); | ||
@@ -42,2 +39,14 @@ } | ||
async function getTestFiles(directory) { | ||
return glob(path.join(directory, "**/*.js"), { ignore: "node_modules/**" }); | ||
} | ||
function createExecutor(environment, testFiles, options) { | ||
if (environment === Environments.Node) { | ||
return new NodeExecutor(testFiles, options); | ||
} else { | ||
return new BrowserExecutor(testFiles, options); | ||
} | ||
} | ||
export default runner; |
import { assert, should, expect } from "../assert/lib.js"; | ||
import { ComponiumStatus, isBrowser } from "../util/env.js"; | ||
import { ComponiumStatus, getExitStatus, isBrowser } from "../util/env.js"; | ||
let Emit; | ||
@@ -34,3 +34,7 @@ let parentPort; | ||
} else { | ||
if (parentPort) parentPort.postMessage(result); | ||
if (parentPort) { | ||
parentPort.postMessage(result); | ||
} else { | ||
process.exit(getExitStatus(result)); | ||
} | ||
} | ||
@@ -73,3 +77,3 @@ }); | ||
let end; | ||
let err = null; | ||
let errorOccurred = false; | ||
const tests = Object.keys(suiteObject) | ||
@@ -97,3 +101,3 @@ .filter((key) => !INTERNAL_SUITE_METHODS.includes(key)) | ||
this.emitter.emit("failed", key, e); | ||
err = true; | ||
errorOccurred = true; | ||
failed++; | ||
@@ -106,6 +110,6 @@ totalTestsFailed++; | ||
end = performance.now(); | ||
if (!err) { | ||
if (!errorOccurred) { | ||
this.emitter.emit("passed", key, end - start); | ||
} | ||
err = null; | ||
errorOccurred = null; | ||
} | ||
@@ -112,0 +116,0 @@ } |
@@ -17,2 +17,7 @@ const Enviroments = { | ||
}; | ||
export { isBrowser, Enviroments, ComponiumStatus }; | ||
function getExitStatus(result) { | ||
return result.status === ComponiumStatus.Fail ? 1 : 0; | ||
} | ||
export { isBrowser, Enviroments, ComponiumStatus, getExitStatus }; |
// import framework modules | ||
import ct, { assert } from "componium-test"; | ||
// fixture to test | ||
import Calculator from "./fixtures/calculator.js"; | ||
import Calculator from "../fixtures/calculator.js"; | ||
@@ -6,0 +6,0 @@ let calc; |
import ct, { assert } from "componium-test"; | ||
ct({ | ||
one: async function () { | ||
one: function () { | ||
let a = 1; | ||
@@ -6,0 +6,0 @@ assert.strictEqual(a, 1, "a is 1"); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
492
17425
3
26