Skip to content

Instantly share code, notes, and snippets.

@sagiavinash
Created April 13, 2017 16:18
Show Gist options
  • Save sagiavinash/15c5858143ec42ce87e5f2493136ea1f to your computer and use it in GitHub Desktop.
Save sagiavinash/15c5858143ec42ce87e5f2493136ea1f to your computer and use it in GitHub Desktop.
Unit Testing Guidelines

UNIT TESTING GUIDELINES

File structure

  • All unit test files should be placed under /__tests__
  • Every test file's name should be suffixed with .spec.js

Test Codebase

Every test should convey the following information

  • Unit of work which is being tested
  • Scenario in which the unit is being tested
  • Expected Behaviour of the unit in a given scenario

Considering the above design goals - when testing a module the test codebase should be structured using the following mocha test helpers.

suite(message, bodyFn)

  • to be used to wrap all the test cases related to a Unit of work or wrap multiple suite()'s.
  • The message should be of the format "<helperFunction/filename> Test Cases:".
  • all the mock data variables should be declared here

describe(message, bodyFn)

  • to be used to convey the the scenario for which the Unit of work is being tested or wrap multiple describe()'s.
  • a describe should only hold one scenario, if tests are written for combination of scenarios then nested describe() should be used.
  • outermost wrapper describe's message should be of the format When .
  • inner wrapper describe's message should be of the format and when .
  • the arguments to be passed to the unit of work should be declared and assigned with one of the values from the mock data declared at suite()

it(message, bodyFn)

  • to be used to convey the expected behaviour in a particular scenario of the unit of work
  • the message should be of the format should
  • only one assert() should be used per it() test (if first assert fails other asserts are not tested)
  • actualResult variable needs to be declared within it() and needs to be passed as the first argument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment