For component library to be stable, it must be testable and tested
-
Tests are running with
jest
. -
Tests in this API level are ones that require browser-like environment but can still run without any visual rendering. The nature of these tests is testing the behavior of a component and wiring methods. For example: clicking on a component triggers a callback, changing the input value, etc...
-
Every component has test file with
spec.js
extension, for example:ComponentName.spec.js
. -
Every component has a driver (read about them here). Naming convention is
ComponentName.driver.js
import React from 'react';
import checkboxDriverFactory from './Checkbox.driver';
import {createDriverFactory} from 'wix-ui-test-utils/driver-factory';
describe('Checkbox', () => {
const createDriver = createDriverFactory(checkboxDriverFactory);
it('should be unchecked and not disabled by default', () => {
const driver = createDriver(<Checkbox/>);
expect(driver.isChecked()).toBeFalsy();
expect(driver.isDisabled()).toBeFalsy();
});
});
- Visual Testing: Visual regression tests are done with
eyes
(powered by applitools). - Browser API: Anything that needs real browser API (e.g. position calculations, hovering, styling).
Tests run with protractor
which uses chrome browser.
- Test File: Every component has test file with
e2e.js
extension, for example:ComponentName.e2e.js
. - Driver File: Every component has a driver . Legacy driver name convention is
ComponentName.protractor.driver.js
, but the best-practice is to write a UniDriver. (read about them here)
-
Every test uses
eyes.it()
to automatically capture screenshots at the beginning and end of test. (Seeeyes.it
](https://github.com/wix/eyes.it) for full API) -
Use
eyes.checkWindow()
to capture a screenshot explicitly.
import {eyesItInstance} from '../../test/utils/eyes-it';
const eyes = new eyesItInstance();
eyes.it('should test something with screenshot diff', async () => {
expect(await assert).toEqual(expectation);
});
eyes.it('should test something with a screenshot on demand', async () => {
// do some manipulation, for example scroll
await eyes.checkWindow('after scrolling');
// do other manipulations
});
- AutoExample Story: Every component has corresponding documentation story (e.g.
stories/ComponentName/index.story.js
) in the Storybook. You may run the e2e test against the story's AutoExample (aka Playground), or the story examples. - Test Stories: You may create a dedicated test story page and add it to the
Tests
category in the storybook. Use:
import {getTestStoryKind} from '../storyHierarchy';
const kind = getTestStoryKind({category: 'Layout', storyName: 'Cell'});
storiesOf(kind, module)
.add('1. Test Page #1', () =>
<div>
My Test Page
</div>
);
npm run build && npm run test
-
single run:
npm run test:unit
-
watch mode:
npm run test:watch
-
watch mode + storybook:
npm start
- In watch mode, you can use
jest
's interactive mode, for example, pressp
in your command line and type the name of the test:
npm run build && npm run test:e2e
npm run build
createsstorybook-static
foldernpm run test:e2e
serves the storybook fromstorybook-static
folder.- Changing tests doesn't require rebuilding.
npm run storybook
- start storybook server with hot module reload- open another terminal console
npm run test:e2e-only
- run protractor tests
- To make a focused test (only it runs) use
fit
instead ofit
- Or use
eyes.fit
instead ofeyes.it
.
- Use
await browser.sleep(100000)
in your test, for quick browser stop and debugging.