jest-preset-split v1.0.5
jest-preset-split
This jest-preset stores your toggles with treatments from split.io as a global variable global.SPLITS available in your tests.
Installation
npm i jest-preset-split --save-devUsage
To use this preset, add it to your jest config and update your test command to include SPLITKEY for the environment which information you want to get.
Update Jest config
To make Jest use this preset, you need to update your Jest config and add jest-preset-split preset to it. For example, if your jest config is in the package.json file:
{
"name": "my-package",
"version": "1.0.0",
"dependencies": {
},
"jest": {
"preset": "jest-preset-split"
}
}Or in the jest.config.js file:
module.exports = {
...
preset: "jest-preset-split"
};Getting splits and current treatments
Once you update your jest config, you can now get all current splits via global.SPLITS variable. Just specify a valid split environment SDK key like this:
// using npx:
SPLITKEY=<YOUR_SDK_KEY_HERE> npx jest
// or via npm:
SPLITKEY=<YOUR_SDK_KEY_HERE> npm testHere is an example of global.SPLITS structure
{
'split-one': 'on',
'split-two': 'off',
'split-three': 'custom'
}Running tests based on splits
You can now filter tests by describe block:
import Foo from '../src/Foo';
if(global.SPLITS['split-1'] === 'on') {
describe( 'Foo class', () => {
it( '...', () => {
...
} );
...
} );
}Or you can have logic inside your test
describe( 'Dashboard page', () => {
it( '...', () => {
if(global.SPLITS['split-1'] === 'on') {
...
} else {
...
}
} );
} );Mocking splits in localhost mode
You can also use global.SPLITS to mock your environment splits in localhost mode
var sdk = splitio({
core: {
authorizationKey: 'localhost'
},
features: global.SPLITS,
scheduler: {
offlineRefreshRate: 15 // 15 sec
}
});Contribute
Want to help or have a suggestion? Open a new ticket and we can discuss it or submit a pull request.
License
MIT