- Overview
- Directory Structure
- Running the Application Locally
- Using Codespaces
- Running Tests
- Starting React Storybook
- Scripts Reference
This app is a template setup to quickly stand up a new app with the following tools:
- Next.js
- TypeScript
- Tailwind CSS
- React Storybook
- Jest
- React Testing Library
- Apollo Client
- Cypress
- Chromatic
- Codespaces
This template uses Apollo & GraphQL for the data-layer. The template by default generates types based off the Countries API by @trevorblades.
- components/
- myComponent/
- index.tsx # the component itself
- index.spec.tsx # the unit test suite
- index.stories.tsx # storybook stories
- docs.mdx # component documentation used by storybook
- pages/
- pagename.tsx # uses components/ files
- pagename.spec.tsx # integration test suite for pages
- I chose to use flat directories in this project, since there would be
many files associated with a single "component".
- There are test files (
.spec.tsx
), documentation (.mdx
), storybook files (.stories.tsx
), and the component itself (.tsx
). - If these were all placed in separate peer directories to the components themselves, navigating the project would be more challenging. Colocating components and associated files allows for quick navigation and easy copying to quickly spin up new components.
- There are test files (
- This does, however mean that multiple files are named the same thing (like
index.spec.tsx
), where you need to focus on the directory name instead of the file name when navigating. This can be confusing when multiple files are open at the same time from different components, but I think the benefits of colocation outweigh the risks there.
To run this application, clone this repo and then run the following (requires yarn to be installed previously):
cd countries
# install dependencies
yarn
# starts nextjs development server
npm run dev
Once up and running, go to localhost:3000 to see the app running.
This app is set up to be developed in Github Codespaces.
To edit this project, click the Code
button at the top right of this page.
Click on the Codespaces
tab and follow the instructions to spin up your own codespace.
There are three kinds of tests used in this project:
- Unit tests on individual components to test specific functionality in an isolated environment.
- These are found in the
src/components/*
directories under theindex.spec.tsx
files.
- Integration tests rendering a single page (feature) and using mocked data to ensure proper rendering of the page.
- These are found in the
src/pages
directory under theindex.spec.tsx
files.
- End-to-end tests, which run the application and fetch real-world data. These tests act like a user and make assertions about the app's state based on click and navigation interaction like a user would.
- These tests are found under the
cypress/e2e
directory. - An output file is saved to
cypress/videos
showing the last run of these tests.
To run unit & intergration tests, make sure dependencies are installed and run
yarn test:unit
To run e2e tests, make sure dependencies are installed and run:
yarn test:e2e:dev
You may need to stop any running instance of yarn dev
that currently is running
on your machine.
In most src/components
directory, there are index.stories.tsx
and docs.mdx
files.
These files can include programmatic sandboxes and written documentation on the usage,
accessibility concerns, and props for each of the built components.
Note: There is a
docs
and acanvas
tab at the top of each page. Thedocs
tab is there to display thedocs.mdx
files, and thecanvas
tab is the rendering of the examples defined in theindex.stories.mdx
file. You can use the left sidebar to navigate between components.
You can also run storybook locally if you'd like. To run storybook, clone this repo and then run the following (requires yarn to be installed previously):
cd countries
# install dependencies
yarn
npm run storybook
Once up and running, go to localhost:6006 to see the app running.
Using yarn <command-name>
:
Development workflow:
dev
: Run app in development modestorybook
: Build and run React Storybook on port 6006generate:types
: Generate GraphQL typesgenerate:types:watch
: Generate GraphQL types and rebuild on changelint
: Check project with ESLint
Testing:
test:unit
: Run jest tests incomponents/
andpages/
cypress:open
: Open theCypress
app locally for manually running e2e tests- expects the app to be running on
localhost:3000
(yarn dev
)
- expects the app to be running on
cypress:run
: run all Cypress e2e tests- expects the app to be running on
localhost:3000
(yarn dev
)
- expects the app to be running on
test:e2e:dev
: builds the app withyarn dev
in the background and opens Cypress tests.
Deployment:
build
: Build app for productionstart
: Serve built production appbuild:storybook
: Build storybook for deploychromatic
: Deploys storybook instance to Chromatic- Expects project token set in this script in
package.json
- Expects project token set in this script in