Skip to main content

# dash-snapshot-testing
Use snapshot testing, inspired by Jest snapshot testing, to test [Dash][] components.

See the project [README](https://github.com/StratoDem/dash-snapshot-testing/blob/master/README.md) for more detailed information.

## Inspiration
Testing a long HTML component output for a Dash application is difficult.
It typically requires hardcoding data or setting up a dummy database.
Using snapshot tests that JSON serialize the Dash component output provide another
easy testing layer to ensure that code refactors/changes do not change the
output unexpectedly.

To learn more about snapshot testing in general, see a much more elaborate explanation from the [Facebook Jest site](https://facebook.github.io/jest/docs/en/snapshot-testing.html)

## Installation and usage
```bash
$ pip install dash-snapshot-testing
```

```python
import dash_html_components as html

from dash_snapshot_testing.snapshot_test import DashSnapshotTestCase


class MyUnitTestCase(DashSnapshotTestCase):
def test_component(self):
my_component = html.Div([html.P('wow'), html.Span('this works')], id='test-id')

self.assertSnapshotEqual(my_component, 'my-test-unique-id')
```

This outputs/checks this JSON at `__snapshots__/MyUnitTestCase-my-test-unique-id.json`:
```json
{
"type": "Div",
"props": {
"id": "test-id",
"children": [
{
"type": "P",
"props": {"children": "wow"},
"namespace": "dash_html_components"
},
{
"type": "Span",
"props": {"children": "this works"},
"namespace": "dash_html_components"
}
]
},
"namespace": "dash_html_components"
}
```

### Setting a custom `snapshots_dir` for the class
```python
class MyOtherUnitTestCase(DashSnapshotTestCase):
snapshots_dir = '__snapshots_2__'

def test_component(self):
my_component = html.Div([html.P('wow'), html.Span('another one')], id='test-id')

self.assertSnapshotEqual(my_component, 'my-test-unique-id')
```

This outputs/checks this JSON at `__snapshots_2__/MyOtherUnitTestCase-my-test-unique-id.json`:
```json
{
"type": "Div",
"props": {
"id": "test-id",
"children": [
{
"type": "P",
"props": {"children": "wow"},
"namespace": "dash_html_components"
},
{
"type": "Span",
"props": {"children": "another one"},
"namespace": "dash_html_components"
}
]
},
"namespace": "dash_html_components"
}
```

### Overwriting snapshots
To overwrite pre-existing snapshots, [like in Jest](https://facebook.github.io/jest/docs/en/snapshot-testing.html#updating-snapshots), set an environment variable as `UPDATE_DASH_SNAPSHOTS=TRUE`:
```bash
# This will run and make new snapshots
> UPDATE_DASH_SNAPSHOTS=TRUE python -m unittest my_test_module
# This will run against the previous snapshots
> python -m unittest my_test_module
```

### How this works
At its core, this `unittest.TestCase` compares a JSON-serialized Dash component
against a previously stored JSON-serialized Dash component, and checks if the `dict`
objects from `json.loads` are equivalent using `assertEqual`.

[Dash]: https://github.com/plotly/dash


Metadata

Release files for dash-snapshot-testing 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dash-snapshot-testing 1.2.2
File Size Uploaded Provenance
dash-snapshot-testing-1.2.2.tar.gz 4.2 kB Without provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for dash-snapshot-testing 1.2.2
File Interpreter ABI Platform
dash_snapshot_testing-1.2.2-py3-none-any.whl Python 3 none any Details
dash_snapshot_testing-1.2.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 17.7 kB

Release files / dash-snapshot-testing-1.2.2.tar.gz

Download URL dash-snapshot-testing-1.2.2.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
16118087b57aba66053fd98c493161bc50a75ffcf9d3fa4747d5bf9e36be3585
BLAKE2b-256 checksum
How to use checksums
7af23b0b0a7c35074be081f65c771d4e6568f14d7b0999208de3aa34e06a3de9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / dash_snapshot_testing-1.2.2-py3-none-any.whl

Download URL dash_snapshot_testing-1.2.2-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
243fa0ee62da8a6ec5de5f8a842c50f8943bfa5dc0994080053e2352984d2be0
BLAKE2b-256 checksum
How to use checksums
769394fbcdd706489df30937e7caf0f2b089f75d69f31bba3bb0e144a4e0c934
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / dash_snapshot_testing-1.2.2-py2.py3-none-any.whl

Download URL dash_snapshot_testing-1.2.2-py2.py3-none-any.whl
Size 6.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b8b3a4e7399ac85cd15a458b920a5f98f1ea2cbc00c844a410ad122226db9005
BLAKE2b-256 checksum
How to use checksums
6aa48adbf6a17893dbfc84a6c1f821f7ebd39cd4bb2f2cf03369fab1ca6354cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for dash-snapshot-testing 1.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page