Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-svelte-events

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-svelte-events

Custom Jest matchers to test Svelte events

  • 1.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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(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')
})

Contributions

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

LICENSE

MIT

Keywords

FAQs

Package last updated on 12 Dec 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc