Skip to content

Commit

Permalink
Add expectation message formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuajaco committed Apr 18, 2024
1 parent ab0d4c5 commit b318c6f
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
- run: npm run lint
- run: npm run build
- run: npm run test
env:
FORCE_COLOR: 2
- name: Coveralls
uses: coverallsapp/github-action@v2
with:
Expand Down
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,13 @@ test("custom assertion", async () => {
- [`calls()`](#calls-readonly-call)
- [`hasBeenCalledWith()`](#hasbeencalledwithmatcher-boolean)
- [`hasBeenCalledTimes()`](#hasbeencalledtimestimes-matcher-boolean)
- [`countCalls()`](#countcallsmatcher-number)
- [`reset()`](#reset-void)
- [`resetMocks()`](#resetmocks-void)
- [`resetCalls()`](#resetcalls-void)
- [`ExpectationMessage`](#expectationmessage)
- [`hasBeenCalledWith()`](#hasbeencalledwithmockserver-matcher-string)
- [`hasBeenCalledTimes()`](#hasbeencalledtimesmockserver-times-matcher-string)
- [`Options`](#options)
- [`Request`](#request)
- [`Matcher`](#matcher)
Expand Down Expand Up @@ -575,6 +579,8 @@ If `matcher` is a `string` or `RegExp`, it will be used to match the request pat

Returns `true` if the route has been called `times` times with the given `matcher`, `false` otherwise.

#### Example

```ts
mockServer.get("/test", { status: 200 });

Expand All @@ -589,6 +595,32 @@ console.log(mockServer.hasBeenCalledTimes(1, { path: "/test" })); // true

---

### `countCalls(matcher): number`

Count the number of times the server was called with the given `matcher`.

| Param | Type | Default |
| ------- | --------------------------------------------- | ------- |
| matcher | `string` \| `RegExp` \| [`Matcher`](#matcher) | - |

If `matcher` is a `string` or `RegExp`, it will be used to match the request path.

Returns the number of times the server has been called with the given `matcher`.

#### Example

```ts
mockServer.get("/test", { status: 200 });

console.log(mockServer.countCalls({ path: "/test" })); // 0

await fetch("http://localhost:3000/test");

console.log(mockServer.countCalls({ path: "/test" })); // 1
```

---

### `reset(): void`

Reset all mocks and calls.
Expand Down Expand Up @@ -650,6 +682,49 @@ mockServer.resetCalls();
console.log(mockServer.calls()); // []
```

## `ExpectationMessage`

### `hasBeenCalledWith(mockServer, matcher): string`

Format an expectation message for [`hasBeenCalledWith()`](#hasbeencalledwithmatcher-boolean).

| Param | Type | Default |
| ---------- | --------------------------- | ------- |
| mockServer | [`MockServer`](#mockserver) | - |
| matcher | [`Matcher`](#matcher) | - |

Returns a string with the formatted expectation message.

#### Example

```ts
if (!mockServer.hasBeenCalledWith(matcher)) {
throw new Error(ExpectationMessage.hasBeenCalledWith(mockServer, matcher));
}
```

### `hasBeenCalledTimes(mockServer, times, matcher): string`

Format an expectation message for [`hasBeenCalledTimes()`](#hasbeencalledtimestimes-matcher-boolean).

| Param | Type | Default |
| ---------- | --------------------------- | ------- |
| mockServer | [`MockServer`](#mockserver) | - |
| times | number | - |
| matcher | [`Matcher`](#matcher) | - |

Returns a string with the formatted expectation message.

#### Example

```ts
if (!mockServer.hasBeenCalledTimes(mockServer, 2, matcher)) {
throw new Error(
ExpectationMessage.hasBeenCalledTimes(mockServer, 2, matcher),
);
}
```

## `Options`

Object with the following properties:
Expand Down
86 changes: 78 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"@types/express": "^4.17.21",
"body-parser": "^1.20.2",
"deep-equal": "^2.2.3",
"express": "^4.18.2"
"express": "^4.18.2",
"jest-diff": "^29.7.0"
},
"devDependencies": {
"@types/deep-equal": "^1.0.4",
Expand Down
Loading

0 comments on commit b318c6f

Please sign in to comment.