Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit test example using Jest #1013

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/lit-dev-content/site/docs/tools/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,50 @@ Lit is a standard modern Javascript library, and you can use virtually any Javas

There are a few things you'll want to make sure your testing environment supports to effectively test your Lit code.

### Unit test example using Jest

```javascript
// simpeGreeting.js

import {html, css, LitElement} from 'lit';

export class SimpleGreeting extends LitElement {
static styles = css`p { color: blue }`;

static properties = {
name: {type: String},
};

constructor() {
super();
this.name = 'Somebody';
}

render() {
return html`<p>Hello, ${this.name}!</p>`;
}
}
customElements.define('simple-greeting', SimpleGreeting);
```

```javascript
// simpeGreeting.test.js

import 'simpleGreeting';

describe('SimpleGreeting', () => {
test('should render', async () => {
const element = document.createElement('simple-greeting');
element.setAttribute('name', 'world');
document.body.appendChild(element);
await element.updateComplete;

expect(element.shadowRoot.firstElementChild.textContent).toEqual('Hello, world!');
})
})

```

### Testing in the browser

Lit components are designed to run in the browser so testing should be conducted in a browser environment. Tools specifically focusing on testing [node](https://nodejs.org/) code may not be a good fit.
Expand Down