Skip to content

Commit 0d52cad

Browse files
docs: ✏️ JSDoc blocks
1 parent 6bd589d commit 0d52cad

9 files changed

Lines changed: 170 additions & 126 deletions

File tree

‎packages/jest-either/README.md‎

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -12,125 +12,6 @@ Additional matchers for [Jest](https://jestjs.io) making it easier to test `Eith
1212
yarn add @pacote/jest-either
1313
```
1414

15-
## Usage
16-
17-
```typescript
18-
import matchers from '@pacote/jest-either'
19-
20-
expect.extend(matchers)
21-
```
22-
23-
### `.toBeEither()`
24-
25-
```typescript
26-
import { left, right } from 'fp-ts/lib/Either'
27-
28-
test('passes when value is an Either', () => {
29-
expect(left(true)).toBeEither()
30-
expect(right(true)).toBeEither()
31-
})
32-
33-
test('passes when value is not an Either', () => {
34-
expect(undefined).not.toBeEither()
35-
})
36-
```
37-
38-
### `.toBeRight()`
39-
40-
```typescript
41-
import { left, right } from 'fp-ts/lib/Either'
42-
43-
test('passes when Either is a right', () => {
44-
const actual = right({ test: 'ok' })
45-
expect(actual).toBeRight()
46-
})
47-
48-
test('passes when Either is a left', () => {
49-
const actual = left(Error())
50-
expect(actual).not.toBeRight()
51-
})
52-
```
53-
54-
### `.toBeLeft()`
55-
56-
```typescript
57-
import { left, right } from 'fp-ts/lib/Either'
58-
59-
test('passes when Either is a left', () => {
60-
const actual = left(Error())
61-
expect(actual).toBeLeft()
62-
})
63-
64-
test('passes when Either is a right', () => {
65-
const actual = right({ test: 'ok' })
66-
expect(actual).not.toBeLeft()
67-
})
68-
```
69-
70-
### `.toEqualRight(value)`
71-
72-
```typescript
73-
import { right } from 'fp-ts/lib/Either'
74-
75-
test('passes when right of Either equals a value', () => {
76-
const actual = right({ test: 'ok' })
77-
expect(actual).toEqualRight({ test: 'ok' })
78-
})
79-
80-
test('passes when right of Either does not equal a value', () => {
81-
const actual = right({ test: 'unexpected' })
82-
expect(actual).not.toEqualRight({ test: 'ok' })
83-
})
84-
```
85-
86-
### `.toEqualLeft(value)`
87-
88-
```typescript
89-
import { left } from 'fp-ts/lib/Either'
90-
91-
test('passes when left of Either equals a value', () => {
92-
const actual = left(Error('message'))
93-
expect(actual).toEqualLeft(Error('message'))
94-
})
95-
96-
test('passes when left of Either does not equal a value', () => {
97-
const actual = left(Error('unexpected'))
98-
expect(actual).not.toEqualLeft(Error('message'))
99-
})
100-
```
101-
102-
### `.toMatchRight(object)`
103-
104-
```typescript
105-
import { right } from 'fp-ts/lib/Either'
106-
107-
test('passes when right of Either matches an object', () => {
108-
const actual = right({ test: 'ok', foo: 'bar' })
109-
expect(actual).toMatchRight({ test: 'ok' })
110-
})
111-
112-
test('passes when right of Either does not match an object', () => {
113-
const actual = right({ test: 'unexpected', foo: 'bar' })
114-
expect(actual).not.toMatchRight({ test: 'ok' })
115-
})
116-
```
117-
118-
### `.toMatchLeft(object)`
119-
120-
```typescript
121-
import { left } from 'fp-ts/lib/Either'
122-
123-
test('passes when left of Either matches an object', () => {
124-
const actual = left({ test: 'ok', foo: 'bar' })
125-
expect(actual).toMatchLeft({ test: 'ok' })
126-
})
127-
128-
test('passes when left of Either does not match an object', () => {
129-
const actual = left({ test: 'unexpected', foo: 'bar' })
130-
expect(actual).not.toMatchLeft({ test: 'ok' })
131-
})
132-
```
133-
13415
## License
13516

13617
MIT © [Luís Rodrigues](https://goblindegook.com).

‎packages/jest-either/src/index.ts‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
1+
import type { MatchersObject } from './shared/types'
12
import { toBeEither } from './toBeEither'
23
import { toBeLeft } from './toBeLeft'
34
import { toBeRight } from './toBeRight'
45
import { toEqualLeft } from './toEqualLeft'
56
import { toEqualRight } from './toEqualRight'
67
import { toMatchLeft } from './toMatchLeft'
78
import { toMatchRight } from './toMatchRight'
8-
import type { MatchersObject } from './shared/types'
99

10-
const matchers = {
10+
export { toBeEither } from './toBeEither'
11+
export { toBeLeft } from './toBeLeft'
12+
export { toBeRight } from './toBeRight'
13+
export { toEqualLeft } from './toEqualLeft'
14+
export { toEqualRight } from './toEqualRight'
15+
export { toMatchLeft } from './toMatchLeft'
16+
export { toMatchRight } from './toMatchRight'
17+
18+
/**
19+
* Collection of Jest matchers that assert on `Either` values.
20+
*
21+
* @returns Matchers ready to be passed into `expect.extend`.
22+
*
23+
* @example
24+
* ```typescript
25+
* import matchers from '@pacote/jest-either'
26+
*
27+
* expect.extend(matchers)
28+
* ```
29+
*/
30+
export default {
1131
toBeEither,
1232
toBeLeft,
1333
toBeRight,
1434
toEqualLeft,
1535
toEqualRight,
1636
toMatchLeft,
1737
toMatchRight,
18-
}
19-
20-
export default matchers as MatchersObject
38+
} as MatchersObject

‎packages/jest-either/src/toBeEither.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ const failMessage = (actual: unknown) => () =>
2424
'',
2525
)}\n\nExpected Either, received:\n ${printReceived(actual)}`
2626

27+
/**
28+
* Asserts that the received value is an `Either` instance.
29+
*
30+
* @example
31+
* ```typescript
32+
* import { left, right } from 'fp-ts/lib/Either'
33+
*
34+
* test('passes when value is an Either', () => {
35+
* expect(left(true)).toBeEither()
36+
* expect(right(true)).toBeEither()
37+
* })
38+
*
39+
* test('passes when value is not an Either', () => {
40+
* expect(undefined).not.toBeEither()
41+
* })
42+
* ```
43+
*/
2744
export function toBeEither(actual: unknown): MatcherResult {
2845
const pass = isEither(actual)
2946
return {

‎packages/jest-either/src/toBeLeft.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ const notEitherMessage = (actual: unknown) => () =>
3232
'',
3333
)}\n\nExpected value to be an Either.\n Received: ${printReceived(actual)}`
3434

35+
/**
36+
* Asserts that the received `Either` is a `Left`.
37+
*
38+
* @example
39+
* ```typescript
40+
* import { left, right } from 'fp-ts/lib/Either'
41+
*
42+
* test('passes when Either is a left', () => {
43+
* const actual = left(Error())
44+
* expect(actual).toBeLeft()
45+
* })
46+
*
47+
* test('passes when Either is a right', () => {
48+
* const actual = right({ test: 'ok' })
49+
* expect(actual).not.toBeLeft()
50+
* })
51+
* ```
52+
*/
3553
export function toBeLeft(actual: unknown): MatcherResult {
3654
if (!isEither(actual)) {
3755
return {

‎packages/jest-either/src/toBeRight.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ const notEitherMessage = (actual: unknown) => () =>
3232
'',
3333
)}\n\nExpected value to be an Either.\n Received: ${printReceived(actual)}`
3434

35+
/**
36+
* Asserts that the received `Either` is a `Right`.
37+
*
38+
* @example
39+
* ```typescript
40+
* import { left, right } from 'fp-ts/lib/Either'
41+
*
42+
* test('passes when Either is a right', () => {
43+
* const actual = right({ test: 'ok' })
44+
* expect(actual).toBeRight()
45+
* })
46+
*
47+
* test('passes when Either is a left', () => {
48+
* const actual = left(Error())
49+
* expect(actual).not.toBeRight()
50+
* })
51+
* ```
52+
*/
3553
export function toBeRight(actual: unknown): MatcherResult {
3654
if (!isEither(actual)) {
3755
return {

‎packages/jest-either/src/toEqualLeft.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ const notEitherMessage = (expected: unknown, actual: unknown) => () =>
4848
expected,
4949
)}\n Received: ${printReceived(actual)}`
5050

51+
/**
52+
* Asserts that the left side of an `Either` equals an expected value or
53+
* asymmetric matcher.
54+
*
55+
* @typeParam L Type of the expected left value.
56+
*
57+
* @param expected Value or asymmetric matcher that should equal the left side.
58+
*
59+
* @example
60+
* ```typescript
61+
* import { left } from 'fp-ts/lib/Either'
62+
*
63+
* test('passes when left of Either equals a value', () => {
64+
* const actual = left(Error('message'))
65+
* expect(actual).toEqualLeft(Error('message'))
66+
* })
67+
*
68+
* test('passes when left of Either does not equal a value', () => {
69+
* const actual = left(Error('unexpected'))
70+
* expect(actual).not.toEqualLeft(Error('message'))
71+
* })
72+
* ```
73+
*/
5174
export function toEqualLeft<L>(
5275
actual: unknown,
5376
expected: L | AsymmetricMatcher,

‎packages/jest-either/src/toEqualRight.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ const notEitherMessage = (expected: unknown, actual: unknown) => () =>
4747
expected,
4848
)}\n Received: ${printReceived(actual)}`
4949

50+
/**
51+
* Asserts that the right side of an `Either` equals an expected value or
52+
* asymmetric matcher.
53+
*
54+
* @typeParam R Type of the expected right value.
55+
*
56+
* @param expected Value or asymmetric matcher that should equal the right side.
57+
*
58+
* @example
59+
* ```typescript
60+
* import { right } from 'fp-ts/lib/Either'
61+
*
62+
* test('passes when right of Either equals a value', () => {
63+
* const actual = right({ test: 'ok' })
64+
* expect(actual).toEqualRight({ test: 'ok' })
65+
* })
66+
*
67+
* test('passes when right of Either does not equal a value', () => {
68+
* const actual = right({ test: 'unexpected' })
69+
* expect(actual).not.toEqualRight({ test: 'ok' })
70+
* })
71+
* ```
72+
*/
5073
export function toEqualRight<R>(
5174
actual: unknown,
5275
expected: R | AsymmetricMatcher,

‎packages/jest-either/src/toMatchLeft.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@ const notEitherMessage = (expected: unknown, actual: unknown) => () =>
5656
expected,
5757
)}\n Received: ${printReceived(actual)}`
5858

59+
/**
60+
* Asserts that the left side of an `Either` matches a value, partial object, or
61+
* regular expression.
62+
*
63+
* @typeParam L Type of the left value contained in the `Either`.
64+
*
65+
* @param expected Pattern, partial object, or asymmetric matcher for the left.
66+
*
67+
* @example
68+
* ```typescript
69+
* import { left } from 'fp-ts/lib/Either'
70+
*
71+
* test('passes when left of Either matches an object', () => {
72+
* const actual = left({ test: 'ok', foo: 'bar' })
73+
* expect(actual).toMatchLeft({ test: 'ok' })
74+
* })
75+
*
76+
* test('passes when left of Either does not match an object', () => {
77+
* const actual = left({ test: 'unexpected', foo: 'bar' })
78+
* expect(actual).not.toMatchLeft({ test: 'ok' })
79+
* })
80+
* ```
81+
*/
5982
export function toMatchLeft(
6083
actual: unknown,
6184
expected: RegExp | AsymmetricMatcher,

‎packages/jest-either/src/toMatchRight.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { Either } from 'fp-ts/lib/Either'
22
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils'
3+
import { isEither } from './shared/isEither'
34
import {
45
type AsymmetricMatcher,
56
isAsymmetricMatcher,
67
matchObject,
78
matchString,
8-
rightPredicate,
9+
rightPredicate
910
} from './shared/predicates'
1011
import { printReceivedRight } from './shared/print'
11-
import { isEither } from './shared/isEither'
1212
import type { MatcherResult } from './shared/types'
1313

1414
declare global {
@@ -56,6 +56,29 @@ const notEitherMessage = (expected: unknown, actual: unknown) => () =>
5656
expected,
5757
)}\n Received: ${printReceived(actual)}`
5858

59+
/**
60+
* Asserts that the right side of an `Either` matches a value, partial object,
61+
* or regular expression.
62+
*
63+
* @typeParam R Type of the right value contained in the `Either`.
64+
*
65+
* @param expected Pattern, partial object, or asymmetric matcher for the right.
66+
*
67+
* @example
68+
* ```typescript
69+
* import { right } from 'fp-ts/lib/Either'
70+
*
71+
* test('passes when right of Either matches an object', () => {
72+
* const actual = right({ test: 'ok', foo: 'bar' })
73+
* expect(actual).toMatchRight({ test: 'ok' })
74+
* })
75+
*
76+
* test('passes when right of Either does not match an object', () => {
77+
* const actual = right({ test: 'unexpected', foo: 'bar' })
78+
* expect(actual).not.toMatchRight({ test: 'ok' })
79+
* })
80+
* ```
81+
*/
5982
export function toMatchRight(
6083
actual: unknown,
6184
expected: RegExp | AsymmetricMatcher,

0 commit comments

Comments
 (0)