npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cypress-svelte-unit-test

v3.3.4

Published

Unit testing Svelte components in Cypress E2E test runner

Downloads

925

Readme

cypress-svelte-unit-test CI circle image

renovate-app badge cypress version @bahmutov/cy-rollup version

Component testing for Svelte apps using the open source Cypress.io E2E test runner v4.5.0+

Keypad pin spec

Videos

I have recorded a series of short (3-4 minutes) videos showing Cypress + cypress-svelte-unit-test in action. See the playlist

Install

Requires Node version 8 or above and Cypress v4.5.0+

# Install this plugin and test spec preprocessor
npm install --save-dev cypress-svelte-unit-test
# if Cypress is not installed already
npx install --save-dev cypress
  1. Tell Cypress to use your rollup.config.js to bundle specs using cypress/plugins/index.js:
module.exports = (on) => {
  // @bahmutov/cy-rollup is already a dependency of cypress-svelte-unit-test
  const filePreprocessor = require('@bahmutov/cy-rollup')
  on('file:preprocessor', filePreprocessor())
}
  1. ⚠️ Turn the experimental component support on in your cypress.json. You can also specify where component spec files are located. For example, to have them located in src folder use:
{
  "experimentalComponentTesting": true,
  "componentFolder": "src",
  "testFiles": "**/*spec.js"
}

See cypress.json in this project.

  1. Write a test!
import HelloWorld from './HelloWorld.svelte'
import { mount } from 'cypress-svelte-unit-test'
it('shows greeting', () => {
  mount(HelloWorld, {
    props: {
      name: 'World',
    },
  })
  cy.contains('h1', 'Hello World!')
})

Watch Writing the first component test

Known issues

  • need to load images differently to transform relative paths

Code coverage

Instrument your code

See rollup.config.js how you can instrument source files. In short:

// npm i -D rollup-plugin-istanbul
import istanbul from 'rollup-plugin-istanbul'
plugins: [
  istanbul({
    include: ['cypress/components/**'],
    exclude: ['**/*spec.js'],
  }),
]

In Cypress iframe you should see the code coverage object under window.__coverage__.

Window coverage object

Coverage report

To merge coverage and generate reports we need to use @cypress/code-coverage plugin.

npm i -D @cypress/code-coverage

Add it to your cypress/support/index.js file

import '@cypress/code-coverage/support'

Add the plugin to your cypress/plugins/index.js file

module.exports = (on, config) => {
  const filePreprocessor = require('@bahmutov/cy-rollup')
  on('file:preprocessor', filePreprocessor())

  require('@cypress/code-coverage/task')(on, config)
  // IMPORTANT to return the config object
  // with the any changed environment variables
  return config
}

After the tests finish, you should see messages in the Command Log

Coverage messages

And find generated reports in coverage folder. For example, to open the HTML report

open coverage/lcov-report/index.html

Coverage report

Warning: I am not sure the coverage numbers are making 100% sense for Svelte files.

Svelte v3

This component adaptor is meant for Svelte v3. If you need Svelte v2 support, check out branch svelte-v2

Use

Import your Svelte component and mount using the provided function. Pass component options and global document options (like a global CSS)

Props

/// <reference types="cypress" />
import App from '../components/ChainedBalls.svelte'
import { mount } from 'cypress-svelte-unit-test'

describe('SVG animation', () => {
  it('shows chained balls', () => {
    cy.viewport(960, 500)
    const style = `
      line {
        stroke: gray;
        stroke-width: 2px;
      }
    `
    mount(
      App,
      {
        props: {
          width: 960,
          height: 500,
        },
      },
      { style },
    )
    cy.get('circle').should('have.length', 50)
  })
})

Watch Pass props to the component

Styles

You can use local styles, local CSS file path (relative to the the Cypress project root) or external stylesheets. See styles example. You can surround the component with HTML and mount the component into the element with ID "here", see cypress/components/mount-html

const props = {...}
mount(HelloWorld, props, {
  html: `
    <div class="test-page">
      this is a test
      <div id="here"></div>
      this is after component
    </div>
  `,
  style: `
    body {
      background: pink
    }
    .test-page {
      background: cyan
    }
    #here {
      background: yellow
    }
  `,
})

Mount HTML example

Watch Style component during testing

Callbacks

You can listen for messages from the component by supplying an object of callbacks.

mount(TodoItem, {
  props: {
    id: 'todo-id',
    text: 'write a test',
    complete: false,
  },
  callbacks: {
    remove: cy.stub().as('remove'),
    toggle: cy.stub().as('toggle'),
    'inner-message': cy.stub().as('inner-message'),
  },
})

See cypress/components/callbacks.

Watch Testing message dispatch

Examples

Basic examples

Svelte components copied from https://svelte.dev/examples

All components and tests are in cypress/components folder

Test | Description --- | --- animation | Chained balls SVG animation callbacks | Listen for messages dispatched from the component global-handlers | Attaches event listeners to document and window hello | Hello, component testing! image | Loading Rick-Roll image named-exports | Nice Audio player test nested | Checking nested components and local styles pin | Keypad pin test reactive | Svelte reactive props, declarations and statements rx | Fetching GitHub users as a reactive stream styles | Shows inline, CSS and external stylesheet styles in spec tutorial | A few components and tests from Svelte tutorial mocking-fetch | Mocking window.fetch before mounting the component mocking-network | Polyfills window.fetch automatically and tests the component

External examples

You can find larger Svelte example application with component tests under GitHub topic cypress-svelte-unit-test-example

Name | Description --- | --- svelte-ts-example | Writing Svelte components and tests using TypeScript

Related tools

Same feature for unit testing components from other framesworks using Cypress

Small print

Author: Gleb Bahmutov <[email protected]> © 2018

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2018 Gleb Bahmutov <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.