Comparing version 1.1.0 to 2.0.0
{ | ||
"name": "jest-when", | ||
"version": "1.1.0", | ||
"description": "A when(fn).calledWith(args).thenReturn(value) lib for jest", | ||
"main": "dist/when.js", | ||
"files": [ | ||
"dist/when.js" | ||
], | ||
"author": "Tim Kindberg", | ||
"repository": { | ||
"type" : "git", | ||
"url" : "https://github.com/timkindberg/jest-when.git" | ||
}, | ||
"version": "2.0.0", | ||
"description": "An extension lib for jest", | ||
"license": "MIT", | ||
"private": false, | ||
"main": "src/when.js", | ||
"scripts": { | ||
"test": "jest", | ||
"build": "rm -rf dist && mkdir dist && babel when.js --out-file dist/when.js" | ||
"lint": "eslint src/", | ||
"lint.fix": "eslint src/ --fix", | ||
"stryker": "stryker run" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/timkindberg/jest-when/" | ||
}, | ||
"contributors": [ | ||
"Tim Kindberg <timkindberg@gmail.com>", | ||
"Jonas Holtkamp <jonas.holtkamp@senacor.com>" | ||
], | ||
"dependencies": { | ||
"bunyan": "^1.8.12", | ||
"expect": "^22.4.3" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.0", | ||
"babel-jest": "^22.1.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"eslint": "^4.19.1", | ||
"eslint-config-standard": "^11.0.0", | ||
"eslint-plugin-import": "^2.12.0", | ||
"eslint-plugin-node": "^6.0.1", | ||
"eslint-plugin-promise": "^3.7.0", | ||
"eslint-plugin-standard": "^3.1.0", | ||
"jest": "^22.1.1", | ||
"regenerator-runtime": "^0.11.1" | ||
"pre-commit": "^1.2.2", | ||
"stryker": "^0.24.0", | ||
"stryker-api": "^0.17.1", | ||
"stryker-html-reporter": "^0.14.1", | ||
"stryker-javascript-mutator": "^0.7.1", | ||
"stryker-jest-runner": "^0.7.0" | ||
}, | ||
"dependencies": { | ||
"expect": "^22.1.0" | ||
} | ||
"jest": { | ||
"verbose": false, | ||
"collectCoverage": true, | ||
"collectCoverageFrom": [ | ||
"src/**/*.js" | ||
], | ||
"coverageDirectory": "build/reports/coverage/", | ||
"coverageReporters": [ | ||
"html", | ||
"lcov", | ||
"text" | ||
], | ||
"testEnvironment": "node", | ||
"resetModules": true | ||
}, | ||
"pre-commit": [ | ||
"lint", | ||
"test" | ||
] | ||
} |
169
README.md
# jest-when | ||
[![build status](https://travis-ci.org/timkindberg/jest-when.svg?branch=master)](https://travis-ci.org/timkindberg/jest-when) | ||
[![codecov](https://codecov.io/gh/timkindberg/jest-when/branch/master/graph/badge.svg)](https://codecov.io/gh/timkindberg/jest-when) | ||
[![GitHub license](https://img.shields.io/github/license/timkindberg/jest-when.svg)](https://github.com/timkindberg/jest-when/blob/master/LICENSE) | ||
[![npm](https://img.shields.io/npm/v/jest-when.svg)](https://www.npmjs.com/package/jest-when) | ||
A fork from [@timkindberg](https://github.com/timkindberg/)'s [jest-when](https://github.com/timkindberg/jest-when). | ||
An extended, sugary way to mock return values for specific arguments only | ||
### Features | ||
`jest-when` allows you to use a set of the original | ||
[Jest mock functions](https://facebook.github.io/jest/docs/en/mock-function-api) in order to train | ||
your mocks only based on parameters your mocked function is called with. | ||
An example statement would be as follows: | ||
```javascript | ||
when(fn).calledWith(1).mockReturnValue('yay!') | ||
``` | ||
The trained mock function `fn` will now behave as follows -- assumed no other trainings took place: | ||
* return `yay!` if called with `1` _as first parameter_ | ||
* return `undefined` if called with _any other first parameter_ than `1` | ||
For extended usage see the examples below. | ||
The supported set of mock functions is: | ||
* `mockReturnValue` | ||
* `mockReturnValueOnce` | ||
* `mockResolvedValue` | ||
* `mockResolvedValueOnce` | ||
* `mockRejectedValue` | ||
* `mockRejectedValueOnce` | ||
### Usage | ||
#### Installation | ||
```bash | ||
npm i --save-dev jest-when | ||
``` | ||
A sugary way to mock return values for specific arguments only. | ||
#### Basic usage: | ||
```javascript | ||
import { when } from 'jest-when'; | ||
import { when } from 'jest-when' | ||
const fn = jest.fn(); | ||
when(fn).calledWith(1).mockReturnValue('yay!'); | ||
const fn = jest.fn() | ||
when(fn).calledWith(1).mockReturnValue('yay!') | ||
const result = fn(1); | ||
expect(result).toEqual('yay!'); | ||
expect(fn(1)).toEqual('yay!') | ||
``` | ||
#### Supports multiple args: | ||
#### Supports chaining of mock trainings: | ||
```javascript | ||
import { when } from 'jest-when'; | ||
when(fn) | ||
.calledWith(1).mockReturnValue('yay!') | ||
.calledWith(2).mockReturnValue('nay!') | ||
const fn = jest.fn(); | ||
when(fn).calledWith(1, true, 'foo').mockReturnValue('yay!'); | ||
expect(fn(1)).toEqual('yay!') | ||
expect(fn(2)).toEqual('nay!') | ||
``` | ||
Thanks to [@fkloes](https://github.com/fkloes). | ||
const result = fn(1, true, 'foo'); | ||
expect(result).toEqual('yay!'); | ||
```javascript | ||
when(fn) | ||
.calledWith(1) | ||
.mockReturnValueOnce('yay!') | ||
.mockReturnValue('nay!') | ||
expect(fn(1)).toEqual('yay!') | ||
expect(fn(1)).toEqual('nay!') | ||
``` | ||
Thanks to [@danielhusar](https://github.com/danielhusar). | ||
#### Supports replacement of mock trainings: | ||
```javascript | ||
when(fn).calledWith(1).mockReturnValue('yay!') | ||
expect(fn(1)).toEqual('yay!') | ||
when(fn).calledWith(1).mockReturnValue('nay!') | ||
expect(fn(1)).toEqual('nay!') | ||
``` | ||
This replacement of the training does only happen for mock functions _not_ ending in `*Once`. | ||
Trainings like `mockReturnValueOnce` are removed after a matching function call anyway. | ||
Thanks to [@fkloes](https://github.com/fkloes). | ||
#### Supports multiple args with partial argument matching: | ||
```javascript | ||
when(fn).calledWith(1, true).mockReturnValue('yay!') | ||
expect(fn(1, true)).toEqual('yay!') | ||
expect(fn(1, true, 'foo')).toEqual('yay!') | ||
``` | ||
#### Supports training for single calls | ||
```javascript | ||
when(fn).calledWith(1, true, 'foo').mockReturnValueOnce('yay!') | ||
when(fn).calledWith(1, true, 'foo').mockReturnValueOnce('nay!') | ||
expect(fn(1, true, 'foo')).toEqual('yay!') | ||
expect(fn(1, true, 'foo')).toEqual('nay!') | ||
expect(fn(1, true, 'foo')).toBeUndefined() | ||
``` | ||
#### Supports Promises, both resolved and rejected | ||
```javascript | ||
when(fn).calledWith(1).mockResolvedValue('yay!') | ||
when(fn).calledWith(2).mockResolvedValueOnce('nay!') | ||
await expect(fn(1)).resolves.toEqual('yay!') | ||
await expect(fn(1)).resolves.toEqual('yay!') | ||
await expect(fn(2)).resolves.toEqual('nay!') | ||
expect(await fn(2)).toBeUndefined() | ||
when(fn).calledWith(3).mockRejectedValue(new Error('oh no!')) | ||
when(fn).calledWith(4).mockRejectedValueOnce(new Error('oh no, an error again!')) | ||
await expect(fn(3)).rejects.toThrow('oh no!') | ||
await expect(fn(3)).rejects.toThrow('oh no!') | ||
await expect(fn(4)).rejects.toThrow('oh no, an error again!') | ||
expect(await fn(4)).toBeUndefined() | ||
``` | ||
#### Supports jest matchers: | ||
```javascript | ||
import { when } from 'jest-when'; | ||
const fn = jest.fn(); | ||
when(fn).calledWith( | ||
@@ -39,6 +135,6 @@ expect.anything(), | ||
expect.arrayContaining(false) | ||
).mockReturnValue('yay!'); | ||
).mockReturnValue('yay!') | ||
const result = fn('whatever', 100, [true, false]); | ||
expect(result).toEqual('yay!'); | ||
const result = fn('whatever', 100, [true, false]) | ||
expect(result).toEqual('yay!') | ||
``` | ||
@@ -48,15 +144,12 @@ | ||
```javascript | ||
import { when } from 'jest-when'; | ||
when(fn).calledWith(1).mockReturnValue('no') | ||
when(fn).calledWith(2).mockReturnValue('way?') | ||
when(fn).calledWith(3).mockReturnValue('yes') | ||
when(fn).calledWith(4).mockReturnValue('way!') | ||
const fn = jest.fn(); | ||
when(fn).calledWith(1).mockReturnValue('no'); | ||
when(fn).calledWith(2).mockReturnValue('way?'); | ||
when(fn).calledWith(3).mockReturnValue('yes'); | ||
when(fn).calledWith(4).mockReturnValue('way!'); | ||
expect(fn(1)).toEqual('no'); | ||
expect(fn(2)).toEqual('way?'); | ||
expect(fn(3)).toEqual('yes'); | ||
expect(fn(4)).toEqual('way!'); | ||
expect(fn(5)).toEqual(undefined); | ||
expect(fn(1)).toEqual('no') | ||
expect(fn(2)).toEqual('way?') | ||
expect(fn(3)).toEqual('yes') | ||
expect(fn(4)).toEqual('way!') | ||
expect(fn(5)).toEqual(undefined) | ||
``` | ||
@@ -66,13 +159,17 @@ | ||
Use `expectCalledWith` instead to run an assertion that the `fn` was called with the provided args. Your test will fail if the jest mock function is ever called without those exact `expectCalledWith` params. | ||
Use `expectCalledWith` instead to run an assertion that the `fn` was called with the provided | ||
args. Your test will fail if the jest mock function is ever called without those exact | ||
`expectCalledWith` params. | ||
Disclaimer: This won't really work very well with compound declarations, because one of them will always fail, and throw an assertion error. | ||
Disclaimer: This won't really work very well with compound declarations, because one of them will | ||
always fail, and throw an assertion error. | ||
```javascript | ||
import { when } from 'jest-when'; | ||
when(fn).expectCalledWith(1).mockReturnValue('x') | ||
const fn = jest.fn(); | ||
when(fn).expectCalledWith(1).mockReturnValue('x'); | ||
fn(2); // Will throw a helpful jest assertion error with args diff | ||
``` | ||
### Contributors (in order of contribution) | ||
* [@timkindberg](https://github.com/timkindberg/) (original author) | ||
* [@fkloes](https://github.com/fkloes) | ||
* [@danielhusar](https://github.com/danielhusar) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
153562
12
336
173
2
13
7
1
+ Addedbunyan@^1.8.12
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbunyan@1.8.15(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addeddtrace-provider@0.8.8(transitive)
+ Addedglob@6.0.4(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addedmv@2.1.1(transitive)
+ Addednan@2.22.0(transitive)
+ Addedncp@2.0.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedrimraf@2.4.5(transitive)
+ Addedsafe-json-stringify@1.2.0(transitive)
+ Addedwrappy@1.0.2(transitive)
Updatedexpect@^22.4.3