项目作者: octokit

项目描述 :
Fixtures for all the octokittens
高级语言: JavaScript
项目地址: git://github.com/octokit/fixtures.git
创建时间: 2017-09-03T00:47:55Z
项目社区:https://github.com/octokit/fixtures

开源协议:MIT License

下载


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.

  1. const https = require("https");
  2. const fixtures = require("@octokit/fixtures");
  3. fixtures.mock("api.github.com/get-repository");
  4. https
  5. .request(
  6. {
  7. method: "GET",
  8. hostname: "api.github.com",
  9. path: "/repos/octokit-fixture-org/hello-world",
  10. headers: {
  11. accept: "application/vnd.github.v3+json",
  12. },
  13. },
  14. (response) => {
  15. console.log("headers:", response.headers);
  16. response.on("data", (data) => console.log(data.toString()));
  17. // logs response from fixture
  18. },
  19. )
  20. .end();

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

  1. const mock = fixtures.mock("api.github.com/get-repository");
  2. // send requests ...
  3. mock.done(); // will throw an error unless all mocked routes have been called
  4. mock.isDone(); // returns true / false
  5. 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

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

Now instead of logging

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

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

  1. Request did not match mock:
  2. {
  3. headers: {
  4. - accept: "application/vnd.github.v3"
  5. + accept: "application/vnd.github.v3+json"
  6. }
  7. }

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