1 import { expect } from 'expect'
2 import { restore, stub } from 'sinon'
4 import { ThreadWorker } from '../../lib/index.cjs'
5 import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs'
7 describe('Thread worker test suite', () => {
12 it('Verify worker properties value after initialization', () => {
13 const worker = new ThreadWorker(() => {})
14 expect(worker.isMain).toBe(true)
15 expect(worker.mainWorker).toBe(null)
16 expect(worker.taskFunctions).toBeInstanceOf(Map)
17 expect(worker.taskFunctions.size).toBe(2)
20 it('Verify that sync kill handler is called when worker is killed', () => {
21 const worker = new ThreadWorker(() => {}, {
22 killHandler: stub().returns()
26 postMessage: stub().returns(),
27 unref: stub().returns(),
28 close: stub().returns()
30 worker.handleKillMessage()
31 expect(worker.port.postMessage.calledOnce).toBe(true)
32 expect(worker.port.unref.calledOnce).toBe(true)
33 expect(worker.port.close.calledOnce).toBe(true)
34 expect(worker.opts.killHandler.calledOnce).toBe(true)
37 it('Verify that removeTaskFunction() is working', () => {
44 const worker = new ThreadWorker({ fn1, fn2 })
46 postMessage: stub().returns()
48 expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({
50 error: new TypeError('name parameter is not a string')
52 expect(worker.removeTaskFunction('', fn1)).toStrictEqual({
54 error: new TypeError('name parameter is an empty string')
56 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
57 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
58 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object)
59 expect(worker.taskFunctions.size).toBe(3)
60 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
61 worker.taskFunctions.get('fn1')
63 expect(worker.removeTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
66 'Cannot remove the task function with the default reserved name'
69 expect(worker.removeTaskFunction('fn1')).toStrictEqual({
72 'Cannot remove the task function used as the default task function'
75 worker.removeTaskFunction('fn2')
76 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
77 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
78 expect(worker.taskFunctions.get('fn2')).toBeUndefined()
79 expect(worker.taskFunctions.size).toBe(2)
80 expect(worker.port.postMessage.calledOnce).toBe(true)
83 it('Verify that handleError() method is working properly', () => {
84 const error = new Error('Error as an error')
85 const worker = new ThreadWorker(() => {})
86 expect(worker.handleError(error)).toBeInstanceOf(Error)
87 expect(worker.handleError(error)).toStrictEqual(error)
88 const errorMessage = 'Error as a string'
89 expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage)
92 it('Verify that sendToMainWorker() method invokes the port property postMessage() method', () => {
93 const worker = new ThreadWorker(() => {})
94 worker.port = { postMessage: stub().returns() }
95 worker.sendToMainWorker({ ok: 1 })
96 expect(worker.port.postMessage.calledOnce).toBe(true)