Skip to content

Commit

Permalink
Allow unmocked (#58)
Browse files Browse the repository at this point in the history
* added allowUnmocked to base classes

* added allowUnmocked to moctokit

* updated script and autogenerated endpoint-requests

* bumped version

* updated readme
  • Loading branch information
shubhbapna authored Mar 3, 2023
1 parent 2445ff7 commit a5dfa87
Show file tree
Hide file tree
Showing 9 changed files with 853 additions and 838 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Using this library along with [kiegroup/act-js](https://github.com/kiegroup/act-
## Table of Content

- [Moctokit](#moctokit)
- [Specifying which base url to use](#specifying-which-base-url-to-use)
- [Initialization options](#initialization-options)
- [Mock an api](#mock-an-api)
- [Mock the entire endpoint](#mock-the-entire-endpoint)
- [Mock an endpoint for specific parameter(s)](#mock-an-endpoint-for-specific-parameters)
Expand Down Expand Up @@ -43,7 +43,7 @@ mock.rest.repos
.reply({ status: 200, data: { full_name: "it worked" } });
```

### Specifying which base url to use
### Initialization options

By default a moctokit instance uses `https://api.github.com` as the base url

Expand All @@ -57,6 +57,12 @@ You can also specify a base url when a creating a new instance. Useful when you
const moctokit = new Moctokit("http://localhost:8000"); // mocks github apis for http://localhost:8000
```

You can enable other APIs to pass through if there is no exact match

```typescript
const moctokit = new Moctokit(undefined, true); // mocks github apis for https://api.github.com and lets unmatched apis to pass through
```

### Mock an api

You can mock all the github api exactly how [@octokit/rest](https://octokit.github.io/rest.js/v19) library is used to make an actual call to the corresponding api.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kie/mock-github",
"version": "1.0.0",
"version": "1.0.1",
"description": "A bunch of tools to configure and create a local github environment to test your github actions in without having to clutter your github with test repositories, actions or hitting github api rate limits.",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions scripts/endpoint-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ async function generateEndpointRequest() {

newEndpointRequest[scope][
endpoint.id
] = `new MoctokitRequestMocker<"${path}", "${method}">(baseUrl, endpoints["${scope}"]["${endpoint.id}"]).request`;
] = `new MoctokitRequestMocker<"${path}", "${method}">(baseUrl, endpoints["${scope}"]["${endpoint.id}"], allowUnmocked).request`;
});
const content = prettier
.format(
`import {MoctokitRequestMocker} from "@mg/moctokit/request/request-mocker";
import endpoints from "./endpoint-details";
const endpointToMethod = (baseUrl: string) => (${JSON.stringify(
const endpointToMethod = (baseUrl: string, allowUnmocked = false) => (${JSON.stringify(
newEndpointRequest
)})
export default endpointToMethod`,
{ parser: "typescript" }
)
.replace(
/('|")(new MoctokitRequestMocker<.+>\(baseUrl, endpoints\[.+\]\[.+\]\).request)('|")/g,
/('|")(new MoctokitRequestMocker<.+>\(baseUrl, endpoints\[.+\]\[.+\], allowUnmocked\).request)('|")/g,
"$2"
);

Expand Down
8 changes: 7 additions & 1 deletion src/endpoint-mocker/request/abstract-request-mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { EndpointDetails, Params } from "@mg/endpoint-mocker/endpoint-mocker.typ
export abstract class RequestMocker {
private _endpointDetails: EndpointDetails;
private _baseUrl: string;
private _allowUnmocked: boolean;

constructor(baseUrl: string, endpointDetails: EndpointDetails) {
constructor(baseUrl: string, endpointDetails: EndpointDetails, allowUnmocked = false) {
this._endpointDetails = endpointDetails;
this._baseUrl = baseUrl;
this._allowUnmocked = allowUnmocked;
}

get baseUrl() {
Expand All @@ -18,6 +20,10 @@ export abstract class RequestMocker {
return this._endpointDetails;
}

get allowUnmocked() {
return this._allowUnmocked;
}

protected parseParams(params?: Params) {
const { pathParams, query, requestBody } = this.extractParams(params);

Expand Down
5 changes: 3 additions & 2 deletions src/endpoint-mocker/response/abstract-response-mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export abstract class ResponseMocker<T, S extends number> {
path: string | RegExp,
method: string,
query?: DataMatcherMap,
requestBody?: DataMatcherMap
requestBody?: DataMatcherMap,
allowUnmocked = false
) {
this.scope = nock(baseUrl);
this.scope = nock(baseUrl, { allowUnmocked });
this.responses = [];
this.path = path;
this.query = query;
Expand Down
1,644 changes: 822 additions & 822 deletions src/moctokit/generated/endpoint-request.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/moctokit/moctokit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import endpointToMethod from "@mg/moctokit/generated/endpoint-request";

export class Moctokit {
private _rest;
constructor(baseUrl?: string) {
this._rest = endpointToMethod(baseUrl ?? "https://api.github.com");
constructor(baseUrl?: string, allowUnmocked = false) {
this._rest = endpointToMethod(baseUrl ?? "https://api.github.com", allowUnmocked);
}

get rest() {
Expand Down
8 changes: 5 additions & 3 deletions src/moctokit/request/request-mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export class MoctokitRequestMocker<
Path extends keyof paths,
Method extends keyof paths[Path]
> extends RequestMocker {
constructor(baseUrl: string, endpointDetails: EndpointDetails) {
super(baseUrl, endpointDetails);

constructor(baseUrl: string, endpointDetails: EndpointDetails, allowUnmocked = false) {
super(baseUrl, endpointDetails, allowUnmocked);

// need to bind the instance context to the function. otherwise it is lost during endpointToMethod generation
this.request = this.request.bind(this);
Expand All @@ -22,7 +23,8 @@ export class MoctokitRequestMocker<
path,
this.endpointDetails.method,
query,
requestBody
requestBody,
this.allowUnmocked
);
}
}

0 comments on commit a5dfa87

Please sign in to comment.