jest-svelte-events

1.0.5 • Public • Published

jest-svelte-events

Custom Jest matchers to test Svelte events

version MIT License


Table of Contents

Installation

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"
  ]
}

Babel

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" } }]]
}

Usage

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(componentSvelteComponent, eventstring | 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(eventstring)
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(eventsstring[])
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(eventsstring[])
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(eventstring, timesnumber)
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(eventstring, payloadany)
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(payloadany)
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(nnumber, payloadany)
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')
})

Contributions

All contributions are encouraged and welcome! If you have any ideas then just open an issue.

LICENSE

MIT

Package Sidebar

Install

npm i jest-svelte-events

Weekly Downloads

4

Version

1.0.5

License

MIT

Unpacked Size

28.5 kB

Total Files

8

Last publish

Collaborators

  • mihar-22