refactor: code cleanups
[poolifier.git] / tests / worker / thread-worker.test.js
index 624e395c04f80cb752adc73f8e95d84b19300dab..072617d35a4e47d3e1c7c0912b754e2afe4bb0cc 100644 (file)
@@ -3,12 +3,13 @@ const { ThreadWorker } = require('../../lib')
 
 describe('Thread worker test suite', () => {
   let numberOfMessagesPosted = 0
-  const postMessage = function () {
+  const postMessage = () => {
     ++numberOfMessagesPosted
   }
   class SpyWorker extends ThreadWorker {
-    getMainWorker () {
-      return { postMessage }
+    constructor (fn) {
+      super(fn)
+      this.port = { postMessage }
     }
   }
 
@@ -17,7 +18,15 @@ describe('Thread worker test suite', () => {
     expect(worker.opts.maxInactiveTime).toStrictEqual(60000)
   })
 
-  it('Verify worker invoke the getMainWorker and postMessage methods', () => {
+  it('Verify that handleError() method is working properly', () => {
+    const error = new Error('Error as an error')
+    const worker = new ThreadWorker(() => {})
+    expect(worker.handleError(error)).toStrictEqual(error)
+    const errorMessage = 'Error as a string'
+    expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage)
+  })
+
+  it('Verify worker invokes the postMessage() method on port property', () => {
     const worker = new SpyWorker(() => {})
     worker.sendToMainWorker({ ok: 1 })
     expect(numberOfMessagesPosted).toBe(1)