nemo-fixo is a nemo plugin to manage your test fixtures.
Test fixtures can be complex when you need to run your tests for different environments and locales, as there tends to be a lot of data duplication.
nemo-fixo
solves this issue with fixo
, an extension to JSON parser to extend and compose JSON objects. fixo
instance is accessible from nemo.fixo
in a test case.
There are also load
and iterate
functions to compose test cases based on the fixture data.
$ npm install nemo-fixo
Edit Nemo config.json
file, add nemo-fixo
plugin settings:
{
"plugins": {
"nemo-fixo": {
"module": "nemo-fixo",
"arguments": [{
"srcDir": "test/fixture"
}]
}
}
}
- srcDir: Test fixtures directory (required, default:
test/fixture
) - specDir: Test specs directory (optional, default:
undefined
), more info here. - defaultIterateList : Default list loaded by
iterate
helper function (optional, default:all
), more info here.
// Load fixture
nemo.fixo.load('card', function(err, card) { ... });
// Or with Promise
nemo.fixo.load('card').then(function(card) { ... });
// Load fixture property for a profile
nemo.fixo.load('card.visa', 'GB', function(card) { ... });
// Load multiple fixtures
nemo.fixo.load(['card', 'bank'], function(fixtures) { ... });
// Load fixture in a folder
nemo.fixo.load('folder/card', function(card) { ... });
Fixo supports object inheritance and override, example:
GB
extends frommaster
GB-en
extends frommaster
,GB
,en
GB-en-dev
extends frommaster
,GB
,en
,GB-en
{
"master": {
"visa": {
"account_number": "1111111111111111",
"expiry_date": "08/18",
"cvv": "123"
},
"diners": {
"account_number": "22222222222222",
"expiry_date": "08/18",
"cvv": "123"
},
"amex": {
"account_number": "333333333333333",
"expiry_date": "08/18",
"cvv": "123"
}
},
"GB": {
"visa": {
"account_number": "4444444444444444"
}
},
"en": {},
"GB-en": {},
"GB-en-GB": {}
}
Shared default values. Visa, Diners, Amex extend the default values:
{
"master": {
"default": {
"expiry_date": "08/18",
"cvv": "123"
},
"visa": {
"account_number": "1111111111111111",
"cvv": "123"
},
"diners": {
"account_number": "22222222222222",
},
"amex": {
"account_number": "333333333333333"
}
}
}
Resolvers
allow you to transform an object property value as a whole, while Macros
allow you to replace parts of a string value with dynamic content.
You could easily create custom resolvers and macros, refer to fixo documentation for more information.
{
"master": {
"profile": {
"name": "Walter Mitty",
"email": "test-{random}@domain.com"
}
"name": "get:profile.name",
"wallet": {
"bank": "include:bank.citibank",
"card_GB": "include.GB:card.visa"
}
},
"GB": {
"profile": {
"name": "William"
}
}
}
Use your test fixture data to drive your test cases. nemo-fixo
provides load
and iterate
functions. They load fixtures synchronously, so you could use the fixture data to customize your test spec:
nemo-fixo/load
— to load one or multiple fixturenemo-fixo/iterate
— to load one or multiple fixtures and iterate over different profiles
var load = require('nemo-fixo/load');
var fixture = load('fixture-name');
if (fixture.flagOn) {
it('should test this', function() { ... }
} else {
it('should test that', function() { ... }
}
// Load multiple fixtures
var fixtures = load(['card', 'bank']);
To repeat the same set of test cases for each profile, with the option for further customization:
var iterate = require('nemo-fixo/iterate');
iterate('fixture-name', ['US', 'GB', 'CA'], function(country, iterateFixture) {
it('should do this', function(fixture) { ... });
if (country === 'US' && iterateFixture.flagOn) {
it('should also do this', function(done, fixture) { ... });
}
});
Iterate profile list is determined from multiple sources in the following precedence:
FIXO_ITERATE_PROFILES
environment variable, a comma-seperated value of profile list. Used in development to force iterate a fixed set of profiles.- Profile list argument passed to
iterate
function - Profile list configured in the fixture file under
iterate
property name
// test/fixture/card.json
{
"master": {
"visa": {},
"amex": {}
},
"US": {},
"GB": {},
"CA": {},
...
"iterate": {
"all": ["US", "GB", "CA"]
}
}
// test/spec/test-card.js
// Profile list is retrieved from the card.json, i.e. ['US', 'GB', 'CA']
iterate('card', function(profile, fixture) {
...
});
You could configure multiple profile lists in your fixture file. By default, iterate method will look for the all
list:
// test/fixture/card.json
{
"master": {},
...
"iterate": {
"all": [...],
"small": [...],
"medium": [...],
"large": [...]
}
}
If you need to change the default list name, set defaultIterateList
option when configuring nemo fixo plugin. To load a specific profile list, you could pass the list
option:
iterate('card', undefined, { list: 'small' }, function(profile, card) { ... });
When configuring your CI environment, you could also set FIXO_ITERATE_LIST
environment variable.
To load fixture matching the spec name and the directory structure, configure specDir
option in Nemo config.json
and pass __filename
for the fixture name.
// Nemo config.json
{
"plugins": {
"nemo-fixo": {
"modules": "nemo-fixo"
"arguments": [{
"srcDir": "test/fixture"
"specDir": "test/spec" // Comment off to ignore fixture directory structure
}]
}
}
}
// test/spec/nested/test-card.js
// fixture will load from test/fixture/nested/test-card.json
load(__filename, function(fixture) { ... });
iterate(__filename, function(profile) { ... });
Copyright (C) 2016 PayPal
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.