RITEway-Jest
Inspired by Eric Elliott's RITEway.
Why?
TLDR: I wanted RITEway's assert
for Jest.
I love RITEway's API because it forces you to write good unit tests by it's given
-should
API and only exposing the equals
assertion.
Only problem is RITEway is build using tape. You can't use it with Jest, which in turn has some advantages and disadvantages.
Disadvantages
- You can't use it to test React Native components because Jest has the only good, up to date React Native mock.
- There might be some other Jest features that RITEway lacks.
- 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.
Advantages
- RITEway forces you to split your components' tests in an effective way. 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.
- You can't mock.
You might want to check out RITEway because you can learn these advantages first hand. I prefer RITEway for React apps and use RITEway-Jest for React Native apps.
Installation
npm i --save-dev riteway-jest
or
yarn add --dev riteway-jest
Then import it in your src/setupTests.js
for React with CRA.
;
For React Native you need to add a key in your package.json
to the jest
key.
"jest":
If you have a jest.config.js
.
moduleexports = setupFilesAfterEnv: 'node_modules/riteway-jest/src/riteway-jest.js' // ... other setup files ... // ... other options ...;
If ESLint yells at you, add a global
key to your .eslintrc.json
.
Usage
With pure functions.
const sum = a + b; ;
Using React Native Testing Library.
;;; { return <Text>title</Text>;} ;
Using React Testing Library.
;;; { return <button data-testid="foo" disabled=disabled onClick=onClick> text </button> ;} ButtonpropTypes = disabled: PropTypesboolisRequired onClick: PropTypesfuncisRequired text: PropTypesstring; ButtondefaultProps = disabled: false text: ''; ;
skip & only & todo
assert
supports Jest's skip
, only
and todo
functions.
// This test is explicitly skippedassert; // This test gets executedassert; // This test is implicitly skipped because the .only() above;
each
is currently not supported.