Getting started with sane code and a testing mentality has never been simplier with Gatsby.
Gatsby is a framework to create React apps that load static content first, then bootstraps React once everything loads. This keeps everything blazing fast to load, but keeps the dynamic nature that everyone loves with React.
Typescript lets you catch all those pesky errors by uping your typing hygiene and providing better code hints.
Jest keeps your Unit and Integration tests easy with Testing-Library. Jest + Gatsby take some setting up, but that's all been taken care of.
Cypress automates your browser to check through your End-to-End tests with ease and speed.
With Jest and Cypress together, be able to cover important aspects of your code while only testing how the user would interact with your app!
-
Create a Gatsby site.
Use the Gatsby CLI to create a new site, specifying the default starter.
# create a new Gatsby site using the default starter gatsby new new-starter https://github.com/lexedwards/starter-cypr-jest-ts
-
Start developing.
Navigate into your new site’s directory and start it up.
cd new-starter/ npm run validate gatsby develop
-
Open the source code and start editing!
Your site is now running at
http://localhost:8000
!Note: You'll also see a second link:
http://localhost:8000/___graphql
. This is a tool you can use to experiment with querying your data. Learn more about using this tool in the Gatsby tutorial.Open the
new-starter
directory in your code editor of choice and editsrc/pages/index.js
. Save your changes and the browser will update in real time!
There are a multitude of config files within the root, from .nvmrc
stating
what version of node to use, .eslintrc
to check for typos, and .huskyrc
&
lintstagedrc
which will fire commands when you start commiting code with git.
/src
: This directory will contain all of the code related to what you will see on the front-end of your site (what you see in the browser) such as your site header or a page template.src
is a convention for “source code”.
gatsby-*.js
files run in Node
; not the browser. Until Node pushes support
for modules in the LTS line, I still recommend these files stay in commonJS
rather than ES6 Modules.
-
gatsby-browser.js
: This file is where Gatsby expects to find any usage of the Gatsby browser APIs. These allow customization/extension of default Gatsby settings affecting the browser. Currently there is alayout
React Element wrapping the Page Element so that I wouldn't have to continuously import it to each and every page or template. As well as this, there is a nifty css variable generator to calculate--vh
, as safari users will experience a differentvh
in native CSS. -
gatsby-config.js
: This is the main configuration file for a Gatsby site. This is where you can specify information about your site (metadata) like the site title and description, which Gatsby plugins you’d like to include, etc. (Check out the config docs for more detail). -
gatsby-node.js
: This file is where Gatsby expects to find any usage of the Gatsby Node APIs. This would be where functions for auto-generated pages would live, taking in data and templates and feeding it to the SSR generation. -
gatsby-ssr.js
: This file is where Gatsby expects to find any usage of the Gatsby server-side rendering APIs. It's highly recommended anything that's present withingatsby-browser
is also present withingatsby-ssr
if it affects page-generation, such as wrapping elements i.e<Layout />
-
package.json
: A manifest file for Node.js projects, which includes things like metadata (the project’s name, author, etc). This manifest is how npm knows which packages to install for your project. -
README.md
: A text file containing useful reference information about your project.