@avimate/msfs-jest-utils
v1.0.1
Published
Unit testing framework for MSFS instruments with DOM support via jsdom
Maintainers
Readme
MSFS Unit Test Framework
Unit testing framework for MSFS HTML/JS instruments (Jest + JSDOM).
Features
- ✅ Full DOM support - uses jsdom for component rendering
- ✅ FSComponent.render() - render components and test DOM elements
- ✅ SimVar and Coherent mocks - complete simulator API emulation
- ✅ Jest integration - ready-to-use testing utilities
- ✅ TypeScript support - full type safety
Installation
As a library (recommended)
npm install --save-dev @avimate/msfs-jest-utilsLocal development
npm install
npm run buildUsage
Basic Example
import { TestEnvironment, ComponentTestHelper } from '@avimate/msfs-jest-utils';
import { MyComponent } from '../html_ui/MyComponent';
describe('MyComponent', () => {
let env: TestEnvironment;
let helper: ComponentTestHelper;
beforeEach(() => {
env = new TestEnvironment();
env.setup();
helper = new ComponentTestHelper(env);
});
afterEach(() => {
helper.cleanup();
env.teardown();
});
test('should render component', () => {
const { element } = helper.renderComponent(MyComponent, {
value: 42
});
expect(element).toBeTruthy();
expect(element.textContent).toContain('42');
});
test('should update when SimVar changes', () => {
env.setSimVar('L:MY_VALUE', 'number', 10);
const { element } = helper.renderComponent(MyComponent, {
valueSource: env.getSubject('L:MY_VALUE', 'number')
});
expect(helper.getTextContent('.value')).toBe('10');
env.setSimVar('L:MY_VALUE', 'number', 20);
// Wait for subscription update
await helper.waitForUpdate(50);
expect(helper.getTextContent('.value')).toBe('20');
});
});Testing DOM Elements
test('should have correct CSS classes', () => {
const { element } = helper.renderComponent(MyComponent, {});
expect(helper.hasClass('.my-component', 'active')).toBe(true);
expect(helper.getAttribute('.my-component', 'data-state')).toBe('ready');
});
test('should apply styles correctly', () => {
const { element } = helper.renderComponent(MyComponent, {
width: 100,
height: 200
});
expect(helper.getStyle('.my-component', 'width')).toBe('100px');
});Working with SimVar
test('should read SimVar values', () => {
env.setSimVar('L:TEST_VALUE', 'number', 42);
const value = env.getSimVar('L:TEST_VALUE', 'number');
expect(value).toBe(42);
});
test('should track SimVar access', () => {
env.setSimVar('L:TEST', 'number', 10);
SimVar.GetSimVarValue('L:TEST', 'number');
const log = env.getSimVarAccessLog();
expect(log).toHaveLength(1);
expect(log[0].operation).toBe('get');
});API
TestEnvironment
setup()- initialize test environmentteardown()- cleanup after testsreset()- reset mockssetSimVar(name, unit, value)- set SimVar valuegetSimVar(name, unit)- get SimVar valuegetDocument()- get jsdom documentgetWindow()- get jsdom window
ComponentTestHelper
renderComponent(ComponentClass, props)- render componentquerySelector(selector)- find elementgetTextContent(selector)- get text contenthasClass(selector, className)- check classgetAttribute(selector, attrName)- get attributegetStyle(selector, property)- get stylewaitForUpdate(ms)- wait for updatecleanup()- cleanup
Structure
msfs-unit-test-framework/
├── src/
│ ├── mocks/
│ │ ├── SimVarMock.ts # SimVar API mock
│ │ ├── CoherentMock.ts # Coherent API mock
│ │ └── index.ts
│ ├── test-utils/
│ │ ├── TestEnvironment.ts # Environment setup
│ │ ├── ComponentTestHelper.ts # Component utilities
│ │ └── index.ts
│ ├── setupTests.ts # Jest setup
│ └── index.ts # Main export
├── tests/ # Test examples
├── jest.config.js
├── tsconfig.json
└── package.jsonExamples
See the tests/ folder for usage examples.
