Skip to content

Part 0: Testing

KYY edited this page Aug 14, 2018 · 1 revision

Q: Why do we need unit testing?

To quote MPJ (Youtube Video):

I think of Unit testing as a software development technique where you break your software up into small, isolated units and write automated tests that ensure that each unit works as expected.

My main motivation for doing unit testing is to keep complexity from overwhelming me... a new piece of code always starts out simple but very quickly gets too much to keep all in our heads.


Q: How do we do unit testing for this app?

Unit testing is done with jest library (the default testing tool used by react-scripts). It includes both an assertion library and a mocking library out of the box.

The basic usage of jest for unit testing is to create a yourJSfile.test.js for yourJSfile.js, with jest methods like test();, it(); and expect().toBe();.


for JS testing, this article is very helpful: reference


Q: How does continuous integration help with testing?

The continuous integration (CI) tool we use in this project is Travis CI (see Wiki page on Node and Travis). As specified in the .travis.yml file:

script:
- npm run-script build
- npm run-script coverage

Whenever the Github repo online has push or pull request actions, Travis CI will automatically run .travis.yml's npm run-scripts on its cloud server.

In particular, npm run-script coverage is instructed to run the following script CI=true react-scripts test --env=jsdom --coverage in our package.json:

  ...
  "scripts": {
    "build": "react-scripts build",
    "test": "CI=true react-scripts test --env=jsdom",
    "coverage": "CI=true react-scripts test --env=jsdom --coverage",
  }
}

CI=true sets the process.env.CI to true, and according to the /node_modules/react-scripts/scripts/test.js, --watch mode is disabled when running testing.

Travis CI runs test (with --coverage enabled) as an automatic service linked to this Github project.


  • Test Runner: edp-test karma
  • Testing Framework: jasmine mocha qunit Jest
  • Assertion library: expect.js should chai
  • Coverage library: istanbul

reference


Assertion library: expect(window.r).to.be(undefined);