{"id":13769628,"url":"https://github.com/ballercat/jest-plugin-must-assert","last_synced_at":"2025-04-12T15:31:32.826Z","repository":{"id":42634783,"uuid":"179722760","full_name":"ballercat/jest-plugin-must-assert","owner":"ballercat","description":"A jest plugin to ensure assertions happen within all tests at runtime","archived":false,"fork":false,"pushed_at":"2023-03-04T03:34:25.000Z","size":481,"stargazers_count":26,"open_issues_count":4,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-26T10:11:27.594Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ballercat.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}},"created_at":"2019-04-05T17:04:59.000Z","updated_at":"2024-03-09T07:05:46.000Z","dependencies_parsed_at":"2024-01-06T20:54:37.110Z","dependency_job_id":null,"html_url":"https://github.com/ballercat/jest-plugin-must-assert","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ballercat%2Fjest-plugin-must-assert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ballercat%2Fjest-plugin-must-assert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ballercat%2Fjest-plugin-must-assert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ballercat%2Fjest-plugin-must-assert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ballercat","download_url":"https://codeload.github.com/ballercat/jest-plugin-must-assert/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248589550,"owners_count":21129635,"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-08-03T17:00:29.658Z","updated_at":"2025-04-12T15:31:32.377Z","avatar_url":"https://github.com/ballercat.png","language":"JavaScript","funding_links":[],"categories":["Packages"],"sub_categories":["Library extensions"],"readme":"# `jest-plugin-must-assert`\n\nA plugin extending the default Jest behavior to fail any tests which do not\nperform a runtime assertion.\n\n## Problem\n\nAsynchronous tests could be challenging to get _right_, particularly for junior\ndevelopers or engineers new to async JavaScript. The most common mistake is an async\ntest which does not fire any assertions, either due to logic or even syntax errors.\nStatic analysis (linters) gets close to pointing out the issues, but is not enough to catch logic mistakes.\nFor this, we require a runtime check that _some_ assertion was run during the test.\n\n[Jest, unfortunately, has no \"failWithoutAssertions\" configuration options, so this plugin aims to remedy that.](https://github.com/facebook/jest/issues/2209)\nThe plugin patches the Jest API to force tests without any assertions to fail. In addition\nto failing tests without assertions this plugin also patches a bug in Jest which\nleads to [assertions \"leaking\" accross different tests](https://github.com/facebook/jest/issues/8297).\n\n## Install\n\n`npm i -D jest-plugin-must-assert`\n\nor\n\n`yarn add -D jest-plugin-must-assert`\n\nFor default behavior, add the plugin to your setup files.\n\n## Supported Jest Environments\n\n- `jest-plugin-must-assert` - Default supported environment, `node`\n- `jest-plugin-must-assert/jsdom` - JSDOM environment support. Necessary for\n  mocking window task functions like `setTimeout` when using `jest-environment-jsdom`.\n  Useful for React tests.\n\n## Use\n\n### For a specific test file\n\nYou may import the plugin into any test file you need additional safeguard for async logic.\n\n```js\nimport 'jest-plugin-must-assert';\n\ntest('some logic', () =\u003e {\n  setTimeout(() =\u003e expect(1).toBe(2)); // will be caught by the plugin\n  ...\n});\n```\n\n### For entire test suite\n\nAlternatively, you can enable the plugin for an entire test suite by adding it\nto your jest configuration.\n\n```js\n...\nsetupFilesAfterEnv: ['jest-plugin-must-assert'],\n...\n```\n\n### Manual configuration\n\nYou may also extend the default behavior with the following, manual configuration.\n\n```js\n// \u003crootDir\u003e/must-assert-setup.js\nconst patchJestAPI = require('jest-plugin-must-assert/manual');\n\npatchJestAPI({\n  /**\n   * Control the task execution during a test. You may log a custom warning message\n   * from here, throw an error etc.\n   *\n   * Default: The default behavior is that mismatched testIds result in ignoring of the task\n   * and a warning message.\n   *\n   * @param {Object} options       Options for the handler (see below)\n   *\n   * Options:\n   * @param {Number}   originTestId  The unique ID of the test where the task is oginating from\n   * @param {Number}   currentTestId The unique ID of the currently executing test\n   * @param {String}   testName      The name of the test which triggered this event\n   * @param {Object}   task          The task which is about to be invoked\n   * @param {String}   task.type     The type of task being invoked (micro/macro task)\n   * @param {String}   task.source   The source of the taks (\"promise.then\", \"setTimeout\" etc)\n   * @param {Object}   logger        The logger object (defaults to console)\n   * @param {Function} getStackTrace Returns the stack-trace of the stack\n   *\n   * @throws {Error} Default: throws. This function _may_ throw an error instead of logging it if\n   *                 you would like a stack trace back to the origin of the task being ignored.\n   *\n   * @return {Boolean} true/false for whether or not the task should execute\n   */\n  onInvokeTask({\n    originZoneId,\n    currentZoneId,\n    testName,\n    task,\n  }) {\n    // This is the default implementation of onInvokeTask. The error thrown will\n    // be displayed as a logger.warn with a cleaned up stack trace.\n    if (originZoneId !== currentZoneId) {\n      throw new Error(\n        `Test \"${testName}\" is attempting to invoke a ${task.type}(${task.source}) after test completion. Ignoring`\n      );\n    }\n    return true;\n  },\n\n  /**\n   * Logger DI. Used by the internal methods to log warnings/errors. Should match console API.\n   */\n  logger,\n\n  /**\n   * Regex list of what functions should be REMOVED from the stack traces of cancelled tasks.\n   * These are the default values. Overwriting this option removes these values\n   */\n  ignoreStack = [/Zone/, /zone\\.js/, /node_modules/],\n});\n```\n\nThen in your config file:\n\n```js\n...\nsetupFilesAfterEnv: [\n  '\u003crootDir\u003e/must-assert-setup'\n],\n...\n```\n\n## Performance\n\nThere are some performance implications of using this plugin as it does add a bit of\noverhead, but from testing it's a trivial increase. This plugin has been tested\nwithin a project with 1600+ test suites and over 10k individual tests, with only a negligible slow-down.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fballercat%2Fjest-plugin-must-assert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fballercat%2Fjest-plugin-must-assert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fballercat%2Fjest-plugin-must-assert/lists"}