Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expectation message formatting #27

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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