Skip to content

Commit

Permalink
test: create utility to flush all internal promises
Browse files Browse the repository at this point in the history
  • Loading branch information
fellowseb committed Feb 26, 2024
1 parent 973d63b commit 4de9585
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
3 changes: 2 additions & 1 deletion test/Event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { slot } from './../src/Slot'
import { combineEvents, createEventBus } from './../src/Events'
import { TestChannel } from './TestChannel'
import { DEFAULT_PARAM } from './../src/Constants'
import { flushPromises } from './testing-utils';

describe('combineEvents()', () => {

Expand Down Expand Up @@ -93,7 +94,7 @@ describe('createEventBus()', () => {
data: 5
})

await Promise.resolve() // yied to ts-event-bus internals
await flushPromises();
expect(channel.sendSpy).toHaveBeenCalledWith({
type: 'response',
slotName: 'numberToString',
Expand Down
5 changes: 2 additions & 3 deletions test/Slot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TestChannel } from './TestChannel'
import { Transport } from './../src/Transport'
import { DEFAULT_PARAM } from './../src/Constants'
import { TransportRegistrationMessage } from './../src/Message'
import { flushPromises } from './testing-utils';

const makeTestTransport = () => {
const channel = new TestChannel()
Expand Down Expand Up @@ -145,9 +146,7 @@ describe('connectSlot', () => {
type: 'handler_registered'
})

// setTimeout(0) to yield control to ts-event-bus internals,
// so that the call to handlers can be processed
await new Promise(resolve => setTimeout(resolve, 0))
await flushPromises();

// Once a remote handler is registered, both local and remote should be called
expect(localCalled).toEqual(true)
Expand Down
5 changes: 3 additions & 2 deletions test/Transport.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Transport } from './../src/Transport'
import { TransportMessage } from './../src/Message'
import { TestChannel } from './TestChannel'
import { flushPromises } from './testing-utils';

const param = 'param'

Expand Down Expand Up @@ -89,7 +90,7 @@ describe('Transport', () => {

channel.fakeReceive(request)

await Promise.resolve() // yield to ts-event-bus internals
await flushPromises();
expect(handler).toHaveBeenCalledWith(request.data)
expect(channel.sendSpy).toHaveBeenLastCalledWith({
slotName,
Expand Down Expand Up @@ -141,7 +142,7 @@ describe('Transport', () => {
}
}
channel.fakeReceive(request)
await Promise.resolve() // yield to ts-event-bus internals
await flushPromises();
expect(handler).not.toHaveBeenCalled()
})

Expand Down
6 changes: 6 additions & 0 deletions test/testing-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Utility to flush current promises during a test when using fake timers
*/
export function flushPromises(): Promise<void> {
return new Promise(jest.requireActual('timers').setImmediate);
}

0 comments on commit 4de9585

Please sign in to comment.