Skip to content

WIP: Omit certain paths from mocking #29

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
push:
branches: [master]
pull_request:
branches: [master]
branches: ['*']

jobs:
build:
Expand Down
76 changes: 50 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Main features
- [URL and method matching](#url-and-method-matching)
- [Path parameters matching](#path-parameters-matching)
- [Query params matching](#query-parameters-matching)
- [Wildcard matching](#wildcard-matching)
- [Request assertion](#request-assertion)
- [One-time mocks](#one-time-mocks)
- [Matching order](#matching-order)
Expand Down Expand Up @@ -142,52 +143,50 @@ test('Sign-up form', async () => {

### URL and method matching <a name="url-and-method-matching"/>

Request can be matched by:
Request can most easily be mocked by calling `mockiavelli.mock<HTTP_METHOD>` with endpoint URL:

- providing URL string to `mockiavelli.mock<HTTP_METHOD>` method:
```typescript
mockiavelli.mockGET('/api/users', { status: 200, body: [] });
// GET /api/users => 200

```typescript
mockiavelli.mockGET('/api/users?age=30', {status: 200, body: [....]})
```
mockiavelli.mockPOST('/api/users', { status: 201 });
// POST /api/users => 200
```

- providing matcher object to `mockiavelli.mock<HTTP_METHOD>` method
Alternatively you can provide HTTP method and URL as object to `mockiavelli.mock`:

```typescript
mockiavelli.mockGET({
url: '/api/users',
query: { age: '30' }
}, {status: 200, body: [....]})
```
```typescript
mockiavelli.mock(
{ method: 'GET', url: '/api/users' },
{ status: 200, body: [] }
);
// GET /api/users => 200
```

- providing full matcher object `mockiavelli.mock` method
If HTTP method is not specified, any request that matches provided URL will be mocked.

```typescript
mockiavelli.mock({
method: 'GET',
url: '/api/users',
query: { age: '30' }
}, {status: 200, body: [...]})
```
```typescript
mockiavelli.mock('/api/users', { status: 500 });
// GET /api/users => 500
// POST /api/users => 500
```

### Path parameters matching <a name="path-parameters-matching"/>

Path parameters in the URL can be matched using `:param` notation, thanks to [path-to-regexp](https://www.npmjs.com/package/path-to-regexp) library.
Path parameters in the URL can be matched using `:param` notation, thanks to [url-pattern](https://www.npmjs.com/package/url-pattern) library.

If mock matches the request, those params are exposed in `request.params` property.

```typescript
const getUserMock = mockiavelli.mockGET('/api/users/:userId', { status: 200 });

// GET /api/users/1234 => 200
// GET /api/users => 404
// GET /api/users/1234/categories => 404

console.log(await getUserMock.waitForRequest());
// { params: {userId : "12345"}, path: "/api/users/12345", ... }
```

Mockiavelli uses

### Query params matching <a name="query-parameters-matching"/>

Mockiavelli supports matching requests by query parameters. All defined params are then required to match the request, but excess params are ignored:
Expand All @@ -211,6 +210,31 @@ mockiavelli.mockGET(
// GET /api/users?status=active&status=blocked => 200
```

### Wildcard matching

Use "\*" wildcard in URL to match any portion of path.

```typescript
mockiavelli.mockGET('/api/users/*', { status: 200 });
// GET /api/users/123 => 200
// GET /api/users/123/comments => 200
```

It can be mixed with both path and query parameters matching. For the latter provide matcher as object to avoid wildcard matching query part of the URL.

```typescript
mockiavelli.mockGET('*/api/users/:userId', { status: 200 });
// GET /v1/api/users/123 => 200
// GET /v2/api/users/123 => 200

mockiavelli.mockGET(
{ url: '/api/users/*', query: { name: 'Bob' } },
{ status: 200 }
);
// GET /api/users/?name=Bob => 200
// GET /api/users/active?name=Bob => 200
```

### Request assertion <a name="request-assertion"/>

`mockiavelli.mock<HTTP_METHOD>` and `mockiavelli.mock` methods return an instance of `Mock` class that records all requests the matched given mock.
Expand Down Expand Up @@ -406,9 +430,9 @@ Respond all requests of matching `matcher` with provided `response`.

###### Arguments

- `matcher` _(object)_ matches request with mock.
- `method: string` - any valid HTTP method
- `matcher` _(string | object)_ URL string or object with following properties:
- `url: string` - can be provided as path (`/api/endpoint`) or full URL (`http://example.com/endpoint`) for CORS requests. Supports path parameters (`/api/users/:user_id`)
- `method?: string` - any valid HTTP method. If not provided, will match any HTTP method.
- `query?: object` object literal which accepts strings and arrays of strings as values, transformed to queryString
- `response` _(object | function)_ content of mocked response. Can be a object or a function returning object with following properties:
- `status: number`
Expand Down
Loading