Tests

Testing (that write programers) in programming means the code that checks our code. Testing program evokes (calls) our main code with many arguments, props etc.

Why testing is necessary? In future in changed code we may run tests and check if result will be successful.

A good approach to write a code in the step when you start a work is a question about tests (sometimes must to ask), but remember about time (2 times more) and price (minimus 30% increase).

In front-end we may divide 3 types of tests: module (some function or class), integration (between modules) & UI (application test; in fullstack development mostly UI tests name the test of the front-end).

Types and prices increasing schema with tests

Company divide two options of development with tests: code first, tests first. Test first schema.

Instruments: Runner - starts the test; Reporter - shows the result of tests; Matchers - check what we get after start; SPY, Mock - transfer some programmed data (mocked means to imitate some data for checking); Coverage - check if all functions, even lines in some function is working, actually how much percents of a code coverage with tests.

Jest - example 1

(Leap year fn()) according to "Test first"

Theory about leap year.

Leap year - year that has 366 days, or 29 in Feb.
Leap year (not really clear for me a, b, c):
a) - divisible by 4 without rest
b) - doesn't divide by 100 without rest
c) - divisible by 400 without rest
d) - not less then number 42

Find out all situations with "Leap year":

  • 2008 - leap, b/c - a)
  • 2003 - isn't leap, b/c - b)
  • 1900 - isn't leap, b/c - c)
  • 2000 - leap, b/c - c)
  • So for integer years atop the FN will be: Return "true" if year leap, or "false" if it isn't.
  • 41 - return "error" - year can't be less than 42
    In this case in the fn we may use simplest if(), but for whole testing theory and for developers (f.e. if we create a package we may think about all situations) it may return "error"
  • 2004.4 - return "error" - year should be integer number
  • "2008" - return "error" - Argument should be a number
  • true - return "error" - Argument should be a number
  • false - return "error" - Argument should be a number
  • null - return "error" - Argument should be a number
  • [] - return "error" - Argument should be a number
  • {} - return "error" - Argument should be a number
  • () => return "error" - Argument is not received

    P.S. if fn doesn't get "argument", js in fn transforms it to "undefined", so if we get undefined return error "Argument is not received" or "Argument should be a number"

  • undefined - return "error" - Argument should be a number

Create project and tests:

  • npm i nodemon -D
  • npm i jest
  • add scripts
    "start:dev": "nodemon app.js",
    "test": "jest" - looking for all files with ".test" endings & run them (f.e. isLeapYear.test.js )

    So. with integer numbers and with number less than 42 test looks like:

    See the Pen 112__isLeapYear__Jest-tests_for-Integer by Andrii (@imitator) on CodePen.

    P.S. Error in function and error in tests must be the same - 'A year should be higher than 41'

Function with all possible arguments, isLeapYear.js

TESTS for function above isLeapYear.test.js

Jest - example 2

"get /products" Route testing
  • npm i express jest supertest
    supertest - SuperAgent driven library for testing HTTP servers.
    Supertest lib imitates http requests.
  • npm i nodemon -D
  • add scripts
    "start:dev": "nodemon app.js",
    "test": "jest"
  • Create simplest web server in app.js
  • Create get \products ROUTE in routes/products.js
    -reexport products routes in routes\index.js
    const productRoutes = require('./products');
    module.exports = {productRoutes};
  • Create test products routes\products.test.js