Skip to content

Instantly share code, notes, and snippets.

@BillClinton
Last active October 3, 2019 21:50
Show Gist options
  • Save BillClinton/777e5b22efbb3961b34bc21ee7d9d1b8 to your computer and use it in GitHub Desktop.
Save BillClinton/777e5b22efbb3961b34bc21ee7d9d1b8 to your computer and use it in GitHub Desktop.
set up jest

Testing node using Jest

Install

  • Install jest as a dev dependency

nmp i jest --save-dev

Set up directories

  • Create a test directory in main project directory
  • Create a test/fixtures directory for static content, db connection, etc
  • Create a test/__mocks__ directory for mocked classes

Add to Package.json

  • Add a start script in package.json

"test": "jest"

  • Use env-cmd module to load tests with necessary .env variables
  • Add -i or --runInBand options to run tests serially
  • Add --watch to watch files for changes and rerun tests related to changed files.

"test": "env-cmd -f ./test.env jest --runInBand --watch"

  • Add root property to package.json for jest config
  "test": "env-cmd -f ./test.env jest --runInBand --watch",
  "jest": {
    "testEnvironment": "node"
  }

Testing a node api with jest

  • Install supertest as a dev dependency

npm install supertest --save-dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment