Overview
LiveCodes includes a built-in test runner supporting:
Jest - JavaScript testing framework
Testing Library - React, Vue, and other framework testing
Custom test frameworks - Bring your own
Live results - See results as you type
Getting Started
Create Tests
Write tests in the Tests editor (separate from your main script)
Run Tests
Click the Tests tab in the tools pane
View Results
See pass/fail status with details
Tests run in isolation from your main code but can import and test your functions.
Jest Tests
Basic Test Structure
// In Tests editor
import { sum } from './script' ;
describe ( 'Math functions' , () => {
test ( 'adds 1 + 2 to equal 3' , () => {
expect ( sum ( 1 , 2 )). toBe ( 3 );
});
test ( 'adds negative numbers' , () => {
expect ( sum ( - 1 , - 2 )). toBe ( - 3 );
});
});
Test Functions
test / it
describe
beforeEach / afterEach
test ( 'description' , () => {
expect ( value ). toBe ( expected );
});
it ( 'also works' , () => {
expect ( value ). toBe ( expected );
});
describe ( 'Test Suite' , () => {
test ( 'test 1' , () => { /* ... */ });
test ( 'test 2' , () => { /* ... */ });
});
describe ( 'Tests with setup' , () => {
let data ;
beforeEach (() => {
data = { count: 0 };
});
afterEach (() => {
data = null ;
});
test ( 'uses setup data' , () => {
expect ( data . count ). toBe ( 0 );
});
});
Matchers
Equality
Truthiness
Numbers
Strings
Arrays
Objects
Exceptions
expect ( value ). toBe ( 4 ); // Strict equality (===)
expect ( obj ). toEqual ({ a: 1 }); // Deep equality
expect ( value ). not . toBe ( 5 ); // Negation
React Testing
Use Testing Library for React components:
Use React Template
Start with the Jest + React starter template
Write Component Tests
import { render , screen , fireEvent } from '@testing-library/react' ;
import App from './script' ;
test ( 'renders button' , () => {
render ( < App /> );
const button = screen . getByRole ( 'button' );
expect ( button ). toBeInTheDocument ();
});
test ( 'increments counter' , () => {
render ( < App /> );
const button = screen . getByText ( /click me/ i );
fireEvent . click ( button );
expect ( screen . getByText ( /clicked 1 times/ i )). toBeInTheDocument ();
});
React Testing Queries
screen . getByRole ( 'button' )
screen . getByText ( 'Submit' )
screen . getByLabelText ( 'Email' )
screen . getByPlaceholderText ( 'Enter name' )
screen . getByTestId ( 'custom-element' )
// Returns null if not found (no error)
const element = screen . queryByText ( 'Not here' );
expect ( element ). toBeNull ();
// Async - waits for element
const element = await screen . findByText ( 'Loaded' );
expect ( element ). toBeInTheDocument ();
User Interactions
import { fireEvent } from '@testing-library/react' ;
// Click
fireEvent . click ( button );
// Type
fireEvent . change ( input , { target: { value: 'Hello' } });
// Submit
fireEvent . submit ( form );
// Custom events
fireEvent . mouseEnter ( element );
fireEvent . keyDown ( element , { key: 'Enter' });
Async Testing
Promises
test ( 'async test' , async () => {
const data = await fetchData ();
expect ( data ). toBe ( 'success' );
});
// Or with .resolves/.rejects
test ( 'promise resolves' , () => {
return expect ( fetchData ()). resolves . toBe ( 'success' );
});
test ( 'promise rejects' , () => {
return expect ( failingFunction ()). rejects . toThrow ( 'error' );
});
Async Queries
import { waitFor , screen } from '@testing-library/react' ;
test ( 'waits for element' , async () => {
render ( < AsyncComponent /> );
await waitFor (() => {
expect ( screen . getByText ( 'Loaded' )). toBeInTheDocument ();
});
});
Test Runner Controls
Run Execute all tests Keyboard: Ctrl/Cmd + Alt + T
Watch Auto-run tests on code changes
Test Results
Result Display
Test results show:
Test name and description
Status : Pass (green), Fail (red), Skip (gray)
Error messages for failing tests
Stack traces for debugging
Summary : Total, passed, failed, skipped
Pass/Fail Indicators
Passing Test
Failing Test
Skipped Test
Green checkmark, no additional info ✗ adds 1 + 2 to equal 4
Expected: 4
Received: 3
Red X with error details
Skipping Tests
// Skip single test
test . skip ( 'not ready yet' , () => {
// Won't run
});
// Skip test suite
describe . skip ( 'Feature in progress' , () => {
test ( 'test 1' , () => { /* ... */ });
test ( 'test 2' , () => { /* ... */ });
});
// Run only specific tests
test . only ( 'run only this' , () => {
// Only this test runs
});
Importing Code Under Test
Access your script code:
// Import from your script editor
import { functionToTest , Component } from './script' ;
// Test imported code
test ( 'tests function' , () => {
expect ( functionToTest ()). toBe ( expected );
});
The tests editor has access to the script editor’s exports via ./script path.
Mocking
Mock Functions
const mockFn = jest . fn ();
// Use mock
mockFn ( 'arg' );
// Assertions
expect ( mockFn ). toHaveBeenCalled ();
expect ( mockFn ). toHaveBeenCalledWith ( 'arg' );
expect ( mockFn ). toHaveBeenCalledTimes ( 1 );
// Mock return value
mockFn . mockReturnValue ( 42 );
expect ( mockFn ()). toBe ( 42 );
// Mock implementation
mockFn . mockImplementation (( x ) => x * 2 );
expect ( mockFn ( 5 )). toBe ( 10 );
Mock Modules
jest . mock ( './api' , () => ({
fetchData: jest . fn (() => Promise . resolve ( 'mocked data' ))
}));
import { fetchData } from './api' ;
test ( 'uses mocked module' , async () => {
const data = await fetchData ();
expect ( data ). toBe ( 'mocked data' );
});
Coverage
Code coverage is not currently available in LiveCodes. Consider running tests locally with Jest for coverage reports.
Best Practices
Write Clear Test Names
// Good
test ( 'adds two positive numbers correctly' , () => { });
// Avoid
test ( 'test1' , () => { });
One Assertion Per Test
// Good
test ( 'returns correct length' , () => {
expect ( result . length ). toBe ( 3 );
});
test ( 'contains expected values' , () => {
expect ( result ). toContain ( 'value' );
});
Arrange-Act-Assert
test ( 'increments counter' , () => {
// Arrange
const counter = new Counter ();
// Act
counter . increment ();
// Assert
expect ( counter . value ). toBe ( 1 );
});
Test Edge Cases
test ( 'handles empty array' , () => {
expect ( sum ([])). toBe ( 0 );
});
test ( 'handles negative numbers' , () => {
expect ( sum ([ - 1 , - 2 ])). toBe ( - 3 );
});
Troubleshooting
Click the Run button in tests panel
Check for syntax errors in test code
Verify Jest template is being used
Enable watch mode for auto-run
Ensure you’re importing from './script'
Check that exports exist in script editor
Verify export syntax is correct
Increase timeout: test ( 'async test' , async () => {
// Test code
}, 10000 ); // 10 second timeout