{"id":15062126,"url":"https://github.com/mediacomem/mocha-api-errors","last_synced_at":"2026-01-02T06:04:50.561Z","repository":{"id":57299736,"uuid":"112324123","full_name":"MediaComem/mocha-api-errors","owner":"MediaComem","description":"Print full API responses in your Mocha tests when you get an unexpected responses.","archived":false,"fork":false,"pushed_at":"2018-05-18T08:03:10.000Z","size":60,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-15T12:14:49.324Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MediaComem.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-28T11:00:24.000Z","updated_at":"2021-01-07T16:17:33.000Z","dependencies_parsed_at":"2022-08-26T17:00:53.619Z","dependency_job_id":null,"html_url":"https://github.com/MediaComem/mocha-api-errors","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MediaComem%2Fmocha-api-errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MediaComem%2Fmocha-api-errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MediaComem%2Fmocha-api-errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MediaComem%2Fmocha-api-errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MediaComem","download_url":"https://codeload.github.com/MediaComem/mocha-api-errors/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243725633,"owners_count":20337670,"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":[],"created_at":"2024-09-24T23:30:53.614Z","updated_at":"2026-01-02T06:04:45.543Z","avatar_url":"https://github.com/MediaComem.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mocha-api-errors\n\nPrint full API responses in your [Mocha](https://mochajs.org) tests when you get an unexpected response.\n\n[![npm version](https://badge.fury.io/js/mocha-api-errors.svg)](https://badge.fury.io/js/mocha-api-errors)\n[![Dependency Status](https://gemnasium.com/badges/github.com/MediaComem/mocha-api-errors.svg)](https://gemnasium.com/github.com/MediaComem/mocha-api-errors)\n[![Build Status](https://travis-ci.org/MediaComem/mocha-api-errors.svg?branch=master)](https://travis-ci.org/MediaComem/mocha-api-errors)\n[![Coverage Status](https://coveralls.io/repos/github/MediaComem/mocha-api-errors/badge.svg?branch=master)](https://coveralls.io/github/MediaComem/mocha-api-errors?branch=master)\n[![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE.txt)\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [Creating a custom reporter](#creating-a-custom-reporter)\n- [Configuration](#configuration)\n- [Using a custom reporter programmatically](#using-a-custom-reporter-programmatically)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\nDeveloped at the [Media Engineering Institute](http://mei.heig-vd.ch) ([HEIG-VD](https://heig-vd.ch)).\n\n\n\n## Installation\n\n```bash\n$\u003e npm install mocha-api-errors\n```\n\n\n\n## Usage\n\nIt can be quite annoying to write assertions on HTTP responses in tests,\nbecause you get so little information from the assertion error:\n\n```js\nexpect(res.status, 'res.status').to.equal(200);\n\n// AssertionError: expected 400 to equal 200\n//\n// res.status\n// + expected - actual\n//\n// -400\n// +200\n//\n// at Context.it (spec/index.spec.js:12:20)\n```\n\nOkay, the server responded with the HTTP 400 Bad Request status code, but you\nhave no idea why. It would be great to know what was the response from the\nserver. Should you add code to your tests to print it if an error occurs?\nShould you write a complex custom Mocha matcher for HTTP responses?\n\nRun Mocha with the **mocha-api-errors** reporter, and it will include the HTTP\nresponse in the error's stack trace so that you will see it if any assertion\nfails:\n\n```bash\n$\u003e mocha --reporter mocha-api-errors spec/**/*.js\n```\n\nYou also need to attach the offending HTTP response to the test's `res`\nproperty. Here's an example with\n[supertest](https://github.com/visionmedia/supertest):\n\n```js\nconst supertest = require('supertest');\n\nconst app = require('./my-express-app');\n\nit('should work', async function() {\n  const res = this.test.res = await supertest(app).get('/test');\n  expect(res.status, 'res.status').to.equal(200);\n});\n```\n\nHere's the output you might get with this configuration.  Note the HTTP\nresponse shown before the stack trace:\n\n```js\n// AssertionError: expected 400 to equal 200\n//\n// res.status\n// + expected - actual\n//\n// -400\n// +200\n//\n// HTTP/1.1 400 Bad Request\n// x-powered-by: Express\n// content-type: application/json; charset=utf-8\n// content-length: 13\n// etag: W/\"d-pedE0BZFQNM7HX6mFsKPL6l+dUo\"\n// date: Tue, 28 Nov 2017 08:58:02 GMT\n// connection: close\n//\n// {\n//   \"why\": \"because\"\n// }\n// at Context.it (spec/index.spec.js:12:20)\n```\n\nNow you know exactly what the problem is (provided that your server is kind\nenough to give you that information).\n\n\n\n## Creating a custom reporter\n\nThe **mocha-api-errors** reporter extends Mocha's spec reporter, but you can extend any other reporter by using its `extend` function:\n\n```js\nconst mocha = require('mocha');\nconst MochaApiErrorsReporter = require('mocha-api-errors');\n\n// Extend any existing reporter with API error enrichment:\nconst CustomReporter = MochaApiErrorsReporter.extend(mocha.reporters.Dot);\n```\n\nThis assumes that you extend Mocha's base reporter, which adds test failures to\nthe `this.failures` array in the reporter.\n\n\n\n## Configuration\n\nThe following options can be passed as the second argument to `extend`:\n\n```js\nconst CustomReporter = MochaApiErrorsReporter.extend(mocha.reporters.Dot, { /* options... */ });\n```\n\n* `responseProperty` - The name of the test property where the HTTP response is expected. Defaults to `res`.\n* `getResponse(test, err)` - A function that should return the HTTP response of a test when an error occurs.\n  It will be called with the reporter as context (`this`), and with the test and error as arguments (the same\n  arguments as Mocha's `fail` runner event). It defaults to simply returning the test property defined by the\n  `responseProperty` option.\n* `getError` - A function that should return the error to enrich when an error occurs in a test.\n  It will be called with the reporter as context (`this`), and with the test and error as arguments (the same\n  arguments as Mocha's `fail` runner event). It defaults to returning the `err` property of the last failure\n  (taken from the `this.failures` array of the base reporter).\n\n\n\n## Using a custom reporter programmatically\n\nTo use your custom reporter without publishing it as a package,\nyou might need to [run Mocha programmatically](https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically):\n\n```js\nconst Mocha = require('mocha');\nconst fs = require('fs');\nconst path = require('path');\n\n// Instantiate a Mocha instance with your custom reporter.\nconst mocha = new Mocha({\n  reporter: require('./custom-reporter')\n});\n\nconst testDir = 'some/dir/test'\n\n// Add each .js file to the mocha instance.\nfs.readdirSync(testDir).filter(function(file){\n  // Only keep the .js files.\n  return file.substr(-3) === '.js';\n}).forEach(function(file){\n  mocha.addFile(\n    path.join(testDir, file)\n  );\n});\n\n// Run the tests.\nmocha.run(function(failures){\n  process.on('exit', function () {\n    // Exit with non-zero status if there were failures.\n    process.exit(failures);\n  });\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmediacomem%2Fmocha-api-errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmediacomem%2Fmocha-api-errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmediacomem%2Fmocha-api-errors/lists"}