Collecting Coverage Reports in Jest with Integration Tests - express

I have a bunch of tests setup with Jest that test Express server endpoints in the same repo. In order to accomplish this testing, I have Jest spinning up an Express server in the beforeAll() method. When I run Jest with the --coverage flag, I get coverage information for just the scripts that were run in order to start Jest and no reporting on the scripts that were triggered by hitting the endpoints. That makes sense, but is there a way to get coverage information on the endpoint code?
Snippet of test code:
const rp = require('request-promise')
describe('testFunctions', () => {
beforeAll(done => {
app.listen(config.PORT, async () => {
done()
})
})
it('hit endpoint', async () => {
const response = await rp({ uri: '/testFunctions', method: 'POST' })
expect(response).toEqual('response)
})
})
I'm trying to get a coverage report for all of the server code hit with the /testFunctions request.

This is the solution that worked for me. It required a bit of refactoring, but I think it was cleaner in the end. In my ecosystem, we are running a Parse Server as middleware with an Express server, but this could apply to anyone who needs to run a separate server with the tests.
So to get my server in a place that nyc (the coverage reporting tool I'm using) could monitor, I abstracted the server initialization completely out of the jest test suite and created a special npm script to run it in an entirely separate process.
NPM Script (package.json):
"scripts": {
"coverage": "nyc --silent npm start & ./listen_on_port_5000.sh && jest test/cloud/integration && kill $(lsof -t -i tcp:5000) && nyc report --reporter=html",
}
listen_on_port_5000.sh
while ! nc -z localhost 5000
do
sleep 0.5
done
So how does this work?
nyc --silent npm start runs which is the normal command we would run to start our server with Express and Parse but with the prepended nyc part with the --silent flag so that nyc can run in the background and watch the server code.
Because we know that the server always starts on port 5000, we run the start script in the background and start a separate process running a shell script (listen_on_port_5000.sh) to wait for the server to boot up.
Once the listener script detects anything running on port 5000, the npm script moves onto the jest command (all while the Express server is up and running).
When Jest finishes running, the final script runs a kill script to close the server running on port 5000.
We then run a second nyc script to generate the report that it collected in the first script. The generated report can be found in your project's directory under /coverage/lcov-report/ (or you can use a different coverage reporter.

Related

How to silently kill wiremock after Cypress test are executed?

I have an all-in-one npm script that:
Starts a React application
Starts Wiremock server in playback mode
Executes Cypress tests silently
For that I'm using concurrently:
"cypress:runner:one-shot": "concurrently npm:wiremock:playback npm:start:mock npm:cypress:execution:recorded",
After tests are executed I have to shut down the application and WireMock server.
For that, I added the following parameters to concurrently command:
--kill-others --success !command-0,!command-1
I need the all-in-one command to finish with an exit code 0.
For some reason is still finishing with a 1 (given that WireMock server is killed)
Is there any way to ignore the exit codes of any of those commands?
Just in case:
wiremock:playback is something like
"wiremock:playback": "npx wiremock --proxy-all '...' --verbose --port 3001 --enable-stub-cors --root-dir wiremock",
You can shut down WireMock by making a POST request to /__admin/shutdown (on the main host/port you configured WireMock to run on).

How to force exit an vue cli thread on completion in a deploy script (e.g. ctrl c equivelant)

I'm using Laravel Forge to run a simple deploy script. npm run build calls 'vue-cli-service build'.
Script below. The script 'ends' on
DONE Build complete. The dist directory is ready to be deployed.
INFO Check out deployment instructions at https://cli.vuejs.org/guide/deployment.html
but the thread does not quit, which causes issues in forge (e.g. thinks it's timed out or failed when it hasn't).
How do I do the equivelant of ctrl-c in a terminal once this has finished, in the deploy script? I've seen threads on trap SIGINT / trap etc. but I'm still not really sure how to implement it.
It may be that I just include the exit callback fix noted here: Vue-cli-service serve build completion callback?
git pull origin $FORGE_SITE_BRANCH;
npm run build;
( flock -w 10 9 || exit 1
echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock
if [ -f artisan ]; then
$FORGE_PHP artisan migrate --force
fi```
Try out to add Daemon termination command to the end of your deployment script
$FORGE_PHP artisan horizon:terminate

serverless-dynamodb-local web shell error

First time trying out serverless framework.
Am trying to use the local web shell to do some inspection.
But realise I couldn't list tables or show a list of records.
Web shell example:
var params = {
TableName: 'stocks-table-dev',
};
dynamodb.scan(params, function(err, data) {
if (err) ppJson(err); // an error occurred
else ppJson(data); // successful response
});
The above command throws a status code 413 error.
But aws cli works fine: aws dynamodb scan --table-name=stocks-table-dev --endpoint-url='http://localhost:8000'
I start the web shell with the command sls dynamodb start.
Prior to that, I used the following commands to install the plugin:
npm install --save-dev serverless-dynamodb-local
sls dynamodb install
Am I suppose to use the web shell for inspection?
Is there some configuration to be done for the web shell to work?

Parallel Build steps in Team City

I am Pretty new to Team City and have been set with a task of creating a CI build.
The thing I trying to build is an angular2 app with protractor e2e tests.
All the other build steps in Team City run ok but I am having trouble trying to run the step that does the e2e test.
if I was to do this locally I would open a cmd window and type...
npm run start
I would then open another command window and type...
npm run e2e
How do I run parallel steps in Team City?
Build steps cannot be run parallel in TeamCity. What you need to do, is create a script that runs 'npm run start' in background, and then runs 'npm run e2e'. You can use command line runner to run the script
I still couldn't get the forever thing working properly for me so I created my own node script that fires up live-server and then executes npm run e2e and that seems to have done the trick thanks for your help though Oleg.
This is how I did it in the end...
const exec = require('child_process').exec;
var psTree = require('ps-tree');
const server = exec('live-server ./dist --port=3000 --no-browser');
const tests = exec('npm run e2e');
tests.stdout.on('data', function(data) {
console.log(data);
});
tests.stderr.on('data', function(data) {
console.log(data);
});
tests.on('close', function(code) {
console.log('closing code: ' + code);
exec('taskkill /PID ' + server.pid + ' /T /F');
});

Forever Node.JS Express 4

How do you run the Express 4 app with Forever? (or is there a new package?)
I am running my Express 3 apps with Forever installed locally with the package manager. I use the command:
forever -a start app.js
Try this:
forever start ./bin/www
Let's take a look to package.json:
"scripts": {
"start": "node ./bin/www"
},
I guess when we call npm start, ./bin/www will be executed at some point.
Then look at the content of./bin/www:
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
so we are ready to listen for connections.
forever start --minUptime 1000 --spinSleepTime 1000 ./bin/www
If you use npm start to run your app, this works in place of it:
forever start -c "npm start" /path/to/app/dir/
Source: https://github.com/foreverjs/forever/issues/540
Try node app.js first, for me, I added a new module in code base, but i did not run npm install in my AWS box, forever is not giving you the error, it just stopped silently, but node will give you the error
http://expressjs.com/guide.html
in Expressjs guide doc,
use 'npm start'
I want use 'forever' but can not too
so,
add code at 'app.js'
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});
and
$node app.js
can use it.
and forever can use too
Feb 2021
This solution is for express.js & forever, on Windows OS.
package.json
"scripts": {
"start": "set PORT=3001 && set DEBUG=myapp:* && forever start --minUptime 1000 --spinSleepTime 1000 ./bin/www",
}
Now run:
npm start
NOTE: For other OS, and customization see following link: https://expressjs.com/en/starter/generator.html