{"id":15552208,"url":"https://github.com/bahmutov/rocha","last_synced_at":"2025-09-28T23:31:29.188Z","repository":{"id":2874007,"uuid":"47704223","full_name":"bahmutov/rocha","owner":"bahmutov","description":"Runs Mocha unit tests but randomizes their order","archived":false,"fork":false,"pushed_at":"2024-09-27T18:50:02.000Z","size":90,"stargazers_count":40,"open_issues_count":18,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-03T14:12:30.966Z","etag":null,"topics":["mocha","random","testing"],"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/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":"2015-12-09T16:34:45.000Z","updated_at":"2023-08-22T15:53:28.000Z","dependencies_parsed_at":"2023-07-06T07:34:04.615Z","dependency_job_id":"52e12735-39a3-4372-8f60-eaa15632d38e","html_url":"https://github.com/bahmutov/rocha","commit_stats":{"total_commits":179,"total_committers":5,"mean_commits":35.8,"dds":0.4301675977653632,"last_synced_commit":"d56b84e9857425ade3b591548931a8eaad5043ec"},"previous_names":[],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Frocha","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Frocha/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Frocha/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Frocha/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bahmutov","download_url":"https://codeload.github.com/bahmutov/rocha/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234570384,"owners_count":18854156,"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":["mocha","random","testing"],"created_at":"2024-10-02T14:12:28.838Z","updated_at":"2025-09-28T23:31:28.866Z","avatar_url":"https://github.com/bahmutov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rocha (aka \"ROKKA\" the Random Mocha)\n\n\u003e Runs Mocha unit tests but randomizes their order\n\n[![NPM][rocha-icon] ][rocha-url]\n\n[![Build status][rocha-ci-image] ][rocha-ci-url]\n[![semantic-release][semantic-image] ][semantic-url]\n[![manpm](https://img.shields.io/badge/manpm-%E2%9C%93-3399ff.svg)](https://github.com/bahmutov/manpm)\n[![standard style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)\n[![renovate-app badge][renovate-badge]][renovate-app]\n\n**E2E tests**\n\n`rocha-test` - [![Build Status](https://travis-ci.org/bahmutov/rocha-test.svg?branch=master)](https://travis-ci.org/bahmutov/rocha-test)\n\n## Install and use\n\nShould be just like [Mocha](https://mochajs.org/) for most cases\n\n    npm install -g rocha\n    rocha src/*-spec.js\n\nOpen an [issue][issues] if things do not work as expected.\n\nBecause I used some pieces of ES6, and Ubuntu does not play nicely with `--harmony`\nflag (which allows [using some ES6 today](https://glebbahmutov.com/blog/using-node-es6-today/))\nthis package requires Node \u003e= 4.\n\n## Demo screencast\n\n[![asciicast](https://asciinema.org/a/31549.png)](https://asciinema.org/a/31549)\n\nIn this demo I am showing how Rocha can find ordering problems in the tests (see the example below).\n\n## Example\n\nThe tests in [spec/tricky-spec.js](spec/tricky-spec.js) always pass in Mocha,\nbut only because their execution order is 1 - 2 - 3.\n\n```js\ndescribe('example', function () {\n  var foo\n  it('runs test 1', function () {\n    foo = 42\n    console.log('polluted the environment')\n  })\n  it('runs test 2', function () {})\n  it('runs test 3', function () {\n    console.assert(foo === 42, 'foo is 42', foo)\n  })\n})\n```\n\nThis tests pass under Mocha but this is very unreliable: a tiny code change\ncan break the tests for no obvious reason. A pain to find the problem too.\n\n### Running tests using Mocha\n\n    \u003e mocha spec/tricky-spec.js\n      example\n    polluted the environment\n        ✓ runs test 1\n        ✓ runs test 2\n        ✓ runs test 3\n      3 passing (8ms)\n\n### Running tests using Rocha\n\n    \u003e rocha spec/tricky-spec.js\n    shuffling 3 unit tests in \"example\"\n      example\n        1) runs test 3\n    polluted the environment\n        ✓ runs test 1\n        ✓ runs test 2\n      2 passing (10ms)\n      1 failing\n      1) example runs test 3:\n          AssertionError: foo is 42 undefined\n\nRocha takes each suite and shuffles its list of unit tests. Given enough test runs this should\nmake visible the problems due to shared data, or polluted environment, or even poor understanding of\nJavaScript [concurrency](http://glebbahmutov.com/blog/concurrency-can-bite-you-even-in-node/).\n\n## Notes\n\nNot every random order will be\n\n- so random that it is different from sequential\n- enough to flush out every problem\n\n## Recreating the failed order\n\nIf the unit tests fail, **the executed order is saved** in JSON file `.rocha.json`.\nFor the included example `rocha spec/*-spec.js` it will be something like this\n\n    [{\n      \"title\": \"fixed example\",\n      \"tests\": [\n        \"runs test 1\",\n        \"runs test 2\",\n        \"runs test 3\"\n      ]\n    }, {\n      \"title\": \"tricky example\",\n      \"tests\": [\n        \"runs test 1\",\n        \"runs test 3\",\n        \"runs test 2\"\n      ]\n    }]\n\nWhen you start `rocha` again, it will find this file and will reorder the tests\n**in the same order**, recreating the failure again.\n\nIf the tests pass, the `.rocha.json` file is deleted, thus the next run will be random again.\n\n## How should we test?\n\nEach unit test should NOT depend on the order the other tests are running. In the above case,\nrefactor the test to reset the variable before each unit test,\nsee [spec/fixed.spec.js](spec/fixed.spec.js)\n\n```js\ndescribe('fixed example', function () {\n  var foo\n  beforeEach(function () {\n    foo = undefined\n  })\n  ...\n});\n```\n\nNow each unit test starts from the same values (at least in this example).\n\n## Options\n\n**verbose log** - to see diagnostic messages as Rocha runs, set environment variable `DEBUG=rocha`\nwhen running.\n\n    DEBUG=rocha rocha \u003cspec\u003e\n\nDuring end to end tests, verbose logging is enabled using `DEBUG=rocha:e2e`\n\n### Small print\n\nAuthor: Gleb Bahmutov \u0026copy; 2015\n\n* [@bahmutov](https://twitter.com/bahmutov)\n* [glebbahmutov.com](https://glebbahmutov.com)\n* [blog](https://glebbahmutov.com/blog/)\n\nLicense: MIT - do anything with the code, but don't blame me if it does not work.\n\nSpread the word: tweet, star on github, etc.\n\nSupport: if you find any problems with this module, email / tweet /\n[open issue][issues] on Github\n\n[issues]: https://github.com/bahmutov/rocha/issues\n\n## MIT License\n\nCopyright (c) 2015 Gleb Bahmutov\n\nPermission is hereby granted, free of charge, to any person\nobtaining a copy of this software and associated documentation\nfiles (the \"Software\"), to deal in the Software without\nrestriction, including without limitation the rights to use,\ncopy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the\nSoftware is furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\nOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\nHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\nWHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\nOTHER DEALINGS IN THE SOFTWARE.\n\n[rocha-icon]: https://nodei.co/npm/rocha.svg?downloads=true\n[rocha-url]: https://npmjs.org/package/rocha\n[rocha-ci-image]: https://travis-ci.org/bahmutov/rocha.svg?branch=master\n[rocha-ci-url]: https://travis-ci.org/bahmutov/rocha\n[semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg\n[semantic-url]: https://github.com/semantic-release/semantic-release\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%2Frocha","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbahmutov%2Frocha","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Frocha/lists"}