Testing JavaScript in Node
Here is a JavaScript example of a test script: The to programs you will see are quoted from https://blog.logrocket.com/node-js-express-test-driven-development-jest/, and then adapted to fit better into our style guide, and adapted a bit to include a couple of extra points.
Example 1. A Test Suite, logrocketjs/tests/index.test.js
|
|
Here comes the software containing the units, functions to be tested. Please notice that this program is oblivious to the existence of the test while the test was very much aware of the functions in this program.
Example 2. A Program to be Tested, logrocketjs/index.js
|
|
The Test Runner is executed from the CLI by
|
|
The Test Runner will recursively search the current directory for any of:
*.js
,*.cjs
, and*.mjs
files in directories calledtest
.- Files named
test
. Example:test.js
- Files starting with
test-
. Example:test-something.cjs
- Files ending with
.test
,-test
, or_test
. Examples: something.test.js, something-test.cjs, something_test.mjs