23.1.1 • Published 1 month ago

@octokit/fixtures v23.1.1

Weekly downloads
930
License
MIT
Repository
github
Last release
1 month ago

fixtures

Fixtures for all the octokittens

Test

Records requests/responses against the GitHub REST API and stores them as JSON fixtures.

Usage

Currently requires node 8+

fixtures.mock(scenario)

fixtures.mock(scenario) will intercept requests using nock. scenario is a String in the form <host name>/<scenario name>. host name is any folder in scenarios/. scenario name is any filename in the host name folders without the .js extension.

const https = require("https");
const fixtures = require("@octokit/fixtures");

fixtures.mock("api.github.com/get-repository");
https
  .request(
    {
      method: "GET",
      hostname: "api.github.com",
      path: "/repos/octokit-fixture-org/hello-world",
      headers: {
        accept: "application/vnd.github.v3+json",
      },
    },
    (response) => {
      console.log("headers:", response.headers);
      response.on("data", (data) => console.log(data.toString()));
      // logs response from fixture
    },
  )
  .end();

For tests, you can check if all mocks have been satisfied for a given scenario

const mock = fixtures.mock("api.github.com/get-repository");
// send requests ...
mock.done(); // will throw an error unless all mocked routes have been called
mock.isDone(); // returns true / false
mock.pending(); // returns array of pending mocks in the format [<method> <path>]

mock.explain can be used to amend an error thrown by nock if a request could not be matched

const mock = fixtures.mock("api.github.com/get-repository");
const github = new GitHub();
return github.repos
  .get({ owner: "octokit-fixture-org", repo: "hello-world" })
  .catch(mock.explain);

Now instead of logging

Error: Nock: No match for request {
  "method": "get",
  "url": "https://api.github.com/orgs/octokit-fixture-org",
  "headers": {
    "host": "api.github.com",
    "content-length": "0",
    "user-agent": "NodeJS HTTP Client",
    "accept": "application/vnd.github.v3+json"
  }
}

The log shows exactly what the difference between the sent request and the next pending mock is

 Request did not match mock:
 {
   headers: {
-    accept: "application/vnd.github.v3"
+    accept: "application/vnd.github.v3+json"
   }
 }

fixtures.get(scenario)

fixtures.get(scenario) will return the JSON object which is used by nock to mock the API routes. You can use that method to convert the JSON to another format, for example.

fixtures.nock

fixtures.nock is the nock instance used internally by @octokit/fixtures for the http mocking. Use at your own peril :)

License

MIT

23.1.1

1 month ago

23.1.0

1 month ago

23.0.0

2 months ago

22.0.7

4 months ago

22.0.6

2 years ago

22.0.5

2 years ago

22.0.3

2 years ago

22.0.4

2 years ago

22.0.2

3 years ago

22.0.1

3 years ago

22.0.0

3 years ago

21.5.0

3 years ago

21.4.0

3 years ago

21.3.9

3 years ago

21.3.8

3 years ago

21.3.7

3 years ago

21.3.6

3 years ago

21.3.5

3 years ago

21.3.4

3 years ago

21.3.3

3 years ago

21.3.2

3 years ago

21.3.1

3 years ago

21.3.0

3 years ago

21.2.5

3 years ago

21.2.4

4 years ago

21.2.3

4 years ago

21.2.2

4 years ago

21.2.1

4 years ago

21.2.0

4 years ago

21.1.0

4 years ago

21.0.9

4 years ago

21.0.8

4 years ago

21.0.7

4 years ago

21.0.6

4 years ago

21.0.5

4 years ago

21.0.4

4 years ago

21.0.3

4 years ago

21.0.2

4 years ago

21.0.1

4 years ago

21.0.0

4 years ago

19.0.2

4 years ago

20.0.0

4 years ago

19.0.1

4 years ago

19.0.0

4 years ago

18.1.0

4 years ago

18.0.0

4 years ago

17.1.0

4 years ago

17.0.0

4 years ago

16.1.2

5 years ago

16.1.1

5 years ago

16.1.0

5 years ago

16.0.0

5 years ago

15.0.2

5 years ago

15.0.1

5 years ago

15.0.0

5 years ago

14.0.5

5 years ago

14.0.4

5 years ago

14.0.3

5 years ago

14.0.2

5 years ago

14.0.1

5 years ago

14.0.0

5 years ago

13.0.0

5 years ago

12.0.0

5 years ago

11.0.0

5 years ago

10.1.4

5 years ago

10.1.3

5 years ago

10.1.2

5 years ago

10.1.1

6 years ago

10.1.0

6 years ago

10.0.0

6 years ago

9.2.2

6 years ago

9.2.1

6 years ago

9.2.0

6 years ago

9.1.2

6 years ago

9.1.1

6 years ago

9.1.0

6 years ago

9.0.0

6 years ago

8.6.0

6 years ago

8.5.0

6 years ago

8.4.0

6 years ago

8.3.1

6 years ago

8.3.0

6 years ago

8.2.0

6 years ago

8.1.0

6 years ago

8.0.0

6 years ago

7.4.0

6 years ago

7.3.1

6 years ago

7.3.0

6 years ago

7.2.0

6 years ago

7.1.0

6 years ago

7.0.2

6 years ago

7.0.1

6 years ago

7.0.0

6 years ago

6.5.0

6 years ago

6.4.1

6 years ago

6.4.0

6 years ago

6.3.0

6 years ago

6.2.1

6 years ago

6.2.0

6 years ago

6.1.0

6 years ago

6.0.1

6 years ago

6.0.0

6 years ago

5.5.2

6 years ago

5.5.1

6 years ago

5.5.0

6 years ago

5.4.1

6 years ago

5.4.0

6 years ago

5.3.0

6 years ago

5.2.0

6 years ago

5.1.0

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.4.0

7 years ago

4.3.0

7 years ago

4.2.1

7 years ago

4.2.0

7 years ago

4.1.0

7 years ago

4.0.0

7 years ago

3.1.0

7 years ago

3.0.0

7 years ago

2.4.0

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.9.1

7 years ago

1.9.0

7 years ago

1.8.0

7 years ago

1.7.2

7 years ago

1.7.1

7 years ago

1.7.0

7 years ago

1.6.0

7 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago