switch (taskFunctionOperation) {
case 'add':
if (typeof taskFunction !== 'string') {
- throw new Error(
+ throw new TypeError(
`Cannot handle task function operation ${taskFunctionOperation} message without task function`
)
}
: 'fn1',
fnObj
)
- } else if (isPlainObject(taskFunctions)) {
- let firstEntry = true
- for (let [name, fnObj] of Object.entries(taskFunctions)) {
- if (typeof fnObj === 'function') {
- fnObj = { taskFunction: fnObj } satisfies TaskFunctionObject<
- Data,
- Response
- >
- }
- checkValidTaskFunctionObjectEntry<Data, Response>(name, fnObj)
- fnObj.taskFunction = fnObj.taskFunction.bind(this)
- if (firstEntry) {
- this.taskFunctions.set(DEFAULT_TASK_NAME, fnObj)
- firstEntry = false
- }
- this.taskFunctions.set(name, fnObj)
- }
- if (firstEntry) {
- throw new Error('taskFunctions parameter object is empty')
- }
- } else {
+ return
+ }
+ if (!isPlainObject(taskFunctions)) {
throw new TypeError(
'taskFunctions parameter is not a function or a plain object'
)
}
+ let firstEntry = true
+ for (let [name, fnObj] of Object.entries(taskFunctions)) {
+ if (typeof fnObj === 'function') {
+ fnObj = { taskFunction: fnObj } satisfies TaskFunctionObject<
+ Data,
+ Response
+ >
+ }
+ checkValidTaskFunctionObjectEntry<Data, Response>(name, fnObj)
+ fnObj.taskFunction = fnObj.taskFunction.bind(this)
+ if (firstEntry) {
+ this.taskFunctions.set(DEFAULT_TASK_NAME, fnObj)
+ firstEntry = false
+ }
+ this.taskFunctions.set(name, fnObj)
+ }
+ if (firstEntry) {
+ throw new Error('taskFunctions parameter object is empty')
+ }
}
private checkWorkerOptions (opts: WorkerOptions): void {
)
})
- it('Verify that handleTaskFunctionOperationMessage() throws add without function', () => {
- const worker = new ThreadWorker(() => {})
- expect(() =>
- worker.handleTaskFunctionOperationMessage({
- taskFunctionOperation: 'add',
- taskFunctionProperties: { name: 'fn' },
- })
- ).toThrow(
- /Cannot handle task function operation add message without task function/
- )
- })
+ it.each([{}, { taskFunction: 1 }])(
+ 'rejects an add message without a string task function (%j)',
+ taskFunctionFields => {
+ const worker = new ThreadWorker(() => {})
+ const handleAdd = () =>
+ worker.handleTaskFunctionOperationMessage({
+ ...taskFunctionFields,
+ taskFunctionOperation: 'add',
+ taskFunctionProperties: { name: 'fn' },
+ })
+
+ expect(handleAdd).toThrow(TypeError)
+ expect(handleAdd).toThrow(
+ /^Cannot handle task function operation add message without task function$/
+ )
+ }
+ )
})
describe('Check active mechanism', () => {