-
test suite is a collection of tests for a web application. To run a test suite, do
npm test
- the test file for
index.js
should be named asindex-test.js
- testing types:
- unit test: test the smallest unit of the code
- integration test: test the interaction between internal services inside the web application
- End to End test: real user experience
- test with mocha:
-
npm init -y
to create a package.json file for managing packages. - install mocha by
npm install mocha -D
so it's saved underdevDependencies
in package.json, which means it's only included during development mode not in the production bundle. - change the value of "test" in "script" object to
mocha
- run
npm test
to start the test
-
-
describe
andit
funcitons accept two parameters, one is the descriptive message, the second one is the call back funciton. - The callback function should has setup, execise, verify and teardown sections.
- variables are declared in setup section
- execute functions in execise section
- and verify the result with the expectedValue
- teardown makes the tests isolated so it's not affected or affecting other tests.
- Hooks can make setup and teardown easier. (Note: The example below works for mocha, it may vary in other test package)
-
beforeEach(callback)
callback is run before each test -
afterEach(callback)
callback is run after each test -
before(callback)
callback is run before the first test -
after(callback)
callback is run after the last test
-
- TDD stand for test driven development, it is guided by red-green-refactor cycle. The test is first written, then write the minimum amound of implement code to pass the test and then refactor the code.
-
code coverage can depend on:
- function coverage: all the funcitons are called
- statement coverage: all the statements are run?
- path coverage: every edge?
- condition coverage: both the boolean value tested?
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)