• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language
    JavaScript
  • Created over 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Painless testing for Svelte components

Dainte CI

Painless testing for Svelte components, inspired by Enzyme.

🥂  Test Svelte runtime and SSR simultanously
🎭  Zero-config compatible with Jest
🤖  Low-level compile options to ensure that tests match prod
🔎  Component "state" introspection

Usage

dainte.compile

result: {
    // Compiled Svelte component class
    Component,

    // Alias to component as specified or inferred name
    [name],
} = await dainte.compile(source: string, options?: {...})

Creates a compiled Svelte component class from a source file path.

The following options can be passed to compile, including svelte.compile options. The dev option defaults to true for testing. None are required:

option default description
name 'Component' Name of the component class, inferred from filename
dev true Perform runtime checks and provide debugging information
immutable false You promise not to mutate any objects
hydratable false Enables the hydrate: true runtime option
legacy false Generates code that will work in IE9 and IE10
accessors false Getters and setters will be created for the component's props
css true Include CSS styles in JS class
generate 'dom' Create JS DOM class or object with .render()
inspect false Include instance.inspect() accessor
plugins [svelte(), resolve()] Advanced option to manually specify Rollup plugins for bundling.

Example

import { compile } from 'dainte'

const { App } = await compile('./App.svelte')

const app = new App({
    target: document.body,
})

dainte.mount

result: {
    // Svelte component instance
    instance,

    // Compiled JS component class
    Component,

    // JSDom window and document where component is mounted.
    window,
    document,

    // Alias to the Component with specified or inferred name
    [name],

    // Alias to the instance with lowercase specified or inferred name
    [lowercase(name)],
}  = await mount(source: string, options?: {...})

Creates an instance of a component from a source file path. Mounts the instance in a JSDom.

All compile options can also be passed to mount. Additionally, these options, including the component initialisation options, can be provided:

option default description
html '<body></body>' HTML to initiate the JSDom instance with
target 'body' Render target (as a query selector, not a DOM element as in Svelte initialisation)
anchor null Render anchor (as a query selector, not a DOM element as in Svelte initialisation)
props {} An object of properties to supply to the component
hydrate false Upgrade existing DOM instead of replacing it
intro false Play transitions on initial render

A svelte.tick is awaited between mounting the instance and resolving the mount promise so that the DOM is full initialized. An additional svelte.tick should be awaited between updating the component and reading from the DOM.

Example

import { mount } from 'dainte'
import { tick } from 'svelte'

const { app, document } = await mount('./App.svelte')
app.$set({ answer: 42 })
await tick()

expect(document.querySelector('#answer').textContent).toBe('42')

dainte.render

result: {
    head,
    html,
    css,
} = await dainte.render(source: string, options?: {...})

Wraps Svelte's server-side Component.render API for rendering a component to HTML.

The following options can be passed to render, including svelte.compile options. The dev option defaults to true for testing. None are required:

option default description
dev true Perform runtime checks and provide debugging information
immutable false You promise not to mutate any objects
hydratable false Enables the hydrate: true runtime option
css true Include CSS styles in JS class
preserveComments false HTML comments will be preserved
preserveWhitespace false Keep whitespace inside and between elements as you typed it
plugins [svelte(), resolve()] Advanced option to manually specify Rollup plugins for bundling.

Example

import { render } from 'dainte'

const { html } = await render('./App.svelte')
// '<div id="answer">42</div>'

instance.inspect

variables: {
    // Snapshot of all top-level variables and imports
} = instance.inspect()

Compiling with inspect: true adds a inspect() function to the component instance. Calling the function returns a snapshot object of all top-level variables and their current values. Snapshot values are not reactive and inspect() must be called again to retrieve updated values.

Example

import { mount } from 'dainte'

const { app } = await mount('./App.svelte', { inspect: true })
const { answer } = app.inspect()
// 42

More Repositories

1

split

Unopinionated utilities for resizeable split views
JavaScript
6,091
star
2

Anycomplete

The magic of Google Autocomplete while you're typing. Anywhere.
Lua
1,539
star
3

mimicdb

Python implementation of MimicDB
Python
234
star
4

sveltik

Powerful forms in Svelte, inspired by Formik.
JavaScript
192
star
5

table-edits

A lightweight jQuery plugin for making table rows editable
JavaScript
59
star
6

Flask-Edits

Editable Content in Flask
CSS
54
star
7

flask-inputs

Incoming request data validation
Python
53
star
8

CleanMaps

Cleaner Google Maps
JavaScript
52
star
9

skeleton-tabs

Tab navigation for Skeleton
HTML
31
star
10

pipeup

Unix Pipes to the Web
HTML
24
star
11

snippets

Python
22
star
12

ImageJS

Simple library for loading images in Javascript
JavaScript
17
star
13

rollup-plugin-collect-sass

Collect Sass, then compile
JavaScript
16
star
14

prioritize-url

Prioritize URLs in Firefox autocomplete dropdown
CSS
15
star
15

aws-serverless-micro

Deploy Micro functions on AWS Lambda
JavaScript
13
star
16

skeleton-alerts

Alerts for Skeleton
CSS
12
star
17

github-time-travel

Easily browse commit history on Github
JavaScript
11
star
18

gittip-button

Gittip Button for Github
JavaScript
11
star
19

video-freqtimeupdate

Frequent Time Update for HTML5 video elements
JavaScript
8
star
20

Flask-Logging

Filter requests from Flask logs
Python
8
star
21

tile-proxy-bing

Cloudflare Worker proxy for consuming Bing Maps tiles as slippy tiles.
JavaScript
8
star
22

docker-firefox-headless

JavaScript
7
star
23

eminent

A DOM assertions library with Emmet syntax for JavaScript testing
JavaScript
6
star
24

flask-oauth-example

Barebones example of integrating an OAuth login system with a Flask app
Python
6
star
25

skeleton-fontawesome-buttons

Font Awesome icon buttons for Skeleton
CSS
6
star
26

fuck-w3schools

Fuck W3Schools
HTML
5
star
27

werkzeug-raw

Werkzeug meets Raw HTTP
Python
5
star
28

pikaday-skeleton

The refreshing JavaScript Datepicker, styled for Skeleton
CSS
5
star
29

applescripts

Objective-C
4
star
30

python-growl

Improved Python implementation of the Growl (GNTP) protocol.
Python
4
star
31

gif2fb

Post gifs to Facebook
Python
4
star
32

shp-postgis-schema

Creates a Postgres table from an shapefile
Python
3
star
33

clearbit-intercom

Clearbit - Intercom.io Integration
Python
3
star
34

Pikaday2

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS
JavaScript
2
star
35

reply-now

Change Gmail timestamps to display relative times.
JavaScript
2
star
36

wkb-raster

Read WKB rasters to Numpy
Python
2
star
37

duo-cli

Python
2
star
38

wkb

Read WKB rasters to Go data structures
Go
2
star
39

rollup-multiple

Obsolute, merged to Rollup core
JavaScript
2
star
40

splitjs-bower

Bower repo for backwards compatibility
JavaScript
1
star
41

vercel-sns-integration

Subscribe Vercel deployments to AWS SNS
Svelte
1
star
42

nathancahill.github.com

JavaScript
1
star
43

create-rollup-app

Create Rollup React apps with no build configuration.
1
star
44

now-postgrest

Deploy PostgREST to Now
TypeScript
1
star
45

dotfiles

...
Shell
1
star
46

stackoverflow-header

Brings back the gray header bar in StackOverflow
CSS
1
star
47

test

HTML
1
star
48

scripts

Perl
1
star
49

pipeup-hosted

JavaScript
1
star