Local Development
We provide convenient ways for development, debugging, and unit tests to improve your development experience.
Here, we need to use egg-bin module (Only used in local development and unit tests. For production environment, please refer to Deployment).
First of all, we need to include egg-bin
module in devDependencies
:
$ npm i egg-bin --save-dev |
# Start App
Once we have modified code and saved in local development, the app will restart automatically, and our changes will take place right after that.
# Adding Script
Add npm scripts
into package.json
:
{ |
And then we may start app by npm run dev
.
# Environment Configuration
To start app in local, environment needs to be set as env: local
. The configuration comes from the combination of both config.local.js
and config.default.js
.
# Port Assignment
Starting app in local will listen to port 7001 by default. You may assign other port to it like this:
{ |
# Unit Test
Here we mainly cover the usage of the tools, for more details about unit tests, please refer to here.
# Adding Script
Add npm scripts
into package.json
:
{ |
And then we can run unit test by npm test
.
# Environment Configuration
To run test cases, environment needs to be set as env: unittest
. The configuration comes from the combination of both config.local.js
and config.unittest.js
.
# Run specific test file
npm test
command will look for all the files ended with .test.js
under test folder (default glob matching rule is test/**/*.test.js
).
However, we would like to run only the test cases that we are working on sometimes. In this case, we may specify a file in the following way:
$ TESTS=test/x.test.js npm test |
glob expressions are supported here.
# Reporter Setting
Mocha supports various reporters. Default reporter is spec
.
Reporter is allowed to be specified mannually via setting TEST_REPORTER as the environment variable. For example, to use dot
instead:
$ TEST_REPORTER=dot npm test |
# Timeout Setting
The default timeout is 30 seconds. We may set our own timeout (in milliseconds). For example, setting timeout to 5 seconds:
$ TEST_TIMEOUT=5000 npm test |
# Pass Parameters via argv
Besides environment variables, egg-bin test
also supports passing parameters directly, and it supports all mocha parameters. You may refer to mocha usage.
$ # Passing parameters via npm need to add an extra `--`, according to https://docs.npmjs.com/cli/run-script |
# Code Coverage
egg-bin has build-in nyc to support calculating code coverage report of unit test.
Add npm scripts
into package.json
:
{ |
And then we can get code coverage report of unit test via npm run cov
.
$ egg-bin cov |
And we may open HTML file of complete code coverage report via open coverage/lcov-report/index.html
.
# Environment Configuration
Just like test, the environment needs to be set to env:unittest
to run cov, and the configuration comes from the combination of both config.local.js
and config.unittest.js
.
# Ignore Specific Files
To ignore some files in code coverage rate calculation, You may use COV_EXCLUDES
as the environment variable to ignore some specific files that don't need the test converages:
$ COV_EXCLUDES=app/plugins/c* npm run cov |
# Debugging
# Log Print
# Use Logger
Module
There's a built-in Log in the egg, so you may use logger.debug() to print out debug information. We recommend you use it in your own code.
// controller |
Levels of logs can be configured via config.logger.level
for printing into file, and config.logger.consoleLevel
for printing into console.
# Use Debug
Module
debug module is a debug tool widely adopted in Node.js community, plenty of modules are using it to print debug information. So for the Egg community. We recommand you use it in your own development of framework and plugins.
It's easy for us to watch the whole process of test through DEBUG
as the environment variable to start with certain code.
(Do not confuse debug module with logger module, for the latter also has quite a lot of functions, what we mean "log" here is the debug info.)
Turn on log of all modules:
$ DEBUG=* npm run dev |
Turn on log of specific module:
$ DEBUG=egg* npm run dev |
Detail logs of unit tests progress are able to be viewed via DEBUG=* npm test
.
# Debug with egg-bin
# Adding Script
Add npm scripts
into package.json
:
{ |
And then we may set breakpoints for debugging our app via npm run debug
.
egg-bin
will select debug protocol automatically. Inspector Protocol will be selected for version 8.x and later. For earlier ones, Legacy Protocol is the choice.
Meanwhile, It also supports customized debug parameters.
$ egg-bin debug --inpsect=9229 |
- Debug port of
master
is 9229 or 5858 (Legacy Protocol). - Debug port of
agent
is fixed to 5800, it is customizable viaprocess.env.EGG_AGENT_DEBUG_PORT
. - Debug port number of
worker
will increase frommaster
port number. - During developing period, worker will "hot restart" once the code is changed, and it will cause the increase of port. Please refer to the following IDE configuration for auto-reconnecting.
# Environment Configuration
App starts by env: local
when executing debug . The configuration comes from the combination of both config.local.js
and config.unittest.js
.
# Debug with DevTools
The latest DevTools only supports Inspector Protocol. Thus you will need to install Node.js 8.x or higher verions to be able to use it.
Execute npm run debug
to start it:
➜ showcase git:(master) ✗ npm run debug |
And then choose one of the following ways:
- Visit the
DevTools
URL printed in the last few lines in console directly. Since this URL is proxied from worker, you don't need to worry about restarting. - Open
chrome://inspect
, and config port accordingly, then clickOpen dedicated DevTools for Node
to open debug console.
# Debug with WebStorm
egg-bin
will read environment variable $NODE_DEBUG_OPTION
set in WebStorm debug mode.
Start npm debug in WebStorm:
# Debug with VSCode
There are 2 ways:
1st method: open settings in VSCode, turn on Debug: Toggle Auto Attach
, and then execute npm run debug
in the terminal.
2nd method: setup .vscode/launch.json
in VSCode, and then simply start with F5 key. (Note: You have to turn off the settings mentioned in the 1st method before doing it).
// .vscode/launch.json |
And we offer a vscode-eggjs extention to setup auto-matically.
For more options of setting up debug in VSCode, please refer to Node.js Debugging in VS Code.
# More
If you would like to know more about local development, like customizing a local development tool for your team, please refer to egg-bin.