chore: migrate to eslint 9
[poolifier.git] / tests / worker / thread-worker.test.mjs
index 81b096f2b8b308812089cdebfb234e15954199ba..f78451f7d95482a33660076d71408c073661fc66 100644 (file)
@@ -1,22 +1,31 @@
 import { expect } from 'expect'
 import { restore, stub } from 'sinon'
-import { ThreadWorker } from '../../lib/index.js'
-import { DEFAULT_TASK_NAME } from '../../lib/utils.js'
+
+import { ThreadWorker } from '../../lib/index.cjs'
+import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs'
 
 describe('Thread worker test suite', () => {
   afterEach(() => {
     restore()
   })
 
+  it('Verify worker properties value after initialization', () => {
+    const worker = new ThreadWorker(() => {})
+    expect(worker.isMain).toBe(true)
+    expect(worker.mainWorker).toBe(null)
+    expect(worker.taskFunctions).toBeInstanceOf(Map)
+    expect(worker.taskFunctions.size).toBe(2)
+  })
+
   it('Verify that sync kill handler is called when worker is killed', () => {
     const worker = new ThreadWorker(() => {}, {
-      killHandler: stub().returns()
+      killHandler: stub().returns(),
     })
     worker.isMain = false
     worker.port = {
       postMessage: stub().returns(),
       unref: stub().returns(),
-      close: stub().returns()
+      close: stub().returns(),
     }
     worker.handleKillMessage()
     expect(worker.port.postMessage.calledOnce).toBe(true)
@@ -34,19 +43,25 @@ describe('Thread worker test suite', () => {
     }
     const worker = new ThreadWorker({ fn1, fn2 })
     worker.port = {
-      postMessage: stub().returns()
+      postMessage: stub().returns(),
     }
     expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({
       status: false,
-      error: new TypeError('name parameter is not a string')
+      error: new TypeError('name parameter is not a string'),
     })
     expect(worker.removeTaskFunction('', fn1)).toStrictEqual({
       status: false,
-      error: new TypeError('name parameter is an empty string')
+      error: new TypeError('name parameter is an empty string'),
+    })
+    expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
+      taskFunction: expect.any(Function),
+    })
+    expect(worker.taskFunctions.get('fn1')).toStrictEqual({
+      taskFunction: expect.any(Function),
+    })
+    expect(worker.taskFunctions.get('fn2')).toStrictEqual({
+      taskFunction: expect.any(Function),
     })
-    expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
-    expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
-    expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
     expect(worker.taskFunctions.size).toBe(3)
     expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
       worker.taskFunctions.get('fn1')
@@ -55,17 +70,21 @@ describe('Thread worker test suite', () => {
       status: false,
       error: new Error(
         'Cannot remove the task function with the default reserved name'
-      )
+      ),
     })
     expect(worker.removeTaskFunction('fn1')).toStrictEqual({
       status: false,
       error: new Error(
         'Cannot remove the task function used as the default task function'
-      )
+      ),
     })
     worker.removeTaskFunction('fn2')
-    expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
-    expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
+    expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
+      taskFunction: expect.any(Function),
+    })
+    expect(worker.taskFunctions.get('fn1')).toStrictEqual({
+      taskFunction: expect.any(Function),
+    })
     expect(worker.taskFunctions.get('fn2')).toBeUndefined()
     expect(worker.taskFunctions.size).toBe(2)
     expect(worker.port.postMessage.calledOnce).toBe(true)