{"id":17068838,"url":"https://github.com/janhesters/riteway-jest","last_synced_at":"2025-04-12T18:23:04.832Z","repository":{"id":34889180,"uuid":"187075948","full_name":"janhesters/riteway-jest","owner":"janhesters","description":"Unit tests that always supply a good bug report when they fail for Jest.","archived":false,"fork":false,"pushed_at":"2023-01-05T16:16:45.000Z","size":1113,"stargazers_count":25,"open_issues_count":15,"forks_count":4,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-26T12:46:04.699Z","etag":null,"topics":["jest","unit-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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/janhesters.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2019-05-16T17:51:59.000Z","updated_at":"2023-03-20T20:28:03.000Z","dependencies_parsed_at":"2023-01-15T09:57:51.897Z","dependency_job_id":null,"html_url":"https://github.com/janhesters/riteway-jest","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janhesters%2Friteway-jest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janhesters%2Friteway-jest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janhesters%2Friteway-jest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janhesters%2Friteway-jest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/janhesters","download_url":"https://codeload.github.com/janhesters/riteway-jest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248610713,"owners_count":21132988,"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":["jest","unit-testing"],"created_at":"2024-10-14T11:15:14.856Z","updated_at":"2025-04-12T18:23:04.720Z","avatar_url":"https://github.com/janhesters.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RITEway-Jest\n\nInspired by [Eric Elliott](https://twitter.com/_ericelliott?lang=de)'s [RITEway](https://github.com/ericelliott/riteway).\n\n## Why?\n\n**TLDR:** I wanted [RITEway](https://medium.com/javascript-scene/rethinking-unit-test-assertions-55f59358253f)'s `assert` for Jest.\n\nI love RITEway's API because [it forces you to write good unit tests](https://medium.com/javascript-scene/what-every-unit-test-needs-f6cd34d9836d) by it's `given`-`should` API and only exposing the `equals` assertion.\n\nOnly problem is RITEway is build using [tape](https://www.npmjs.com/package/tape). You can't use it with\n[Jest](https://jestjs.io/en/), which in turn has some advantages and disadvantages.\n\n### Disadvantages\n\n- [You can't use it to test React Native components](https://github.com/ericelliott/riteway/issues/48)   because Jest has the only good, up to date React Native mock.\n- There might be some other Jest features that RITEway lacks.\n- I know this is minor, but you also have to do more setup compared to Jest,  which just works™ for React Native and  [React apps created with CRA](https://facebook.github.io/create-react-app/docs/running-tests).\n\n### Advantages\n\n- RITEway forces you to [split your components' tests in an effective way](https://medium.com/javascript-scene/unit-testing-react-components-aeda9a44aae2). This means only testing display components and their respective split off pure logic with unit tests, while covering the rest of your code using E2E tests.\n- [You can't mock.](https://medium.com/javascript-scene/mocking-is-a-code-smell-944a70c90a6a)\n\nYou might want to check out RITEway because you can [learn these advantages](https://medium.com/javascript-scene/tdd-the-rite-way-53c9b46f45e3) first hand. I prefer RITEway for React apps and use RITEway-Jest for React Native apps.\n\n## Installation\n\n```bash\nnpm i --save-dev riteway-jest\n```\n\nor\n\n```bash\nyarn add --dev riteway-jest\n```\n\n## Setup\n### React with CRA\nAdd an import in your `src/setupTests.js`.\n\n```js\nimport 'riteway-jest/src/riteway-jest.js';\n```\n### React Native\nAdd to your `jest` key in `package.json`.\n\n```json\n\"jest\": {\n  \"preset\": \"react-native\",\n  \"setupFilesAfterEnv\": [\"node_modules/riteway-jest/src/riteway-jest.js\"]\n}\n```\n\n### React Native with Expo\nAdd to your `jest` key in `package.json`.\n\n```json\n\"jest\": {\n  \"preset\": \"jest-expo\",\n  \"setupFilesAfterEnv\": [\"node_modules/riteway-jest/src/riteway-jest.js\"]\n}\n```\n\n### `jest.config.js` instead of `package.json`.\n\n```js\nmodule.exports = {\n  setupFilesAfterEnv: [\n    'node_modules/riteway-jest/src/riteway-jest.js',\n    // ... other setup files ...\n  ],\n  // ... other options ...\n};\n```\n\n## Tips\n\n### ESLint complains\nAdd a `global` key to your `.eslintrc.json`.\n\n```json\n{\n  \"_comment\": \"\u003cYour other settings here\u003e\",\n  \"globals\": {\n    \"assert\": 'readonly'\n  },\n  \"rules\": {\n    \"_comment\": \"\u003cYour rules here\u003e\"\n  }\n}\n```\n\n## Usage\n\nWith pure functions.\n\n```js\nconst sum = (a = 0, b = 0) =\u003e a + b;\n\ndescribe('sum()', () =\u003e {\n  const should = 'return the correct sum';\n\n  assert({\n    given: 'no arguments',\n    should: 'return 0',\n    actual: sum(),\n    expected: 0,\n  });\n\n  assert({\n    given: 'zero',\n    should,\n    actual: sum(2, 0),\n    expected: 2,\n  });\n\n  assert({\n    given: 'negative numbers',\n    should,\n    actual: sum(1, -4),\n    expected: -3,\n  });\n});\n```\n\nUsing [React Native Testing Library](https://github.com/callstack/react-native-testing-library).\n\n```js\nimport React from 'react';\nimport { Text } from 'react-native';\nimport { render } from 'react-native-testing-library';\n\nfunction MyText({ title }) {\n  return \u003cText\u003e{title}\u003c/Text\u003e;\n}\n\ndescribe('Text component', () =\u003e {\n  const createText = (props = {}) =\u003e render(\u003cMyText {...props} /\u003e);\n\n  {\n    const props = { title: 'Foo' };\n    const $ = createText(props).getByType;\n\n    assert({\n      given: 'a title',\n      should: 'display the title',\n      actual: $('Text').props.children,\n      expected: props.title,\n    });\n  }\n});\n```\n\nUsing [React Testing Library](https://github.com/testing-library/react-testing-library).\n\n```js\nimport PropTypes from 'prop-types';\nimport React from 'react';\nimport { cleanup, render } from 'react-testing-library';\n\nfunction Button({ disabled, onClick, text }) {\n  return (\n    \u003cbutton data-testid=\"foo\" disabled={disabled} onClick={onClick}\u003e\n      {text}\n    \u003c/button\u003e\n  );\n}\n\nButton.propTypes = {\n  disabled: PropTypes.bool.isRequired,\n  onClick: PropTypes.func.isRequired,\n  text: PropTypes.string,\n};\n\nButton.defaultProps = {\n  disabled: false,\n  text: '',\n};\n\ndescribe('button component', () =\u003e {\n  const createButton = (props = {}) =\u003e\n    render(\u003cButton onClick={() =\u003e {}} {...props} /\u003e);\n\n  {\n    const props = { text: 'foo' };\n    const $ = createButton(props).getByTestId;\n\n    assert({\n      given: 'a text',\n      should: \"render 'foo'\",\n      actual: $('foo').textContent,\n      expected: props.text,\n    });\n    cleanup();\n  }\n\n  {\n    const props = { disabled: true, text: 'foo' };\n    const $ = createButton(props).getByText;\n\n    assert({\n      given: 'disabled',\n      should: 'be disabled',\n      actual: $('foo').hasAttribute('disabled'),\n      expected: props.disabled,\n    });\n    cleanup();\n  }\n});\n```\n\n### skip \u0026 only \u0026 todo\n\n`assert` supports Jest's `skip`, `only` and `todo` functions.\n\n```js\n// This test is explicitly skipped\nassert.skip({\n  given: 'something',\n  should: 'be equal to something',\n  actual: 'nothing',\n  expected: 'something',\n});\n\n// This test gets executed\nassert.only({\n  given: 'something',\n  should: 'be equal to something',\n  actual: 'nothing',\n  expected: 'something',\n});\n\n// This test is implicitly skipped because the .only() above\nassert({\n  given: 'something',\n  should: 'be equal to something',\n  actual: 'nothing',\n  expected: 'something',\n});\n```\n\n`each` is currently **not** supported.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanhesters%2Friteway-jest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjanhesters%2Friteway-jest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanhesters%2Friteway-jest/lists"}