Unit Testing
# Why Unit Testing
Let us start with a few questions:
- How to measure the quality of code
- How to ensure the quality of code
- Are you free to refactor code
- How to guarantee the correctness of refactored code
- Have you confidence to release your untested code
If you are not sure, you probably need unit testing.
Actually, it brings us tremendous benefits:
- guarantee the quality of maintaining code
- guarantee the correctness of reconstruction
- enhance confidence
- automation
It's more important to use unit tests in a web application during the fast iteration, because each testing case can contribute to the increasing stability of the application. The result of various inputs in each test is definite, so it's obvious to detect whether the changed code has an impact on correctness or not.
Therefore, code, such as in Controller, Service, Helper, Extend and so on, require corresponding unit testing for quality assurances, especially modification of the framework or plugins, of which test coverage is strongly recommended to be 100%.
# Test Framework
When searching 'test framework' in npm, there are a mass of test frameworks owning their own unique characteristics.
# Mocha
We choose and recommend you to use Mocha, which is very rich in functionality and supports running in Node.js and Browser, what's more, it's very friendly to asynchronous test support.
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.
# AVA
Why not another recently popular framework AVA which looks like faster? AVA is great, but practice of several projects tells us the truth that code is harder to write.
Comments from @dead-horse:
- AVA is not stable enough, for example, CPU capacity is going to be overloaded when plenty of files are running concurrently. The solution of setting parameter to control concurrent could work, but 'only mode' would be not functioning any more.
- Running cases concurrently makes great demands on implementation, because each test has to be independent, especially containing mock.
- Considering the expensive initialization of app, it's irrational of AVA to execute each file in an independent process initializing their own app while serial framework does only one time.
Comments from @fool2fish:
- It's faster to use AVA in simple application(maybe too simple to judge). But it's not recommended to use in complicate one because of its considerable flaws, such as incapability of offering accurate error stacks; meanwhile, concurrency may cause service relying on other test settings to hang up which reduces the success rate of the test. Therefore, process testing, for example, CRUD operations of database, should not use AVA.
# Assertion Library
Assertion libraries, as flourishing as test frameworks, are emerged continuously. The one we used has changed from assert to should, and then to expect , but we are still trying to find better one.
In the end, we go back to the original assertion library because of the appearance of power-assert, which best expresses 『No API is the best API』.
To be Short, Here are it's advantages:
- No API is the best API. Assert is all.
- ** powerful failure message **
- ** powerful failure message **
- ** powerful failure message **
You may intentionally make mistakes in order to see these failure messages.
# Test Rule
Framework defines some fundamental rules on unit testing to keep us focus on coding rather than assistant work, such as how to execute test cases. Egg does some basic conventions for unit testing.
# Directory Structure
Test code is demand to be put in test
directory, include fixtures
and assistant scripts.
Each Test file has to be named by the pattern of ${filename}.test.js
, ending with .test.js
.
For example:
test |
# Test Tool
Consistently using egg-bin to launch tests , which automatically loads modules like Mocha, co-mocha, power-assert, nyc into test scripts, so that we can concentrate on writing tests without wasting time on the choice of various test tools or modules.
The only thing you need to do is setting scripts.test
in package.json
.
{ |
Then tests would be launched by executing npm test
command.
npm test |
# Test Preparation
This chapter introduces you how to write test, and introduction of tests for the framework and plugins are located in framework and plugin.
# mock
Generally, a complete application test requires initialization and cleanup, such as deleting temporary files or destroy application. Also, we have to deal with exceptional situations like network problem and exception visit of server.
We extracted an egg-mockmodule for mock, help for quick implementation of application unit tests, supporting fast creation of ctx to test.
# app
Before launching, we have to create an instance of App to test code of application-level like Controller, Middleware or Service.
We can easily create an app instance with Mocha's before
hook through egg-mock.
// test/controller/home.test.js |
Now, we have an app instance, and it's the base of all the following tests. See more about app at mock.app(options)
.
It's redundancy to create an instance in each test file, so we offered an bootstrap file in egg-mock to create it conveniently.
// test/controller/home.test.js |
# ctx
Except app, tests for Extend, Service and Helper are also taken into consideration. Let's create a context through app.mockContext(options)
offered by egg-mock.
it('should get a ctx', () => { |
If we want to mock the data for ctx.user
, we can do that by passing the data parameter to mockContext:
it('should mock ctx.user', () => { |
Since we have got the app and the context, you are free to do a lot of tests.
# Testing Order
Pay close attention to testing order, and make sure any chunk of code is executed as you expected.
Common Error:
// Bad |
Mocha is going to load all the code in the beginning, which means doSomethingBefore
would be invoked before execution. It's not expected when especially using 'only' to specify the test.
It's supposed to locate in a before
hook in the suite of a particular test case.
// Good |
Mocha have keywords - before, after, beforeEach and afterEach - to set up preconditions and clean-up after your tests. These keywords could be multiple and execute in strict order.
describe('egg test', () => { |
# Asynchronous Test
egg-bin supports asynchronous test:
// using Promise |
According to specific situation, you could make different choice of these ways. Multiple asynchronous test cases could be composed to one test with async function, or divided into several independent tests.
# Controller Test
It's the tough part of all application tests, since it's closely related to router configuration. We need use app.httpRequest()
to return a real instance SuperTest, which connects Router and Controller and could also help us to examine param verification of Router by loading boundary conditions. app.httpRequest()
is a request instance SuperTest which is encapsulated by egg-mock.
Here is an app/controller/home.js
example.
// app/router.js |
Then a test.
// test/controller/home.test.js |
app.httpRequest
based on SuperTest supports a majority of HTTP methods such as GET, POST, PUT, and it provides rich interfaces to construct request, such as a JSON POST request.
// app/controller/home.js |
See details at SuperTest Document。
# mock CSRF
The security plugin of framework would enable CSRF prevention as default. Typically, tests have to precede with a request of page in order to parse CSRF token from the response, and then use the token in later POST requests. But egg-mock provides the app.mockCsrf()
function to skip the verification of the CSRF token of requests sent by SuperTest.
app.mockCsrf(); |
# Service Test
Service is easier to test than Controller. We need to create a ctx, and then get the instance of Service via ctx.service.${serviceName}
, and then use the instance to test.
For example:
// app/service/user.js |
And a test:
describe('get()', () => { |
Of course it's just a sample, actual code would probably be more complicated.
# Extend Test
It's extendable of Application, Request, Response and Context as well as Helper, and we are able to write specific test cases for extended functions or properties.
# Application
When an app instance is created by egg-mock, the extended functions and properties are already available on the instance and can be tested directly.
For example, we extend the application in app/extend/application
to support cache based on ylru.
const LRU = Symbol('Application#lru'); |
A corresponding test:
describe('get lru', () => { |
As you can see, it's easy.
# Context
Compared to Application, you need only one more step for Context tests, which is to create an Context instance via app.mockContext
.
Such as adding a property named isXHR
to app/extend/context.js
to present whether or not the request was submitted via XMLHttpRequest.
module.exports = { |
A corresponding test:
describe('isXHR()', () => { |
# Request
Extended properties and function are available on ctx.request
, so they can be tested directly.
For example, provide a isChrome
property to app/extend/request.js
to verify requests whether they are from Chrome or not.
const IS_CHROME = Symbol('Request#isChrome'); |
A corresponding test:
describe('isChrome()', () => { |
# Response
Identical with Request, Response test could be based on ctx.response
directly, accessing all the extended functions and properties.
For example, provide an isSuccess
property to indicate current status code equal to 200 or not.
module.exports = { |
The corresponding test:
describe('isSuccess()', () => { |
# Helper
Similar to Service, Helper is available on ctx, which can be tested directly.
Such as app/extend/helper.js
:
module.exports = { |
A corresponding test:
describe('money()', () => { |
# Mock Function
Except functions mentioned above, like app.mockContext()
and app.mockCsrf()
, egg-mock provides quite a few mocking functions to make writing tests easier.
- To prevent console logs through
mock.consoleLevel('NONE')
- To mock session data through
app.mockSession(data)
describe('GET /session', () => { |
Remember to restore mock data in an afterEach
hook, otherwise it would take effect with all the tests that supposed to be independent to each other.
describe('some test', () => { |
When you use egg-mock/bootstrap
, resetting work would be done automatically in an afterEach
hook, Do not need to write these code any more.
The following will describe the common usage of egg-mock.
# Mock Properties And Functions
Egg-mock is extended from mm module which contains full features of mm, so we can directly mock any objects' properties and functions.
# Mock Properties
Mock app.config.baseDir
to return a given value - /tmp/mockapp
.
mock(app.config, 'baseDir', '/tmp/mockapp'); |
# Mock Functions
Mock fs.readFileSync
to return a given function.
mock(fs, 'readFileSync', filename => { |
See more detail in mm API, include advanced usage like mock.data()
,mock.error()
and so on.
# Mock Service
Service is a standard built-in member of the framework, app.mockService(service, methodName, fn)
is offered to conveniently mock its result.
For example, mock the method get(name)
in app/service/user
to return a nonexistent user.
it('should mock fengmk1 exists', () => { |
Using app.mockServiceError(service, methodName, error)
to mock exception.
For example, mock the method get(name)
in app/service/user
to throw an exception.
it('should mock service error', () => { |
# Mock HttpClient
External HTTP requests should be performed though HttpClient, a built-in member of Egg, and app.mockHttpclient(url, method, data)
is able to simulate various network exceptions of requests performed by app.curl
and ctx.curl
.
For example, we submit a request in app/controller/home.js
.
class HomeController extends Controller { |
Then mock it's response.
describe('GET /httpclient', () => { |
# Sample Code
All sample code can be found in eggjs/exmaples/unittest