{"id":19475456,"url":"https://github.com/artdecocode/assert-throws","last_synced_at":"2026-05-15T17:04:47.142Z","repository":{"id":57185051,"uuid":"115836029","full_name":"artdecocode/assert-throws","owner":"artdecocode","description":"A JavaScript assertion method to check if a function throws.","archived":false,"fork":false,"pushed_at":"2019-04-30T16:23:48.000Z","size":138,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-08T06:47:28.798Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://artdeco.bz/assert-throws","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/artdecocode.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-31T01:45:50.000Z","updated_at":"2019-05-03T20:51:48.000Z","dependencies_parsed_at":"2022-09-14T09:01:15.376Z","dependency_job_id":null,"html_url":"https://github.com/artdecocode/assert-throws","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artdecocode%2Fassert-throws","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artdecocode%2Fassert-throws/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artdecocode%2Fassert-throws/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artdecocode%2Fassert-throws/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/artdecocode","download_url":"https://codeload.github.com/artdecocode/assert-throws/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240699043,"owners_count":19843508,"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-11-10T19:32:56.049Z","updated_at":"2026-05-15T17:04:42.110Z","avatar_url":"https://github.com/artdecocode.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# assert-throws\n\n[![npm version](https://badge.fury.io/js/assert-throws.svg)](https://npmjs.org/package/assert-throws)\n\n`assert-throws` is an assertion method for Node.js which checks if a synchronous or asynchronous function throws. It can also compare properties of the error (such as `message`, `code` and `stack` and any other) with expected ones using string strict equality, a regular expression, or a function.\n\n```\nyarn add -D assert-throws\n```\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/0.svg?sanitize=true\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n## Table of Contents\n\n- [Table of Contents](#table-of-contents)\n- [API](#api)\n  * [`async throws(config: Config): Error`](#async-throwsconfig-fn-functionargs-anyanycontext-anymessage-assertioncode-assertionerror-assertionprop-assertion-error)\n    * [`_assertThrows.Assertion`](#type-_assertthrowsassertion)\n    * [`_assertThrows.Config`](#type-_assertthrowsconfig)\n  * [Arguments](#arguments)\n  * [Context](#context)\n- [Assertions](#assertions)\n  * [String Equality](#string-equality)\n  * [RegExp Test](#regexp-test)\n  * [Function Evaluation](#function-evaluation)\n  * [Multiple Assertions](#multiple-assertions)\n  * [Strict Equality](#strict-equality)\n- [Copyright](#copyright)\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/1.svg?sanitize=true\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n## API\n\nThe package exports the default `throws` function.\n\n```js\nimport throws from 'assert-throws'\n```\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/2.svg?sanitize=true\" width=\"25\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n### `async throws(`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;`config: {`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;`fn: function,`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;`args?: any|any[],`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;`context?: any,`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;`message?: Assertion,`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;`code?: Assertion,`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;`error?: Assertion,`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;`[prop]?: Assertion,`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;`},`\u003cbr/\u003e`): Error`\n\nChecks if a function throws an error. As a minimum, the function should be passed in the `fn` property. If the assertion passes, the method returns the error which was thrown by the tested function.\n\n`!(string|RegExp|!Function)` __\u003ca name=\"type-_assertthrowsassertion\"\u003e`_assertThrows.Assertion`\u003c/a\u003e__: An assertion to perform.\n\n__\u003ca name=\"type-_assertthrowsconfig\"\u003e`_assertThrows.Config`\u003c/a\u003e__: Parameters to the `assert-throws` method.\n\n|  Name   |                               Type                               |                                     Description                                      |\n| ------- | ---------------------------------------------------------------- | ------------------------------------------------------------------------------------ |\n| __fn*__ | \u003cem\u003e!Function\u003c/em\u003e                                               | The function to test, either sync or async.                                          |\n| args    | \u003cem\u003e(* \\\\| !Array\u0026lt;*\u0026gt;)\u003c/em\u003e                                 | The arguments or single argument to pass to the function.                            |\n| context | \u003cem\u003e*\u003c/em\u003e                                                       | The context in which to execute the function. Global context will be set by default. |\n| message | \u003cem\u003e[_assertThrows.Assertion](#type-_assertthrowsassertion)\u003c/em\u003e | A string, regex, or function to test the message.                                    |\n| code    | \u003cem\u003e[_assertThrows.Assertion](#type-_assertthrowsassertion)\u003c/em\u003e | A string, regex, or function to test the code.                                       |\n| stack   | \u003cem\u003e[_assertThrows.Assertion](#type-_assertthrowsassertion)\u003c/em\u003e | A string, regex, or function to test the stack.                                      |\n| prop    | \u003cem\u003e[_assertThrows.Assertion](#type-_assertthrowsassertion)\u003c/em\u003e | A string, regex, or function to test any other property of the error.                |\n| error   | \u003cem\u003eError\u003c/em\u003e                                                   | An error to perform strict comparison against.                                       |\n\n```js\nimport throws from 'assert-throws'\n\nasync function testThrows() {\n  await new Promise(r =\u003e setTimeout(r, 100))\n  throw new Error('test-error')\n}\n\n(async function example() {\n  // 1. TEST a throwing function.\n  await throws({\n    fn: testThrows,\n  })\n\n  // 2. TEST a throwing function (alternative syntax).\n  await throws({\n    async fn(){\n      await testThrows()\n    },\n  })\n\n  console.log('Everything passed.')\n})()\n```\n\n```\nEverything passed.\n```\n\n```js\nimport throws from 'assert-throws'\n\nconst testThrows = async () =\u003e {\n  await new Promise(r =\u003e setTimeout(r, 100))\n}\n\n(async function example() {\n  try {\n    await throws({\n      fn: testThrows,\n    })\n  } catch ({ stack }) {\n    console.log(stack)\n  }\n})()\n```\n\n```\nError: Function testThrows should have thrown.\n    at example (/Users/zavr/adc/assert-throws/example/throws-fail.js:9:11)\n    at Object.\u003canonymous\u003e (/Users/zavr/adc/assert-throws/example/throws-fail.js:15:3)\n    at Module.r._compile (/Users/zavr/adc/assert-throws/node_modules/alamode/depack/depack-lib.js:836:20)\n    at Object.l.(anonymous function).E._extensions.(anonymous function) [as .js] (/Users/zavr/adc/assert-throws/node_modules/alamode/depack/depack-lib.js:839:7)\n```\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/3.svg?sanitize=true\" width=\"25\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n### Arguments\n\nTo pass arguments to the tested function, the `args` properties can be used.\n\n```js\nimport throws from 'assert-throws'\n\nconst testThrows = async (message, shouldThrow = true) =\u003e {\n  if (shouldThrow) throw new Error(message)\n}\n\n(async function example() {\n  try {\n    // 1. TEST that a function with arguments throws (pass).\n    await throws({\n      fn: testThrows,\n      args: ['An argument in the array'],\n    })\n\n    // 2. TEST that a function with an argument throws (pass).\n    await throws({\n      fn: testThrows,\n      args: 'A single argument',\n    })\n\n    // 2. TEST that a function with arguments throws (alternative) (pass).\n    await throws({\n      async fn() {\n        await testThrows('A single argument')\n      },\n    })\n\n    // 3. TEST that a function with arguments throws (fail).\n    await throws({\n      fn: testThrows,\n      args: ['An error occurred.', false],\n    })\n  } catch ({ stack }) {\n    console.log(stack)\n  }\n})()\n```\n\n```\nError: Function testThrows should have thrown.\n    at example (/Users/zavr/adc/assert-throws/example/args.js:29:11)\n    at \u003canonymous\u003e\n```\n\n### Context\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/4.svg?sanitize=true\" width=\"25\"\u003e\u003c/a\u003e\u003c/p\u003e\n\nTo pass a context to the function, the `context` properties can be set. Otherwise, it will use the global context, unless it was bound.\n\n```js\nimport throws from 'assert-throws'\n\nasync function testThrows() {\n  if (this.shouldThrow) throw new Error('An error occurred.')\n}\n\n(async function example() {\n  try {\n    // 1. TEST a function with context (pass).\n    await throws({\n      fn: testThrows,\n      context: { shouldThrow: true },\n    })\n\n    // 2.  TEST a function with a context (fail).\n    await throws({\n      fn: testThrows,\n    })\n  } catch ({ stack }) {\n    console.log(stack)\n  }\n})()\n```\n\n```\nError: Function testThrows should have thrown.\n    at example (/Users/zavr/adc/assert-throws/example/context.js:16:11)\n    at \u003canonymous\u003e\n```\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/5.svg?sanitize=true\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n## Assertions\n\nIf a function throws, any of the error properties can be tested. Every property specified in the configuration will test a property of the error, e.g., `message`, `code` and others.\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/6.svg?sanitize=true\" width=\"25\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n### String Equality\n\nIt is possible to check that any property of a thrown error is equal to a given string.\n\n```js\nimport throws from 'assert-throws'\n\nasync function testThrows() {\n  await new Promise(r =\u003e setTimeout(r, 100))\n  throw new Error('test-error')\n}\n\n(async function example() {\n  try {\n    // 1. TEST that a function throws with a string (pass).\n    await throws({\n      fn: testThrows,\n      message: 'test-error',\n    })\n\n    // 2. TEST that a function throws with a string (fail).\n    await throws({\n      fn: testThrows,\n      message: 'wrong-error',\n    })\n  } catch ({ stack }) {\n    console.log(stack)\n  }\n})()\n```\n\n```\nError: testwrong-error\ntest-error != wrong-error\n    at example (/Users/zavr/adc/assert-throws/example/string.js:17:11)\n    at \u003canonymous\u003e\n```\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/7.svg?sanitize=true\" width=\"25\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n\n\n### RegExp Test\n\nMoreover, a regular expression can be used to validate an error's property.\n\n```js\nimport throws from 'assert-throws'\n\nasync function testThrows() {\n  await new Promise(r =\u003e setTimeout(r, 100))\n  const error = new Error('test-error')\n  error.code = 'ENOENT'\n  throw error\n}\n\n(async function example() {\n  try {\n    // 1. TEST that a function throws with a regexp (pass).\n    await throws({\n      fn: testThrows,\n      code: /enoent/i,\n    })\n\n    // 2. TEST that a function throws with a regexp (fail).\n    await throws({\n      fn: testThrows,\n      code: /ENOEXMPL/,\n    })\n  } catch ({ stack }) {\n    console.log(stack)\n  }\n})()\n```\n\n```\nError: ENOENT does not match regular expression /ENOEXMPL/\n    at example (/Users/zavr/adc/assert-throws/example/regexp.js:19:11)\n    at \u003canonymous\u003e\n```\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/8.svg?sanitize=true\" width=\"25\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n\n\n### Function Evaluation\n\nFor more advanced usage, a function can be used to test a property. It will receive the property of the error and should throw when an assertion does not pass.\n\n```js\nimport throws from 'assert-throws'\n\nasync function testThrows() {\n  await new Promise(r =\u003e setTimeout(r, 100))\n  const error = new Error('test-error')\n  throw error\n}\n\n(async function example() {\n  try {\n    // 1. TEST that a function throws with a function (pass).\n    await throws({\n      fn: testThrows,\n      stack(stack) {\n        if (!/at testThrows/.test(stack)) {\n          throw new Error('The function does not have the correct stack.')\n        }\n      },\n    })\n\n    // 2. TEST that a function throws with a function (fail).\n    await throws({\n      fn: testThrows,\n      stack(stack) {\n        if (/anonymous/.test(stack)) {\n          throw new Error('The function has an anonymous call stack line.')\n        }\n      },\n    })\n  } catch ({ stack }) {\n    console.log(stack)\n  }\n})()\n```\n\n```\nError: The function has an anonymous call stack line.\n    at example (/Users/zavr/adc/assert-throws/example/function.js:22:11)\n    at \u003canonymous\u003e\n```\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/9.svg?sanitize=true\" width=\"25\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n### Multiple Assertions\n\nAny number of assertions can be added at the same time, and they will all be executed. However, only the first failing assertion will be presented.\n\n```js\nimport cleanStack from '@artdeco/clean-stack'\nimport throws from 'assert-throws'\n\nasync function testThrows() {\n  const err = new Error('test-error')\n  err.code = 'ENOTEST'\n  err.actual = -1\n  err.expected = Infinity\n  await new Promise(r =\u003e setTimeout(r, 100))\n  err.stack = cleanStack(err.stack)\n  throw err\n}\n\n(async function example() {\n  try {\n    // 1. TEST that a function throws with a regexp (pass).\n    await throws({\n      fn: testThrows,\n      message: 'test-error',\n      code: /TEST/,\n      stack(stack) {\n        if (/Module._compile/.test(stack)) {\n          throw new Error('The stack has a Node.js internal line.')\n        }\n      },\n      actual: -1,\n      expected: Infinity,\n    })\n\n    // 2. TEST that a function throws with a regexp (fail).\n    await throws({\n      fn: testThrows,\n      message: 'test-error',\n      code: /enotest/i,\n      stack(stack) {\n        if (/Module._compile/.test(stack)) {\n          throw new Error('The stack has a Node.js internal line.')\n        }\n      },\n      actual: -1,\n      expected: -Infinity,\n    })\n  } catch ({ stack }) {\n    console.log(stack)\n  }\n})()\n```\n\n```\nError: -Infinity\nInfinity != -Infinity\n    at example (/Users/zavr/adc/assert-throws/example/multiple.js:31:11)\n    at \u003canonymous\u003e\n```\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/10.svg?sanitize=true\" width=\"25\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n### Strict Equality\n\n`assert-throws` allows to assert on a strict equality of an error.\n\n```js\nimport throws from 'assert-throws'\n\nconst error = new Error('test-error')\n\nasync function testThrows() {\n  await new Promise(r =\u003e setTimeout(r, 100))\n  throw error\n}\n\n(async function example() {\n  try {\n    // 1. TEST that a function throws the correct error (pass).\n    await throws({\n      fn: testThrows,\n      error,\n    })\n\n    // 1. TEST that a function throws the correct error (fail).\n    await throws({\n      fn: testThrows,\n      error: new Error('Another error.'),\n    })\n  } catch ({ stack }) {\n    console.log(stack)\n  }\n})()\n```\n\n```\nError: Error: test-error is not strict equal to Error: Another error..\n    at example (/Users/zavr/adc/assert-throws/example/strict.js:19:11)\n    at \u003canonymous\u003e\n```\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/11.svg?sanitize=true\" width=\"25\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n## Copyright\n\n\n  (c) [Context Testing](https://contexttesting.com) 2019\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartdecocode%2Fassert-throws","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fartdecocode%2Fassert-throws","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartdecocode%2Fassert-throws/lists"}