Skip to content

Commit

Permalink
fix: Better error handling on no test code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Jan 12, 2025
1 parent c9884e8 commit ffab476
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions test/notTestExportedInWorkers.test.ts
Original file line number Diff line number Diff line change
@@ -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, {})
})
}

0 comments on commit ffab476

Please sign in to comment.