fix: add types fileds to package.json exports
[poolifier.git] / tests / worker / thread-worker.test.js
CommitLineData
a61a0724 1const { expect } = require('expect')
fd027a65
S
2const { ThreadWorker } = require('../../lib')
3
e1ffb94f
JB
4describe('Thread worker test suite', () => {
5 let numberOfMessagesPosted = 0
440dd7d7 6 const postMessage = () => {
ffcbbad8 7 ++numberOfMessagesPosted
e1ffb94f
JB
8 }
9 class SpyWorker extends ThreadWorker {
85aeb3f3
JB
10 constructor (fn) {
11 super(fn)
12 this.port = { postMessage }
e1ffb94f 13 }
74a2a194 14 }
74a2a194 15
fd027a65
S
16 it('Verify worker has default maxInactiveTime', () => {
17 const worker = new ThreadWorker(() => {})
e1ffb94f 18 expect(worker.opts.maxInactiveTime).toStrictEqual(60000)
fd027a65 19 })
74a2a194 20
2431bdb4
JB
21 it('Verify that handleError() method is working properly', () => {
22 const error = new Error('Error as an error')
23 const worker = new ThreadWorker(() => {})
24 expect(worker.handleError(error)).toStrictEqual(error)
25 const errorMessage = 'Error as a string'
26 expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage)
27 })
28
85aeb3f3 29 it('Verify worker invokes the postMessage() method on port property', () => {
74a2a194
APA
30 const worker = new SpyWorker(() => {})
31 worker.sendToMainWorker({ ok: 1 })
32 expect(numberOfMessagesPosted).toBe(1)
33 })
fd027a65 34})