项目作者: nathancahill

项目描述 :
Painless testing for Svelte components
高级语言: JavaScript
项目地址: git://github.com/nathancahill/dainte.git
创建时间: 2020-02-19T09:23:29Z
项目社区:https://github.com/nathancahill/dainte

开源协议:

下载


" class="reference-link">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

  1. result: {
  2. // Compiled Svelte component class
  3. Component,
  4. // Alias to component as specified or inferred name
  5. [name],
  6. } = 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

  1. import { compile } from 'dainte'
  2. const { App } = await compile('./App.svelte')
  3. const app = new App({
  4. target: document.body,
  5. })

dainte.mount

  1. result: {
  2. // Svelte component instance
  3. instance,
  4. // Compiled JS component class
  5. Component,
  6. // JSDom window and document where component is mounted.
  7. window,
  8. document,
  9. // Alias to the Component with specified or inferred name
  10. [name],
  11. // Alias to the instance with lowercase specified or inferred name
  12. [lowercase(name)],
  13. } = 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

  1. import { mount } from 'dainte'
  2. import { tick } from 'svelte'
  3. const { app, document } = await mount('./App.svelte')
  4. app.$set({ answer: 42 })
  5. await tick()
  6. expect(document.querySelector('#answer').textContent).toBe('42')

dainte.render

  1. result: {
  2. head,
  3. html,
  4. css,
  5. } = 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

  1. import { render } from 'dainte'
  2. const { html } = await render('./App.svelte')
  3. // '<div id="answer">42</div>'

instance.inspect

  1. variables: {
  2. // Snapshot of all top-level variables and imports
  3. } = 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

  1. import { mount } from 'dainte'
  2. const { app } = await mount('./App.svelte', { inspect: true })
  3. const { answer } = app.inspect()
  4. // 42