В процессе разработки у нас может быть множество модулей, и для каждого может быть определено несколько тестов. С помощью метода describe(), который определен в mocha.js, можно оформить тесты в связанные группы. Например, тесты по одному модулю будут составлять одну группу, а тесты другого модуля будут оформлять соответственно другую группу. Разбиение на группы позволит легко идентифицировать, для какого модуля или группы не прошел тест, особенно если тестов очень много.
Например, пусть в проекте будет модуль operations.js:
module.exports.multiply = function(x,y){return x * y;} module.exports.add = function(x, y){ return x + y;}
Для этого модуля определим файл тестов operations.test.js:
const assert = require("assert"); const operations = require("./operations"); describe("Operation Tests", function(){ it("should multiply two numbers", function(){ const expected = 15; const result = operations.multiply(3, 5); assert.strictEqual(result, expected); }); it("should add two numbers", function(){ const expected = 16; const result = operations.add(9, 7); assert.strictEqual(result, expected); }); });
В метод describe()
в качестве первого параметра передается описание тестов, а в качестве второго - функция, которая содержит тесты.
Также пусть в проекте будет файл app.js:
const express = require("express"); const app = express(); app.get("/", function (request, response){ response.send("Hello Test"); }); app.get("/error", function (request, response){ response.status(404).send("NotFound"); }); app.get("/user", function (request, response){ response.send({name:"Tom", age: 22}); }); module.exports.app = app;
Для него определим файл тестов app.test.js:
const request = require("supertest"); const assert = require("assert"); const app = require("./app").app; describe("Express Tests", function(){ it("should return Hello Test", function(done){ request(app) .get("/") .expect("Hello Test") .end(done); }); it("should return NotFound with status 404", function(done){ request(app) .get("/error") .expect(404) .expect("NotFound") .end(done); }); it("should return user with name Tom and age 22", function(done){ request(app) .get("/user") .expect(function(response){ assert.deepStrictEqual(response.body, {name:"Tom", age:22}); }) .end(done); }); });
Здесь метод describe оформляет вторую группу.
Запустим тесты:
c:\app> npm test > testapp@1.0.0 test > mocha *.test.js Express Tests ✔ should return Hello Test ✔ should return NotFound with status 404 ✔ should return user with name Tom and age 22 Operation Tests ✔ should multiply two numbers ✔ should add two numbers 5 passing (44ms) c:\app>