{"id":15552261,"url":"https://github.com/bahmutov/server-logs-example","last_synced_at":"2025-09-28T22:30:23.911Z","repository":{"id":37028467,"uuid":"195909099","full_name":"bahmutov/server-logs-example","owner":"bahmutov","description":"API testing using Cypress.io test runner with server-side logs","archived":false,"fork":false,"pushed_at":"2024-09-28T06:19:22.000Z","size":697,"stargazers_count":7,"open_issues_count":6,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-03T14:14:05.978Z","etag":null,"topics":["api-testing","cypress-example"],"latest_commit_sha":null,"homepage":"https://glebbahmutov.com/blog/api-testing-with-sever-logs/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bahmutov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-09T01:22:48.000Z","updated_at":"2024-09-07T06:04:26.000Z","dependencies_parsed_at":"2023-01-20T11:36:35.035Z","dependency_job_id":"d4f39bea-ce28-450c-9eeb-e092ceed9465","html_url":"https://github.com/bahmutov/server-logs-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fserver-logs-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fserver-logs-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fserver-logs-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fserver-logs-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bahmutov","download_url":"https://codeload.github.com/bahmutov/server-logs-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234565091,"owners_count":18853248,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-testing","cypress-example"],"created_at":"2024-10-02T14:14:05.949Z","updated_at":"2025-09-28T22:30:23.501Z","avatar_url":"https://github.com/bahmutov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# server-logs-example [![renovate-app badge][renovate-badge]][renovate-app] [![CircleCI](https://circleci.com/gh/bahmutov/server-logs-example/tree/master.svg?style=svg\u0026circle-token=b3c1a73d533c11e7f1cf3bf9bdcfd98518f929f1)](https://circleci.com/gh/bahmutov/server-logs-example/tree/master)\n\u003e API testing using Cypress.io test runner with server-side logs\n\nShows how to use [@bahmutov/cy-api](https://github.com/bahmutov/cy-api) plugin and its custom `cy.api` command to perform end-to-end API testing with full server logs. See [cypress/integration/spec.js](cypress/integration/spec.js) file.\n\nRead [\"Black box API testing with server logs\"](https://glebbahmutov.com/blog/api-testing-with-sever-logs/) post.\n\n## Server\n\nServer in [server/index.js](server/index.js) adds numbers passed as query parameters.\n\n```shell\nnpm start\n```\n\nYou can test it from from another terminal using curl or [httpie](https://httpie.org/)\n\n```shell\n$ http ':7000/?a=1\u0026b=-10'\nHTTP/1.1 200 OK\nConnection: keep-alive\nDate: Tue, 09 Jul 2019 01:37:13 GMT\nTransfer-Encoding: chunked\n\n-9\n```\n\n### Server logs\n\nEvery time we call `GET /`, the server logs the input arguments and has several types of verbose logging. See [server/index.js](server/index.js) for entire code, but the important lines are:\n\n```js\n// we will use console.log, util.debuglog and debug modules to log stuff\nconst verbose = require('util').debuglog('verbose')\nconst debug = require('debug')('compute')\n\n// handle \"GET /\" request\nconst { pathname, query } = url.parse(req.url, true)\n// let's just log the basic request parameters\nconsole.log('%s %s pathname %s', req.method, req.url, pathname)\n// and log the parsed query object in verbose mode\n// visible when NODE_DEBUG=verbose is set\nverbose('query is %o', query)\nconst a = parseFloat(query.a)\nconst b = parseFloat(query.b)\nconst sum = a + b\n// \"debug\" log only shows the computation if DEBUG=compute is set\ndebug('%d + %d = %d', a, b, sum)\n```\n\n## End-to-end API tests\n\n![Cypress API test](images/logs.png)\n\nYou can see request and response objects for each `cy.api` command and also see server-side logs, and even query into them by type and namespace. See [cypress/integration/spec.js](cypress/integration/spec.js) file, here is a part of the test:\n\n```js\nit('adds numbers', function () {\n  // first argument - same as \"cy.request\" options\n  // see https://on.cypress.io/request\n  // second argument - optional name for this request\n  cy.api(\n    {\n      url: '/',\n      qs: {\n        a: 2,\n        b: 3\n      }\n    },\n    '2 + 3'\n  ).then(({ body, messages }) =\u003e {\n    // you also have 'status', 'statusText',\n    // 'requestHeaders', 'headers', and 'duration'\n    // we can check the value returned by the API\n    expect(body, 'correct answer').to.equal('5')\n    // you can also inspect messages list and make assertions against it\n    const utilLogs = filter(messages, { type: 'util.debuglog' })\n    const queryLog = find(utilLogs, m =\u003e /query is/.test(m.message))\n    expect(queryLog)\n      .property('message')\n      .to.include(\"{ a: '2', b: '3' }\")\n  })\n})\n```\n\nSee more spec files in [cypress/integration](cypress/integration) folder.\n\n## See\n\n- Read [\"Capture all the logs\"](https://glebbahmutov.com/blog/capture-all-the-logs/)\n- [@bahmutov/cy-api](https://github.com/bahmutov/cy-api) plugin provides `cy.api` command\n\n[renovate-badge]: https://img.shields.io/badge/renovate-app-blue.svg\n[renovate-app]: https://renovateapp.com/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fserver-logs-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbahmutov%2Fserver-logs-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fserver-logs-example/lists"}