Socket
Socket
Sign inDemoInstall

jest-each

Package Overview
Dependencies
Maintainers
1
Versions
144
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-each - npm Package Compare versions

Comparing version 0.3.1 to 0.4.0

dist/array.js

75

dist/index.js
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['module', 'exports', 'sprintf-js'], factory);
define(['module', 'exports', './array', './template'], factory);
} else if (typeof exports !== "undefined") {
factory(module, exports, require('sprintf-js'));
factory(module, exports, require('./array'), require('./template'));
} else {

@@ -10,6 +10,6 @@ var mod = {

};
factory(mod, mod.exports, global.sprintfJs);
factory(mod, mod.exports, global.array, global.template);
global.index = mod.exports;
}
})(this, function (module, exports, _sprintfJs) {
})(this, function (module, exports, _array, _template) {
'use strict';

@@ -21,64 +21,21 @@

function _toConsumableArray(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
arr2[i] = arr[i];
}
var _array2 = _interopRequireDefault(_array);
return arr2;
} else {
return Array.from(arr);
}
var _template2 = _interopRequireDefault(_template);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
exports.default = function (parameterRows) {
var defaultGlobal = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : global;
exports.default = function () {
if (arguments.length > 1) {
return (0, _template2.default)(global).apply(undefined, arguments);
}
var tests = parameterisedTests(parameterRows);
var globalTest = defaultGlobal.test;
var test = tests(globalTest);
test.skip = tests(globalTest.skip);
test.only = tests(globalTest.only);
var globalIt = defaultGlobal.it;
var it = tests(globalIt);
it.skip = tests(globalIt.skip);
it.only = tests(globalIt.only);
var xtest = tests(defaultGlobal.xtest);
var xit = tests(defaultGlobal.xit);
var fit = tests(defaultGlobal.fit);
var globalDescribe = defaultGlobal.describe;
var describe = tests(globalDescribe);
describe.skip = tests(globalDescribe.skip);
describe.only = tests(globalDescribe.only);
var fdescribe = tests(defaultGlobal.fdescribe);
var xdescribe = tests(defaultGlobal.xdescribe);
return { test: test, xtest: xtest, it: it, xit: xit, fit: fit, describe: describe, fdescribe: fdescribe, xdescribe: xdescribe };
return (0, _array2.default)(global).apply(undefined, arguments);
};
var parameterisedTests = function parameterisedTests(parameterRows) {
return function (globalCb) {
return function (title, test) {
parameterRows.forEach(function (params) {
return globalCb((0, _sprintfJs.vsprintf)(title, params), applyTestParams(params, test));
});
};
};
};
var applyTestParams = function applyTestParams(params, test) {
if (params.length < test.length) return function (done) {
return test.apply(undefined, _toConsumableArray(params).concat([done]));
};
return function () {
return test.apply(undefined, _toConsumableArray(params));
};
};
module.exports = exports['default'];
});
{
"name": "jest-each",
"version": "0.3.1",
"version": "0.4.0",
"description": "Parameterised tests for Jest",

@@ -11,3 +11,3 @@ "main": "dist/index.js",

"scripts": {
"build": "babel src -d dist --ignore *.spec.js",
"build": "babel src -d dist --ignore *.test.js",
"prepublish": "npm run build",

@@ -39,2 +39,3 @@ "test": "jest --testPathPattern=src",

"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-gwt": "^1.0.0",
"babel-plugin-transform-es2015-modules-umd": "^6.24.0",

@@ -53,3 +54,4 @@ "babel-preset-es2015": "^6.24.0",

"add-module-exports",
"transform-es2015-modules-umd"
"transform-es2015-modules-umd",
"gwt"
]

@@ -56,0 +58,0 @@ },

@@ -11,2 +11,3 @@ <div align="center">

