import { expect } from 'expect'
+import { restore, stub } from 'sinon'
import { ClusterWorker } from '../../lib/index.js'
describe('Cluster worker test suite', () => {
- let numberOfMessagesSent = 0
- const send = () => {
- ++numberOfMessagesSent
- }
+ const sendStub = stub().returns()
class SpyWorker extends ClusterWorker {
getMainWorker () {
- return { send }
+ return { send: sendStub }
}
}
+ afterEach(() => {
+ restore()
+ })
+
it('Verify that handleError() method is working properly', () => {
const error = new Error('Error as an error')
const worker = new ClusterWorker(() => {})
it('Verify worker invokes the getMainWorker() and send() methods', () => {
const worker = new SpyWorker(() => {})
worker.sendToMainWorker({ ok: 1 })
- expect(numberOfMessagesSent).toBe(1)
+ expect(worker.getMainWorker().send.calledOnce).toBe(true)
})
})
import { expect } from 'expect'
+import { restore, stub } from 'sinon'
import { ThreadWorker } from '../../lib/index.js'
describe('Thread worker test suite', () => {
- let numberOfMessagesPosted = 0
- const postMessage = () => {
- ++numberOfMessagesPosted
- }
class SpyWorker extends ThreadWorker {
constructor (fn) {
super(fn)
- this.port = { postMessage }
+ this.port = { postMessage: stub().returns() }
}
}
+ afterEach(() => {
+ restore()
+ })
+
it('Verify that handleError() method is working properly', () => {
const error = new Error('Error as an error')
const worker = new ThreadWorker(() => {})
it('Verify worker invokes the postMessage() method on port property', () => {
const worker = new SpyWorker(() => {})
worker.sendToMainWorker({ ok: 1 })
- expect(numberOfMessagesPosted).toBe(1)
+ expect(worker.port.postMessage.calledOnce).toBe(true)
})
})