Store test fixtures in files within a designated directory, and assert on those test fixtures in tests.
yarn add qunit-fixtures --dev
# or
npm install qunit-fixtures --save-devImport and run the one-time fixture setup function to load the fixtures for your tests:
import { setupFixtures } from 'qunit-fixtures';
setupFixtures({ fixturePath: './tests/fixtures' });
// ...💡 Tip: It's safe to invoke the setupFixtures function multiple times; the fixtures will be setup only once per test run.
Fixtures can be asserted on by fixture name. The name corresponds to the path of the fixture within the fixturePath. The following illustrates some examples:
Given the directory with the fixturePath of root/tests/fixtures and the contents below:
├── fixture1.txt
└── sub
└── fixture2.txtThe fixtures would be read and cached in a data structure like so:
{
"fixture1": "I am the first fixture",
"sub/fixture2": "I am the section fixture"
}And each fixture would be accessible via the key.
The qunit-fixtures library extends QUnit's assert object to include a new function and assertion. The following example illustrates it:
assert.fixture(fixtureName).matches(expectedValue);Below is a complete example of using qunit-fixtures in tests.
import { module, test } from 'qunit';
import { setupFixtures } from 'qunit-fixtures';
setupFixtures({ fixturePath: './tests/fixtures' });
module('some-stuff', function() {
test('doSomeStuff matches the expected result', function(assert) {
let actualValue = doSomeStuff();
assert.fixture('some-stuff-result').matches(actualValue);
});
});See the Contributing guide for details.
This project is licensed under the MIT License.