| name | livecodes/testing |
| description | Write and run automated tests with Jest and Testing Library in the browser. Import code from script editor, use watch mode, and run tests programmatically. Load this skill when writing tests for playground code or running tests via SDK.
|
| type | core |
| library | livecodes |
| library_version | 0.13.0 |
| sources | ["live-codes/livecodes:docs/docs/features/tests.mdx"] |
LiveCodes — Write and Run Tests
LiveCodes includes Jest and Testing Library for automated testing in the browser. Tests run against the result page in real-time.
Setup
import { createPlayground } from 'livecodes';
createPlayground('#container', {
template: 'jest',
params: { tests: 'open' },
});
createPlayground('#container', {
config: {
activeEditor: 'script',
script: {
language: 'javascript',
content: `
export function add(a, b) {
return a + b;
}
export function greet(name) {
return 'Hello, ' + name;
}
`,
},
tests: {
language: 'javascript',
content: `
import { add, greet } from './script';
describe('add', () => {
test('adds numbers', () => {
expect(add(1, 2)).toBe(3);
});
test('handles negatives', () => {
expect(add(-1, -2)).toBe(-3);
});
});
describe('greet', () => {
test('greets by name', () => {
expect(greet('World')).toBe('Hello, World');
});
});
`,
},
},
params: { tests: 'open' },
});
Core Patterns
Test exports from script editor
export default function greet(name) {
return 'Hello, ' + name;
}
export const add = (a, b) => a + b;
window.multiply = (a, b) => a * b;
import greet, { add } from './script';
describe('imports', () => {
test('greet', () => {
expect(greet('World')).toBe('Hello, World');
});
test('add', () => {
expect(add(1, 2)).toBe(3);
});
});
describe('globals', () => {
test('multiply', () => {
expect(window.multiply(2, 3)).toBe(6);
});
});
Use Testing Library
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import userEvent from '@testing-library/user-event';
test('button click', async () => {
render(<MyComponent />);
await userEvent.click(screen.getByText('Click me'));
expect(screen.getByText('Clicked')).toBeInTheDocument();
});
import { getByText, waitFor } from '@testing-library/dom';
test('element appears', async () => {
document.body.innerHTML = '<div id="root"></div>';
await waitFor(() => {
expect(getByText(document.body, 'Loaded')).toBeTruthy();
});
});
Run tests programmatically via SDK
const playground = await createPlayground('#container', {
config: {
},
});
const { results } = await playground.runTests();
results.forEach((result) => {
console.log(result.status);
console.log(result.testPath);
console.log(result.duration);
console.log(result.errors);
});
playground.watch('tests', ({ results, error }) => {
if (error) {
console.error('Test error:', error);
return;
}
const passed = results.filter((r) => r.status === 'pass');
console.log(`${passed.length} tests passed`);
});
Use Jest globals directly
describe('my tests', () => {
test('passes', () => {
expect(1 + 1).toBe(2);
});
test('asynchronous', async () => {
const result = await someAsyncFunction();
expect(result).toBe('expected');
});
test.skip('skipped', () => {
});
test.only('run only this', () => {
});
});
beforeAll(() => {
});
afterAll(() => {
});
beforeEach(() => {
});
afterEach(() => {
});
jest.fn();
jest.mocked(obj);
jest.spyOn(obj, 'method');
Use Chai assertions (alternative)
import { assert } from 'chai';
describe('with Chai', () => {
test('assertions', () => {
assert.equal(1 + 1, 2);
assert.isArray([1, 2, 3]);
assert.include([1, 2, 3], 2);
});
});
Test Panel Controls
In the standalone app or visible tests panel:
- Run button: Run tests (shortcut: Ctrl/Cmd + Alt + T)
- Watch toggle: Auto-run tests on code change
- Reset button: Clear test results
- Edit button: Open test editor (not available in embeds)
Common Mistakes
HIGH Script errors prevent tests from loading
Wrong:
export function add(a, b) {
return a +
}
import { add } from './script';
test('add', () => expect(add(1, 2)).toBe(3));
Understanding:
Tests are added to the result page. Script errors (import errors, syntax errors) prevent the result page from loading, which prevents tests from running.
Source: docs/docs/features/tests.mdx — Example section
MEDIUM Import path must be relative without extension
Wrong:
import { add } from 'script';
import { add } from './script.js';
Correct:
import { add } from './script';
When importing from the script editor in tests, use relative imports without file extensions.
Source: docs/docs/features/tests.mdx — Importing Code section
Supported Jest Features
| Feature | Support |
|---|
describe, test, it | Full support |
xtest, xit, fit | Skip/only tests |
xdescribe, fdescribe | Skip/only suites |
beforeAll, afterAll | Lifecycle hooks |
beforeEach, afterEach | Lifecycle hooks |
expect | Full matchers |
jest.fn(), jest.mocked() | Function mocks |
jest.spyOn() | Spy on methods |
jest.replaceProperty() | Property replacement |
Supported Testing Libraries
| Library | Import |
|---|
| DOM Testing Library | import { getByText, waitFor } from '@testing-library/dom' |
| React Testing Library | import { render, screen } from '@testing-library/react' |
| jest-dom | import '@testing-library/jest-dom' |
| user-event | import userEvent from '@testing-library/user-event' |
| Chai | import { assert } from 'chai' |
Tests run in the browser using Jest. Libraries are automatically available — no import maps needed for testing libraries.