Update jest

This commit is contained in:
Rico Sta. Cruz 2017-08-29 00:08:07 +08:00
parent 97abd2e302
commit fffe9c38f9
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 156 additions and 28 deletions

188
jest.md
View File

@ -1,76 +1,191 @@
--- ---
title: Jest title: Jest
category: JavaScript libraries category: JavaScript libraries
layout: 2017/sheet
--- ---
## Testing Testing
-------
{: .-three-column}
### Quick start
{: .-prime}
```bash
npm install --save-dev jest babel-jest
```
```js ```js
beforeEach(() => { ... }) /* Add to package.json */
afterEach(() => { ... }) "scripts": {
"test": "jest"
}
```
{: .-setup}
beforeAll(() => { ... }) ```bash
afterAll(() => { ... }) # Run your tests
npm test -- --watch
```
{: .-setup}
See: [Getting started](http://facebook.github.io/jest/docs/en/getting-started.html)
### Writing tests
```js
describe('My work', () => { describe('My work', () => {
test('works', () => { test('works', () => {
expect(2).toEqual(2) expect(2).toEqual(2)
}) })
test('works asynchonously', () => {
return new Promise((resolve, reject) => { ... })
}) })
```
test('works asynchonously', async () => { ### BDD syntax
const hello = await foo()
... ```js
describe('My work', () => {
it('works', () => {
···
}) })
}) })
``` ```
## Expect `it` is an alias for `test`.
See: [test()](http://facebook.github.io/jest/docs/en/api.html#testname-fn)
### Asynchronous tests
```js
test('works with promises', () => {
return new Promise((resolve, reject) => {
···
})
})
```
```js
test('works with async/await', async () => {
const hello = await foo()
···
})
```
Return promises, or use async/await.
See: [Async tutorial](http://facebook.github.io/jest/docs/en/tutorial-async.html)
### Setup
```js
beforeEach(() => { ... })
afterEach(() => { ... })
```
```js
beforeAll(() => { ... })
afterAll(() => { ... })
```
See: [afterAll() and more](http://facebook.github.io/jest/docs/en/api.html#afterallfn)
### Focusing tests
```js
describe.only(···)
it.only(···) // alias: fit()
```
See: [test.only](http://facebook.github.io/jest/docs/en/api.html#testonlyname-fn)
### Skipping tests
```js
describe.skip(···)
it.skip(···) // alias: xit()
```
See: [test.skip](http://facebook.github.io/jest/docs/en/api.html#testskipname-fn)
Expect
------
{: .-three-column}
### Basic expectations
```js ```js
expect(value) expect(value)
.not .not
.toBe(value) .toBe(value)
.toEqual(value) .toEqual(value)
```
// Snapshots See: [expect()](http://facebook.github.io/jest/docs/en/expect.html#expectvalue)
### Snapshots
```js
expect(value)
.toMatchSnapshot() .toMatchSnapshot()
```
// Errors ### Errors
```js
expect(value)
.toThrow(error) .toThrow(error)
.toThrowErrorMatchingSnapshot() .toThrowErrorMatchingSnapshot()
```
// Booleans ### Booleans
```js
expect(value)
.toBeFalsy() .toBeFalsy()
.toBeNull() .toBeNull()
.toBeTruthy() .toBeTruthy()
.toBeUndefined() .toBeUndefined()
.toBeDefined() .toBeDefined()
```
// Numbers ### Numbers
```js
expect(value)
.toBeCloseTo(number, numDigits) .toBeCloseTo(number, numDigits)
.toBeGreaterThan(number) .toBeGreaterThan(number)
.toBeGreaterThanOrEqual(number) .toBeGreaterThanOrEqual(number)
.toBeLessThan(number) .toBeLessThan(number)
.toBeLessThanOrEqual(number) .toBeLessThanOrEqual(number)
```
// Objects ### Objects
```js
expect(value)
.toBeInstanceOf(Class) .toBeInstanceOf(Class)
.toMatchObject(object) .toMatchObject(object)
.toHaveProperty(keyPath, value) .toHaveProperty(keyPath, value)
```
// Arrays ### Objects
```js
expect(value)
.toContain(item) .toContain(item)
.toContainEqual(item) .toContainEqual(item)
.toHaveLength(number) .toHaveLength(number)
```
// String ### Strings
```js
expect(value)
.toMatch(regexpOrString) .toMatch(regexpOrString)
``` ```
### Others
```js ```js
expect.extend(matchers) expect.extend(matchers)
expect.any(constructor) expect.any(constructor)
@ -79,20 +194,28 @@ expect.addSnapshotSerializer(serializer)
expect.assertions(1) expect.assertions(1)
``` ```
## Snapshots More features
-------------
``` ### Snapshots
```jsx
const tree = renderer.create( const tree = renderer.create(
<Link page="http://www.facebook.com">Facebook</Link> <Link page="http://www.facebook.com">Facebook</Link>
).toJSON() ).toJSON()
// First run creates a snapshot; subsequent runs match it
expect(tree).toMatchSnapshot()
// To update snapshots: jest --updateSnapshot
``` ```
## Mocks ```jsx
// First run creates a snapshot; subsequent runs match it
expect(tree).toMatchSnapshot()
```
```bash
# To update snapshots
jest --updateSnapshot
```
### Mocks
```js ```js
const fn = jest.fn() const fn = jest.fn()
@ -115,7 +238,7 @@ expect(fn)
.toHaveBeenCalledWith(expect.stringMatching(regexp)) .toHaveBeenCalledWith(expect.stringMatching(regexp))
``` ```
## [Timers](https://facebook.github.io/jest/docs/timer-mocks.html) ### Timers
```js ```js
jest.useFakeTimers() jest.useFakeTimers()
@ -127,6 +250,11 @@ it('works', () => {
}) })
``` ```
## References See: [Timers](https://facebook.github.io/jest/docs/timer-mocks.html)
Based on Jest v19. <http://facebook.github.io/jest/> ## References
{: .-one-column}
- Based on Jest v19.
- <http://facebook.github.io/jest/>
{: .-also-see}