Skip to content

Commit

Permalink
feat: normalize spy (#22)
Browse files Browse the repository at this point in the history
* feat: normalize spy

BREAKING CHANGE:
Rename `Expecter` to `Expectation`.
Remove `Satisfier` class.
Change `Spy` structure.

* test: fix test.

Remoe await test as it is just testing Promise/A+.

* chore: remove unpartial

* refactor: improve coverage

callback is always assigned as it is used internally.

* chore: update deps
  • Loading branch information
unional authored Jan 9, 2018
1 parent 40b3d93 commit 97fc092
Show file tree
Hide file tree
Showing 16 changed files with 813 additions and 1,167 deletions.
7 changes: 2 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ node_js:
branches:
only:
- master
- assertron
- /^greenkeeper.*$/
# except:
# - /^v\d+\.\d+\.\d+$/
before_install:
- npm install -g greenkeeper-lockfile@1

Expand All @@ -26,5 +23,5 @@ script:
after_script: greenkeeper-lockfile-upload

after_success:
- npm install coveralls && npm run coveralls
- npm run semantic-release
- npm run travis-deploy-once "npm run semantic-release"
- npm install --no-save coveralls && npm run coveralls
50 changes: 28 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

Manage and generate artifacts to test data across boundaries.

## createSatisfier(expecter)
## createSatisfier(expectation)

Each property in `expecter` can be a value, a `RegExp`, or a predicate function.
Each property in `expectation` can be a value, a `RegExp`, or a predicate function.

### test(actual)

test `actual` against `expecter`.
test `actual` against `expectation`.

```ts
import { createSatisfier } from 'satisfier'
Expand All @@ -32,44 +32,32 @@ createSatisfier({ a: /boo/ }).test({ a: 'foo' })
createSatisfier({ a: () => false }).test({ a: 1 })
```

## exec(actual)
### exec(actual)

check `actual` against `expecter` and returns the checking result.
check `actual` against `expectation` and returns the checking result.
If `actual` meets the criteria, returns `null`.

```ts
import { createSatisfier } from 'satisfier'

// these returns null
// returns undefined
createSatisfier({ a: 1 }).exec({ a: 1, b: 2 })
createSatisfier({ a: /foo/ }).exec({ a: 'foo', b: 'boo' })
createSatisfier({ a: n => n === 1 }).exec({ a: 1, b, 2 })

// [{ path: ['a'], expected: 1, actual: 2}]
// returns [{ path: ['a'], expected: 1, actual: 2}]
createSatisfier({ a: 1 }).exec({ a: 2 })

// [{ path: ['b'], expected: 2, actual: undefined}]
// returns [{ path: ['b'], expected: 2, actual: undefined}]
createSatisfier({ a: 1, b: 2 }).exec({ a: 1 })

// [{ path: ['a'], expected: /boo/, actual: 'foo'}]
// returns [{ path: ['a'], expected: /boo/, actual: 'foo'}]
createSatisfier({ a: /boo/ }).exec({ a: 'foo' })

// [{ path: ['a'], expected: 'a => a === 1', actual: 2}]
// returns [{ path: ['a'], expected: 'a => a === 1', actual: 2}]
createSatisfier({ a: a => a === 1 }).exec({ a: 2 })
```

## Satisfier

This is identical to `createSatisfier()`, but as a class.

```ts
import { Satisfier } from 'satisfier'

const s = new Satisfier({...})
s.test(...)
s.exec(...)
```

## Build in predicates

There are a few predicates shipped in the package for convenience.
Expand All @@ -87,6 +75,24 @@ results[0].expected.tersify()
tersify(results[0])
```

## spy(fn)

You can use `spy(fn)` to spy on function and record the calls.
It support 3 main case of async functions.
They are all converted to Promise syntax so you can get the result using `call[n].then(...)`

```ts
import { spy } from 'satisfier'

spy((a, b, cb) => { cb(a + 1, b - 1) })
spy((options) => { options.success() })
const spied = spy((a) => Promise.resolve(a + 1))
await spied.fn(1)
const callRecord = spied.calls[0]
t.is(callRecord.arguments[0], 1)
callRecord.then(result => t.is(result, 2))
```

## Contribute

```sh
Expand Down
Loading

0 comments on commit 97fc092

Please sign in to comment.