https://github.com/evanoc3/jest-test-custom-events
https://github.com/evanoc3/jest-test-custom-events
custom-events jest unit-testing
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/evanoc3/jest-test-custom-events
- Owner: evanoc3
- License: mit
- Created: 2025-03-27T20:42:05.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-27T20:51:17.000Z (over 1 year ago)
- Last Synced: 2025-10-23T15:00:05.665Z (8 months ago)
- Topics: custom-events, jest, unit-testing
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/jest-test-custom-events
- Size: 37.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Jest Test Custom Events
An NPM package which adds a custom matcher, `toMatchCustomEvent` to [Jest](https://jestjs.io/) allowing for the testing of properties of [CustomEvents](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent).
## Installation
```sh
npm install -D jest-test-custom-events
```
## Example Usage
```typescript
import "jest-test-custom-events";
test("example test", () => {
// create a CustomEvent
const e = new CustomEvent("custom-event-name", {
bubbles: true,
composed: true,
detail: {
exampleProperty: true
}
});
// passes
expect(e).toMatchCustomEvent({
type: "custom-event-name",
bubbles: true,
composed: true,
detail: {
exampleProperty: true
}
});
// fails – the value for 'bubbles' does not match the received event
expect(e).toMatchCustomEvent({
bubbles: false
});
});
```
Counterintuitively, you cannot do this using the built-in `isEqual` matcher because `CustomEvent` is implemented in such a way as to make it's properties non-enumerable, and hence wil almost always pass deep equality checks.