https://github.com/ricca509/test-scenarios
Run your tests with different data (scenarios)
https://github.com/ricca509/test-scenarios
jasmine javascript jest mocha unit-testing
Last synced: 3 months ago
JSON representation
Run your tests with different data (scenarios)
- Host: GitHub
- URL: https://github.com/ricca509/test-scenarios
- Owner: ricca509
- Created: 2018-06-10T00:08:40.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-29T22:36:15.000Z (about 8 years ago)
- Last Synced: 2025-10-20T05:06:28.170Z (9 months ago)
- Topics: jasmine, javascript, jest, mocha, unit-testing
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/test-scenarios
- Size: 133 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# test-scenarios
Easily run the same unit test with different test data.
Not tied to a specific library, it can be used with
- [mocha](https://mochajs.org/)
- [jasmine](https://jasmine.github.io/)
- [jest](https://facebook.github.io/jest/)
- any library where tests are defined within a function.
## Example
```js
// sum.js
export const sum = (a, b) => a + b;
// sum.test.js
import scenarios from "test-scenarios";
describe("sum", () => {
scenarios([
{ a: 1, b: 2, result: 3 },
{ a: -1, b: 1, result: 0 }
], ({ a, b, result }, scenarioIndex) => {
describe(`Case ${scenarioIndex}: when ${a} and ${b} are passed`, () => {
it(`should return ${result}`, () => {
expect(sum(a, b)).toEqual(result);
});
});
});
});
```
Example output
```
PASS ./sum.test.js
sum
Case 0: when 1 and 2 are passed
✓ should return 3 (4ms)
Case 1: when -1 and 1 are passed
✓ should return 0
```
## API
`scenarios(testScenarios, testsFn)`
- `testScenarios`: `any` contains the test data for every scenario.
- `testsFn`: `Function` the test function to run each time with different data, it can be a whole `describe` block or multiple `it`/`test` wrapped into a function. It will be invoked with:
- `parameters`: `any` the data for the scenario
- `index`: `number` the scenario index
### Note for Jest users
You can achieve the same result as the above by using the following syntax:
```js
describe.each([
{ a: 1, b: 2, result: 3 },
{ a: -1, b: 1, result: 0 }
])('sum', ({ a, b, result }) => {
describe(`When ${a} and ${b} are passed`, () => {
it(`should return ${result}`, () => {
expect(sum(a, b)).toEqual(result);
});
});
});
```
More info on [describe.each](https://facebook.github.io/jest/docs/en/api.html#describeeachtable-name-fn) and [test.each](https://facebook.github.io/jest/docs/en/api.html#testeachtable-name-fn).