This is a basic group of Node routes and Postgres models setup for testing. You're welcome to create a test suite for the routes and models. Create a issue outlining the tests you aim to complete, then submit a PR that covers it.
Describe basic unit testing in NodeJS. This project is a simple Express server using Jest for testing. This working example has been built for students wanting to write tests on open source projects during Hacktoberfest for practice with testing and contributing.
The main objective is to explain and use describe(), it(), and before()/etc hooks.
describe()
is merely for grouping, which you can nest as deep as you needit()
is a test case for the featurebeforeEach()
,afterEach()
are hooks to run before/after first/each it() or describe(). Which means,beforeEach()
will run before very it()/describe()
- create
#feature#.test.js
in/test
- Write
describe()
block using Jest's BDD style interface | Behavior-driven development (BDD) - Write nested
it()
in thedescribe()
block- Use Jest Matcher functions to assert functionality of feature inside
it()
block | Jest Matches
- Use Jest Matcher functions to assert functionality of feature inside
- If needed, write your
before()/etc
hooks. Inside or outside of thedescribe()
block | Setup and Teardown
- Write
- To submit your own test cases. If all cases are covered, feel free to create a new Model to test or new Node routes.
- Write a test suite outlined above
- Write out comments and steps for your testing as an exmaple to others to see.
- Create your own feature branch then attach your issue to the pull request.
- Create unit test example in Jest
- Proper Git workflow with feature branches and pull request
- Create intergation test example with Postgres and Supertest
- Add in GitHub Actions for testing code once pushed to Github
- Deploy to AWS if test passes
- Add new test cases for Hacktoberfest
You will need Node and Postgres installed. Using Node v18.12.0 and Postgres v15 for this demo. If you don't have these installed follow this guide.
Use yarn or npm to install the following packahes. Testing will require these packages:
TD;LR
npm install jest express sequelize
yarn add jest express sequelize
Install NodeJS via nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
nvm install stable
nvm use stable
which node
||node -v
should now show your version
Install Postgres via Postgres App for Mac use Postico for GUI.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
- Download Posgresapp and follow install instructions.
sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp
You will need to create dev.js
in config/
.
module.exports = settings => ({
postgres: {
pgUser: 'root',
pgHost: '127.0.0.1',
pgDatabase: postgres,
pgPassword: postgres_password,
},
...settings,
});
TODO: Outline setting up Docker container
TODO: Outline setting up Github and Railway
These tools are optional but really help with testing and code coverage.