Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
jest-svelte-events
Advanced tools
This library has peerDependencies
listings for svelte >= 3
.
npm install svelte-jester jest-svelte-events -D
Add the following to your Jest config
{
"setupFilesAfterEnv": [
"jest-svelte-events/extend-expect"
],
"transform": {
"^.+\\.svelte$": "svelte-jester"
},
"moduleFileExtensions": [
"js",
"svelte"
]
}
If you're using Babel then also add the following
npm install @babel/core @babel/preset-env babel-jest -D
Add the following to your Jest config
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.svelte$": "svelte-jester"
}
Create a .babelrc
and add the following
{
"presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]
}
listen
This is a global function called to setup any listeners on the component, you must call this before any matchers. Listeners are destroyed after each test block.
listen(component: SvelteComponent, event: string | string[])
import { render } from '@testing-library/svelte'
import MyComponent from './MyComponent.svelte'
test('', () => {
const { component } = render(MyComponent)
// If you're not using testing-library/svelte.
// const component = new MyComponent()
listen(component, 'myEvent')
// Multiple listeners
listen(component, ['eventOne', 'eventTwo'])
})
toHaveFiredEvent
Check whether a event has fired.
toHaveFiredEvent(event: string)
import { render } from '@testing-library/svelte'
import MyComponent from './MyComponent.svelte'
test('', () => {
const { component } = render(MyComponent)
listen(component, 'myEvent')
// ...
// code ...
// ...
expect(component).toHaveFiredEvent('myEvent')
})
toHaveFiredEvents
Check whether multiple events have fired.
toHaveFiredEvent(events: string[])
import { render } from '@testing-library/svelte'
import MyComponent from './MyComponent.svelte'
test('', () => {
const { component } = render(MyComponent)
listen(component, ['eventOne', 'eventTwo'])
// ...
// code ...
// ...
expect(component).toHaveFiredEvents(['eventOne', 'eventTwo'])
})
toHaveFiredEventsInOrder
Check whether all the events were fired in matching order.
toHaveFiredEventsInOrder(events: string[])
import { render } from '@testing-library/svelte'
import MyComponent from './MyComponent.svelte'
test('', () => {
const { component } = render(MyComponent)
listen(component, ['eventOne', 'eventTwo'])
// ...
// code ...
// ...
expect(component).toHaveFiredEventsInOrder(['eventTwo', 'eventOne', 'eventTwo'])
})
toHaveFiredEventTimes
Check whether a event was fired a set number of times.
toHaveFiredEventsInOrder(event: string, times: number)
import { render } from '@testing-library/svelte'
import MyComponent from './MyComponent.svelte'
test('', () => {
const { component } = render(MyComponent)
listen(component, 'myEvent')
// ...
// code ...
// ...
expect(component).toHaveFiredEventTimes('myEvent', 1)
})
toHaveFiredEventWith
Check whether a event was fired with a specific value.
toHaveFiredEventWith(event: string, payload: any)
import { render } from '@testing-library/svelte'
import MyComponent from './MyComponent.svelte'
test('', () => {
const { component } = render(MyComponent)
listen(component, 'myEvent')
// ...
// code ...
// ...
expect(component).toHaveFiredEventWith('myEvent', 100)
})
toHaveFiredLastEventWith
Check whether the last event was fired with a specific value.
toHaveFiredLastEventWith(payload: any)
import { render } from '@testing-library/svelte'
import MyComponent from './MyComponent.svelte'
test('', () => {
const { component } = render(MyComponent)
listen(component, ['eventOne', 'eventTwo', 'eventThree'])
// ...
// code ...
// ...
expect(component).toHaveFiredLastEventWith('end')
})
toHaveFiredNthEventWith
Check whether the nth event was fired with a specific value.
toHaveFiredNthEventWith(n: number, payload: any)
import { render } from '@testing-library/svelte'
import MyComponent from './MyComponent.svelte'
test('', () => {
const { component } = render(MyComponent)
listen(component, ['eventOne', 'eventTwo', 'eventThree'])
// ...
// code ...
// ...
expect(component).toHaveFiredNthEventWith(1, 'start')
})
All contributions are encouraged and welcome! If you have any ideas then just open an issue.
FAQs
Custom Jest matchers to test Svelte events
We found that jest-svelte-events demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.