Skip to content

Commit

Permalink
tests(types): add createEventBus compile time type checks test
Browse files Browse the repository at this point in the history
added expect-type which is jest like compile time type checks test framework
added coverage for createEventBus return type check to prevent type regression
in the futur
  • Loading branch information
bastienGranger committed Jul 26, 2022
1 parent 41ac9be commit cc074b8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@types/chrome": "^0.0.155",
"@types/jest": "^26.0.20",
"@types/ws": "^3.2.1",
"expect-type": "^0.13.0",
"husky": "^4.3.8",
"jest": "^28.1.3",
"npm-run-all": "^4.1.5",
Expand Down
32 changes: 32 additions & 0 deletions test/Types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expectTypeOf } from 'expect-type'
import { createEventBus } from '../src/Events'
import { Slot, slot } from '../src/Slot'

describe('createEventBus', () => {
type Events = {
numberToString: Slot<number, string>
eventToIgnore: Slot<void, void>
}

type FilteredEvents = {
numberToString: Slot<number, string>
}

const events: Events = {
numberToString: slot<number, string>(),
eventToIgnore: slot<void, void>(),
}

it('should return the right types regardless of an ingnoredEvents list being passed or not', () => {
const eventBus = createEventBus({ events })
const eventBusWithIgnored = createEventBus({
events,
ignoredEvents: ['eventToIgnore'],
})

expectTypeOf(eventBus).toEqualTypeOf<Events>()
expectTypeOf(eventBus).not.toEqualTypeOf<FilteredEvents>()
expectTypeOf(eventBusWithIgnored).not.toEqualTypeOf<Events>()
expectTypeOf(eventBusWithIgnored).toEqualTypeOf<FilteredEvents>()
})
})
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,11 @@ exit@^0.1.2:
resolved "https://registry.yarnpkg.com/exit/-/exit-0.1.2.tgz#0632638f8d877cc82107d30a0fff1a17cba1cd0c"
integrity sha1-BjJjj42HfMghB9MKD/8aF8uhzQw=

expect-type@^0.13.0:
version "0.13.0"
resolved "https://registry.yarnpkg.com/expect-type/-/expect-type-0.13.0.tgz#916646a7a73f3ee77039a634ee9035efe1876eb2"
integrity sha512-CclevazQfrqo8EvbLPmP7osnb1SZXkw47XPPvUUpeMz4HuGzDltE7CaIt3RLyT9UQrwVK/LDn+KVcC0hcgjgDg==

expect@^28.1.3:
version "28.1.3"
resolved "https://registry.yarnpkg.com/expect/-/expect-28.1.3.tgz#90a7c1a124f1824133dd4533cce2d2bdcb6603ec"
Expand Down

0 comments on commit cc074b8

Please sign in to comment.