[![version](https://img.shields.io/npm/v/jest-each.svg?style=flat-square)](https://www.npmjs.com/package/jest-each)
[![downloads](https://img.shields.io/npm/dm/jest-each.svg?style=flat-square)](http://npm-stat.com/charts.html?package=jest-each&from=2017-03-21)
[![MIT License](https://img.shields.io/npm/l/jest-each.svg?style=flat-square)](https://github.com/mattphillips/jest-each/blob/master/LICENSE)

@@ -34,4 +35,15 @@ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

- Unique test titles with: [sprintf](https://github.com/alexei/sprintf.js)
- 🖖 Spock like data tables with [Tagged Template Literals](#tagged-template-literal-of-rows)
---
- [Demo](#demo)
- [Installation](#installation)
- [Importing](#importing)
- APIs
* [Array of Rows](#array-of-rows)
- [Usage](#usage)
* [Tagged Template Literal of rows](#tagged-template-literal-of-rows)
- [Usage](#usage-1)
## Demo

@@ -49,2 +61,6 @@

**`.test` with Tagged Template Literals**
![Current jest tests](assets/tagged-template-literal.gif)
**`.describe`**

@@ -72,23 +88,25 @@

## API
## Array of rows
### `each([parameters]).test(name, testFn)`
### API
#### `each`:
#### `each([parameters]).test(name, testFn)`
##### `each`:
- parameters: `Array` of Arrays with the arguments that are passed into the `testFn` for each row
#### `.test`:
##### `.test`:
- name: `String` the title of the `test`, use `%s` in the name string to positionally inject parameter values into the test title
- testFn: `Function` the test logic, this is the function that will receive the parameters of each row as function arguments
### `each([parameters]).describe(name, suiteFn)`
#### `each([parameters]).describe(name, suiteFn)`
#### `each`:
##### `each`:
- parameters: `Array` of Arrays with the arguments that are passed into the `suiteFn` for each row
#### `.describe`:
##### `.describe`:
- name: `String` the title of the `describe`, use `%s` in the name string to positionally inject parameter values into the suite title
- suiteFn: `Function` the suite of `test`/`it`s to be ran, this is the function that will receive the parameters in each row as function arguments
## Usage
### Usage

@@ -99,5 +117,2 @@ #### `.test(name, fn)`

```js
import each from 'jest-each';
import add from './add';
each([

@@ -108,3 +123,3 @@ [1, 1, 2],

]).test('returns the result of adding %s to %s', (a, b, expected) => {
expect(add(a, b)).toBe(expected);
expect(a + b).toBe(expected);
});

@@ -122,3 +137,3 @@ ```

]).test.only('returns the result of adding %s to %s', (a, b, expected) => {
expect(add(a, b)).toBe(expected);
expect(a + b).toBe(expected);
});

@@ -136,3 +151,3 @@ ```

]).test.skip('returns the result of adding %s to %s', (a, b, expected) => {
expect(add(a, b)).toBe(expected);
expect(a + b).toBe(expected);
});

@@ -161,5 +176,2 @@ ```

```js
import each from 'jest-each';
import add from './add';
each([

@@ -171,7 +183,7 @@ [1, 1, 2],

test(`returns ${expected}`, () => {
expect(add(a, b)).toBe(expected);
expect(a + b).toBe(expected);
});
test('does not mutate first arg', () => {
add(a, b);
a + b;
expect(a).toBe(a);

@@ -181,3 +193,3 @@ });

test('does not mutate second arg', () => {
add(a, b);
a + b;
expect(b).toBe(b);

@@ -198,3 +210,3 @@ });

test(`returns ${expected}`, () => {
expect(add(a, b)).toBe(expected);
expect(a + b).toBe(expected);
});

@@ -214,3 +226,3 @@ });

test(`returns ${expected}`, () => {
expect(add(a, b)).toBe(expected);
expect(a + b).toBe(expected);
});

@@ -220,4 +232,183 @@ });

---
## Tagged Template Literal of rows
### API
#### `each[tagged template].test(name, suiteFn)`
```js
each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`.test('returns $expected when adding $a to $b', ({ a, b, expected }) => {
expect(a + b).toBe(expected);
});
```
##### `each` takes a tagged template string with:
- First row of variable name column headings seperated with `|`
- One or more subsequent rows of data supplied as template literal expressions using `${value}` syntax.
##### `.test`:
- name: `String` the title of the `test`, use `$variable` in the name string to inject test values into the test title from the tagged template expressions
- testFn: `Function` the test logic, this is the function that will receive the parameters of each row as function arguments
#### `each[tagged template].describe(name, suiteFn)`
```js
each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`.describe('$a + $b', ({ a, b, expected }) => {
test(`returns ${expected}`, () => {
expect(a + b).toBe(expected);
});
test('does not mutate first arg', () => {
a + b;
expect(a).toBe(a);
});
test('does not mutate second arg', () => {
a + b;
expect(b).toBe(b);
});
});
```
##### `each` takes a tagged template string with:
- First row of variable name column headings seperated with `|`
- One or more subsequent rows of data supplied as template literal expressions using `${value}` syntax.
##### `.describe`:
- name: `String` the title of the `test`, use `$variable` in the name string to inject test values into the test title from the tagged template expressions
- suiteFn: `Function` the suite of `test`/`it`s to be ran, this is the function that will receive the parameters in each row as function arguments
### Usage
#### `.test(name, fn)`
Alias: `.it(name, fn)`
```js
each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`.test('returns $expected when adding $a to $b', ({ a, b, expected }) => {
expect(a + b).toBe(expected);
});
```
#### `.test.only(name, fn)`
Aliases: `.it.only(name, fn)` or `.fit(name, fn)`
```js
each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`.test.only('returns $expected when adding $a to $b', ({ a, b, expected }) => {
expect(a + b).toBe(expected);
});
```
#### `.test.skip(name, fn)`
Aliases: `.it.skip(name, fn)` or `.xit(name, fn)` or `.xtest(name, fn)`
```js
each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`.test.skip('returns $expected when adding $a to $b', ({ a, b, expected }) => {
expect(a + b).toBe(expected);
});
```
#### Asynchronous `.test(name, fn(done))`
Alias: `.it(name, fn(done))`
```js
each`
str
${'hello'}
${'mr'}
${'spy'}
`.test('gives 007 secret message: $str', ({ str }, done) => {
const asynchronousSpy = (message) => {
expect(message).toBe(str);
done();
};
callSomeAsynchronousFunction(asynchronousSpy)(str);
});
```
#### `.describe(name, fn)`
```js
each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`.describe('$a + $b', ({ a, b, expected }) => {
test(`returns ${expected}`, () => {
expect(a + b).toBe(expected);
});
test('does not mutate first arg', () => {
a + b;
expect(a).toBe(a);
});
test('does not mutate second arg', () => {
a + b;
expect(b).toBe(b);
});
});
```
#### `.describe.only(name, fn)`
Aliases: `.fdescribe(name, fn)`
```js
each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`.describe.only('$a + $b', ({ a, b, expected }) => {
test(`returns ${expected}`, () => {
expect(a + b).toBe(expected);
});
});
```
#### `.describe.skip(name, fn)`
Aliases: `.xdescribe(name, fn)`
```js
each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`.describe.skip('$a + $b', ({ a, b, expected }) => {
test(`returns ${expected}`, () => {
expect(a + b).toBe(expected);
});
});
```
## License
MIT
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc