From: Jérôme Benoit Date: Mon, 2 Oct 2023 19:01:01 +0000 (+0200) Subject: test: use sinon stub to simplify code X-Git-Tag: v2.7.5~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=bb3d0b23ab89cf90ac2977403d74bb755f4f6b07;p=poolifier.git test: use sinon stub to simplify code Signed-off-by: Jérôme Benoit --- diff --git a/tests/worker/cluster-worker.test.mjs b/tests/worker/cluster-worker.test.mjs index f6a927aa..9349743c 100644 --- a/tests/worker/cluster-worker.test.mjs +++ b/tests/worker/cluster-worker.test.mjs @@ -1,17 +1,19 @@ 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(() => {}) @@ -24,6 +26,6 @@ describe('Cluster worker test suite', () => { 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) }) }) diff --git a/tests/worker/thread-worker.test.mjs b/tests/worker/thread-worker.test.mjs index 4be262a2..6121995b 100644 --- a/tests/worker/thread-worker.test.mjs +++ b/tests/worker/thread-worker.test.mjs @@ -1,18 +1,19 @@ 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(() => {}) @@ -25,6 +26,6 @@ describe('Thread worker test suite', () => { 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) }) })