| 1 | This is a development guide.
|
|---|
| 2 | If you want to know the guidelines we follow then read [CONTRIBUTING.md](CONTRIBUTING.md).
|
|---|
| 3 |
|
|---|
| 4 | # Setup development environment
|
|---|
| 5 |
|
|---|
| 6 | ```sh
|
|---|
| 7 | git clone https://github.com/recharts/recharts.git
|
|---|
| 8 | cd recharts
|
|---|
| 9 | npm install # the right Node version can be found in .nvmrc file
|
|---|
| 10 | ```
|
|---|
| 11 |
|
|---|
| 12 | # Linting and types
|
|---|
| 13 |
|
|---|
| 14 | You may also want to enable ESLint and Prettier configuration in your favourite IDE.
|
|---|
| 15 |
|
|---|
| 16 | ```sh
|
|---|
| 17 | $ npm run lint
|
|---|
| 18 | $ npm run check-types
|
|---|
| 19 | ```
|
|---|
| 20 |
|
|---|
| 21 | ## Import restrictions
|
|---|
| 22 |
|
|---|
| 23 | The project enforces that all imports from `recharts` must use the public API entry point. Imports from internal paths like `recharts/types/*` or `recharts/src/*` are not allowed and will fail the linter.
|
|---|
| 24 |
|
|---|
| 25 | **Good:**
|
|---|
| 26 |
|
|---|
| 27 | ```typescript
|
|---|
| 28 | import { TooltipIndex, DataKey, BarRectangleItem } from 'recharts';
|
|---|
| 29 | ```
|
|---|
| 30 |
|
|---|
| 31 | **Bad:**
|
|---|
| 32 |
|
|---|
| 33 | ```typescript
|
|---|
| 34 | import { TooltipIndex } from 'recharts/types/state/tooltipSlice'; // ❌ Will fail lint
|
|---|
| 35 | import { DataKey } from 'recharts/src/util/types'; // ❌ Will fail lint
|
|---|
| 36 | ```
|
|---|
| 37 |
|
|---|
| 38 | This ensures that consumers of the library only depend on stable, public APIs.
|
|---|
| 39 |
|
|---|
| 40 | # Automated testing
|
|---|
| 41 |
|
|---|
| 42 | ## Running unit tests
|
|---|
| 43 |
|
|---|
| 44 | Most unit tests are in the `test` directory, some others are in `www/test`.
|
|---|
| 45 |
|
|---|
| 46 | Run all tests:
|
|---|
| 47 |
|
|---|
| 48 | ```sh
|
|---|
| 49 | $ npm run test
|
|---|
| 50 | ```
|
|---|
| 51 |
|
|---|
| 52 | Run a specific test file:
|
|---|
| 53 |
|
|---|
| 54 | ```sh
|
|---|
| 55 | $ npm run test -- path/to/TestFile.spec.tsx
|
|---|
| 56 | ```
|
|---|
| 57 |
|
|---|
| 58 | ## Running mutation tests
|
|---|
| 59 |
|
|---|
| 60 | Mutation tests may take several hours to complete.
|
|---|
| 61 | You may want to first open `./stryker.config.mjs` and set the `mutate` property to a specific file or directory
|
|---|
| 62 | that you want to test. That may take 5-10 minutes to run.
|
|---|
| 63 |
|
|---|
| 64 | Mutation tests do not run in CI.
|
|---|
| 65 |
|
|---|
| 66 | ```sh
|
|---|
| 67 | $ npm run test-mutation
|
|---|
| 68 | ```
|
|---|
| 69 |
|
|---|
| 70 | ## Storybook
|
|---|
| 71 |
|
|---|
| 72 | To run the Storybook UI:
|
|---|
| 73 |
|
|---|
| 74 | ```sh
|
|---|
| 75 | $ npm run storybook
|
|---|
| 76 | ```
|
|---|
| 77 |
|
|---|
| 78 | and then browse to http://localhost:6006.
|
|---|
| 79 |
|
|---|
| 80 | While the storybook is running:
|
|---|
| 81 |
|
|---|
| 82 | ```sh
|
|---|
| 83 | $ npm run test-storybook
|
|---|
| 84 | ```
|
|---|
| 85 |
|
|---|
| 86 | ## Run visual regression tests (using playwright)
|
|---|
| 87 |
|
|---|
| 88 | ### Prerequisites
|
|---|
| 89 |
|
|---|
| 90 | Playwright tests are running inside Docker. You will need to have Docker installed and running.
|
|---|
| 91 | See https://docs.docker.com/get-started/get-docker/. You do not need Docker account or login.
|
|---|
| 92 |
|
|---|
| 93 | You only need to do this once.
|
|---|
| 94 |
|
|---|
| 95 | ### Build the Docker image
|
|---|
| 96 |
|
|---|
| 97 | This takes two or three minutes to complete.
|
|---|
| 98 | You will need to re-build every time you make a change to dependencies in `package.json`.
|
|---|
| 99 |
|
|---|
| 100 | ```sh
|
|---|
| 101 | $ npm run test-vr:prepare
|
|---|
| 102 | ```
|
|---|
| 103 |
|
|---|
| 104 | ### Run the tests
|
|---|
| 105 |
|
|---|
| 106 | Now, the usual loop. Write a new test, run it, fix it, repeat.
|
|---|
| 107 |
|
|---|
| 108 | ```sh
|
|---|
| 109 | $ npm run test-vr
|
|---|
| 110 | ```
|
|---|
| 111 |
|
|---|
| 112 | Alternatively, the UI playwright mode is available as well:
|
|---|
| 113 |
|
|---|
| 114 | ```sh
|
|---|
| 115 | $ npm run test-vr:ui
|
|---|
| 116 | ```
|
|---|
| 117 |
|
|---|
| 118 | If you want to record new snapshots or update the old ones, you can run:
|
|---|
| 119 |
|
|---|
| 120 | ```sh
|
|---|
| 121 | $ npm run test-vr:update
|
|---|
| 122 | ```
|
|---|
| 123 |
|
|---|
| 124 | You will see new files created in the `test-vr/__snapshots__` directory, please commit them to the repository!
|
|---|
| 125 |
|
|---|
| 126 | ### See VR test results
|
|---|
| 127 |
|
|---|
| 128 | Open http://localhost:9323 in your browser to see the results of the tests.
|
|---|
| 129 | The CLI will tell you to run a "show-report" which is not necessary because there is already a Docker container running
|
|---|
| 130 | in the background and serving the report. Just open the URL in your browser.
|
|---|
| 131 |
|
|---|
| 132 | # Manual testing
|
|---|
| 133 |
|
|---|
| 134 | ## recharts.github.io local run
|
|---|
| 135 |
|
|---|
| 136 | To manually test Recharts in a real application environment, you can use the `www` directory which contains the source code
|
|---|
| 137 | for the Recharts documentation website https://recharts.github.io.
|
|---|
| 138 |
|
|---|
| 139 | You can add a new example and commit it too!
|
|---|
| 140 |
|
|---|
| 141 | To run the website locally in dev mode with hot-reloading:
|
|---|
| 142 |
|
|---|
| 143 | ```sh
|
|---|
| 144 | $ cd www
|
|---|
| 145 | $ npm run start
|
|---|
| 146 | ```
|
|---|
| 147 |
|
|---|
| 148 | When running locally, the website pulls the Recharts library from the local filesystem.
|
|---|
| 149 |
|
|---|
| 150 | When you make changes to the Recharts source code, you need to re-build it for the changes to be reflected in the website:
|
|---|
| 151 |
|
|---|
| 152 | ```sh
|
|---|
| 153 | $ cd ..
|
|---|
| 154 | $ npm run build
|
|---|
| 155 | $ cd www
|
|---|
| 156 | ```
|
|---|
| 157 |
|
|---|
| 158 | In production build, the website pulls recharts from npm registry.
|
|---|
| 159 |
|
|---|
| 160 | ## Storybook
|
|---|
| 161 |
|
|---|
| 162 | You can also use Storybook for manual testing of individual components.
|
|---|
| 163 |
|
|---|
| 164 | ```sh
|
|---|
| 165 | $ npm run storybook
|
|---|
| 166 | ```
|
|---|
| 167 |
|
|---|
| 168 | When adding new stories, mind that all stories here are also used for automated visual regression tests,
|
|---|
| 169 | using Chromatic cloud infrastructure.
|
|---|
| 170 |
|
|---|
| 171 | Chromatic are very generous and free for open source projects,
|
|---|
| 172 | however we already have so many stories that we hit the limit for open source plan in some months.
|
|---|
| 173 |
|
|---|
| 174 | For this reason, try to keep storybook for high fidelity examples, the ones you want to see published on the website
|
|---|
| 175 | and in storybook UI. For low fidelity tests, use unit tests or VR tests instead.
|
|---|
| 176 |
|
|---|
| 177 | ## Playwright UI mode
|
|---|
| 178 |
|
|---|
| 179 | You can also use Playwright in UI mode for manual testing. This opens a browser window where you can see the tests running,
|
|---|
| 180 | and you can see before & after.
|
|---|
| 181 |
|
|---|
| 182 | ```sh
|
|---|
| 183 | $ npm run test-vr:ui
|
|---|
| 184 | ```
|
|---|
| 185 |
|
|---|
| 186 | # Releases
|
|---|
| 187 |
|
|---|
| 188 | [Releases](https://github.com/recharts/recharts/releases) are automated via GH Actions - when a new release is created
|
|---|
| 189 | in GH, CI will trigger that:
|
|---|
| 190 |
|
|---|
| 191 | 1. Runs a build
|
|---|
| 192 | 2. Runs tests
|
|---|
| 193 | 3. Runs `npm publish`
|
|---|
| 194 |
|
|---|
| 195 | Version increments and tagging are not automated at this time.
|
|---|
| 196 |
|
|---|
| 197 | ## Release testing
|
|---|
| 198 |
|
|---|
| 199 | Until we can automate more, it should be preferred to test as close to the results of `npm publish` as we possibly can.
|
|---|
| 200 | This ensures we don't publish unintended breaking changes. One way to do that is using `yalc` - `npm i -g yalc`.
|
|---|
| 201 |
|
|---|
| 202 | 1. Make your changes in recharts
|
|---|
| 203 | 2. `yalc publish` in recharts
|
|---|
| 204 | 3. `yalc add recharts` in your test package (ex: in a vite or webpack reach app with recharts installed, imported, and
|
|---|
| 205 | your recent changes used)
|
|---|
| 206 | 4. `npm install`
|
|---|
| 207 | 5. Test a local run, a build, etc.
|
|---|
| 208 |
|
|---|
| 209 | # Folder structure
|
|---|
| 210 |
|
|---|
| 211 | Source code:
|
|---|
| 212 |
|
|---|
| 213 | - `src` - source code for Recharts library
|
|---|
| 214 | - `test` - unit tests
|
|---|
| 215 | - `test-vr` - visual regression tests (using Playwright)
|
|---|
| 216 | - `www` - source code for Recharts documentation website recharts.github.io
|
|---|
| 217 | - `storybook` - Storybook stories for Recharts components, and Storybook config+scaffolding
|
|---|
| 218 | - `scripts` - helper scripts for development and releases
|
|---|
| 219 | - `.husky` and `.github` - git hooks and GitHub Actions workflows for CI
|
|---|
| 220 |
|
|---|
| 221 | Autogenerated code:
|
|---|
| 222 |
|
|---|
| 223 | Running `npm run build` generates the following folders:
|
|---|
| 224 |
|
|---|
| 225 | - `lib` - compiled output of Recharts library in CJS format - published to npm
|
|---|
| 226 | - `es6` - compiled output of Recharts library in ESM format - published to npm
|
|---|
| 227 | - `umd` - compiled output of Recharts library in UMD format - published to npm
|
|---|
| 228 | - `types` - generated TypeScript declaration files - published to npm
|
|---|
| 229 | - `build` - output of `tsc` build - we don't use this for anything at all I think
|
|---|
| 230 |
|
|---|
| 231 | Running `npm run test-coverage` generates:
|
|---|
| 232 |
|
|---|
| 233 | - `coverage` - code coverage report
|
|---|
| 234 |
|
|---|
| 235 | Running `npm run test-mutation` generates:
|
|---|
| 236 |
|
|---|
| 237 | - `reports` - mutation testing report
|
|---|
| 238 |
|
|---|
| 239 | Running `npm run omnidoc` generates:
|
|---|
| 240 |
|
|---|
| 241 | - all `www/src/docs/api/*API.tsx` files - these are later used to generate API reference pages on the website on URL `/docs/api/*`
|
|---|
| 242 | - all `storybook/stories/API/arg-types/*Args.ts` files - these are later used by Storybook to show props tables for components, and to generate
|
|---|
| 243 | controls for props in Storybook UI
|
|---|
| 244 |
|
|---|
| 245 | If you want to modify any of the autogenerated code, you need to modify the JSDoc comments and/or TypeScript definitions in the appropriate source files in `src` folder,
|
|---|