diff --git a/src/worker.ts b/src/worker.ts index 4b4a9ef..29fee5a 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -168,7 +168,13 @@ export function runWorker(context: WorkerContext, notifier: (value: any) => void const { warmup, tests, index, iterations, errorThreshold } = context // Require the original file to build tests - const [name, testDefinition] = tests[index] + const testToRun = tests?.[index] + + if (!testToRun) { + throw new Error('No test code exported from the worker thread') + } + + const [name, testDefinition] = testToRun // Prepare the test let test: TestFunction = noOp diff --git a/test/notTestExportedInWorkers.test.ts b/test/notTestExportedInWorkers.test.ts new file mode 100644 index 0000000..169754b --- /dev/null +++ b/test/notTestExportedInWorkers.test.ts @@ -0,0 +1,38 @@ +import { deepStrictEqual, ok } from 'node:assert' +import { test } from 'node:test' +import { isMainThread } from 'node:worker_threads' +import { cronometro } from '../src/index.js' + +if (isMainThread) { + test('Errors are properly handled when no tests are exported in the worker threads', async () => { + const results = await cronometro( + { + single() {}, + multiple() {} + }, + { iterations: 10, print: false } + ) + + ok(!results.single.success) + ok(results.single.error instanceof Error) + deepStrictEqual(results.single.error.message, 'No test code exported from the worker thread') + deepStrictEqual(results.single.size, 0) + deepStrictEqual(results.single.min, 0) + deepStrictEqual(results.single.max, 0) + deepStrictEqual(results.single.mean, 0) + deepStrictEqual(results.single.stddev, 0) + deepStrictEqual(results.single.standardError, 0) + deepStrictEqual(results.single.percentiles, {}) + + ok(!results.multiple.success) + ok(results.multiple.error instanceof Error) + deepStrictEqual(results.multiple.error.message, 'No test code exported from the worker thread') + deepStrictEqual(results.multiple.size, 0) + deepStrictEqual(results.multiple.min, 0) + deepStrictEqual(results.multiple.max, 0) + deepStrictEqual(results.multiple.mean, 0) + deepStrictEqual(results.multiple.stddev, 0) + deepStrictEqual(results.multiple.standardError, 0) + deepStrictEqual(results.multiple.percentiles, {}) + }) +}