The MDS development environment contains automated visual regression tests built on the BackstopJS testing framework.
BackstopJS is an automated visual regression (AVR) testing framework capable of capturing and comparing screenshots using various browser engines and platforms. AVR tests are run by taking screenshots of a component test page. These test pages contain examples that represent nearly every permutation of component property.
3.5.10
or higher.17.12
or higher. You must install Docker prior to running the tests. Docker is a “headless virtual machine” that spins up while the tests are running and then exits after the tests have finished. Docker ensures tests are run in an identical environment regardless of who is running the tests.develop
branch in the library
directory.When building a new component, see the building a component page for detailed instructions and terminal commands for generating all of the starter files you will need. One of these files will be a test page where you will create your AVR tests, located at:
library/test/visual/test-pages/components/[new-component].njk
To create a test, wrap sections of the component test page with a special nunjucks macro:
{% call test_row() %}
<h2 class="mds-test__header">Default</h2>
{{ library.button() }}
{% endcall %}
Everything within the test_row()
call will be captured as a new image and used to create a new test.
The testing functionality is exposed via npm run commands:
npm run avr
— Runs the tests by initiating the AVR CLI. Follow the prompts indicated in the CLI to configure your AVR test run. There are three different types of AVR tests.library
directory. These tests run against the HTML/CSS “individual” test pages. This is the most common use of AVR.component.css
. This visual diffing test copies each of those “individual” test pages and creates a new version that contains the entire library, mds.css
. The “individual” and “library” versions of the test page are compared to ensure there are no issues when the entire CSS library is loaded on the page.npm run avr:approve-new-baseline
— Promotes the latest test images as the new baseline for subsequent tests.Run npm run avr
from the command line to run tests. Testing can take several minutes depending on the speed of your computer. Once a test is complete, the results will display in your browser.
A visual diffing test can “fail” in two ways:
Run npm run avr:approve-new-baseline
to promote the latest test’s screenshots as the new baseline screenshots for future tests. This tells the testing framework, “These changes are approved. Don’t flag them as failures anymore.” Commit the new baseline images to the MDS repository along with the code changes that triggered the visual difference.