- Ensure integration tests are kept separate from unit tests
- Spin up a live copy of the system from your test code
- Write a test which calls an API, and asserts on the API result (black-box)
- Write a test which calls an API, and asserts on database contents (white-box)
- Use tear-down hooks to clean up state left behind by the test
- Write integration tests for all APIs exposed by the system, stubbing things if necessary :)
- Write a bash script to execute unit tests first, then integration tests
Same pattern as previous pracs
- Commit any outstanding changes on your Prac04 branch:
git commit -am 'Some message'
- Head back over to master:
git checkout master
- Check out the Start Point for Prac 5:
git checkout Prac05-StartPoint
- Create a branch to make your changes on without impacting the main repo:
git checkout -b Prac05
- Push existing unit tests down into a "unit" folder within the "test" folder
- Create a new "integration" folder under the "test" folder
- Update the existing
package.json
andlaunch.json
config for the new location - Add new
package.json
andlaunch.json
entries to run integration tests separately to unit tests - Create an integration test file
- Make the
app.js
exportable and get a reference to it in - Add placeholder
describe
andit
blocks - Run the integration tests and ensure the app was spun up
- Implement a test that calls the
/api/pets/generateMatch
API and asserts on the result - Implement a test that calls the
/api/pets/random
API, and asserts on the history content directly from the database - Add a tear-down hook to reset the app's state after each test
Here's a step-by-step walkthrough of the practical steps, for if you get stuck :)
- Push existing unit tests down into a "unit" folder within the "test" folder
- Create a
test/unit
folder - Move existing tests into the new folder
- Update any
require
references to our app's code to ensure their relative paths are still correct - eg
require('../src/pet-service');
will need to becomerequire('../../src/pet-service');
, because the test file is now one level deeper in the directory structure
- Create a
- Create a new "integration" folder under the "test" folder
- Create a
test/integration
folder
- Create a
- Update the existing
package.json
andlaunch.json
config for the new location- In
package.json
, update thetest
script with a pathspec:"test": "mocha test/unit/**/*.js"
- In
launch.json
:- Update the name of the existing
Mocha Tests
configuration toUnit Tests
- Update the final
args
entry to point to the new unit test folder:"${workspaceFolder}/test/unit"
- Update the name of the existing
- In
- Add new
package.json
andlaunch.json
entries to run integration tests separately to unit tests- Add a new
package.json
script calledint-test
which sets the NODE_ENV variable to test, and starts the integration tests- Linux/OSX:
NODE_ENV=test mocha --timeout 5000 test/integration/**/*.js
- Windows:
SET NODE_ENV=test& mocha --timeout 5000 test/integration/**/*.js
- Linux/OSX:
- Add a new Mocha configuration to
launch.json
which points to theintegration
folder:{ "type": "node", "request": "launch", "name": "Integration Tests", "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", "args": [ "-u", "tdd", "--timeout", "5000", "--colors", "${workspaceFolder}/test/integration" ], "env": { "NODE_ENV": "test" }, "internalConsoleOptions": "openOnSessionStart" }
- Add a new
- Create an integration test file
- Add a new file
test/integration/api-tests.js
- Add a new file
- Make the
app.js
exportable and get a reference to it in- Open
app.js
- At the bottom of the file, add a line to export the
app
once it's spun up:module.exports = app;
- Open
- Add placeholder
describe
andit
blocks - Run the integration tests and ensure the app was spun up
- Implement a test that calls the
/api/pets/generateMatch
API and asserts on the resultdescribe("When requesting a dog match for Alice", function() { it("returns a Beaglier", function() { return httpClient.get("http://localhost:4000/api/pets/generateMatch?ownerName=Alice&petType=Dog") .then(result => { result.should.exist; result.should.have.property('petName').that.equals("Beaglier"); }); }); });
- Implement a test that calls the
/api/pets/random
API, and asserts on the history content directly from the databasedescribe("When requesting a random pet match", function() { it("adds a single entry to the request history", function() { return httpClient.get("http://localhost:4000/api/pets/random") .then(function() { var keys = db.getKeys(); keys.length.should.equal(1); }); }); });
- Add a tear-down hook to reset the app's state after each test
- Add an
afterEach()
function which callsdb.clear()
, like this:afterEach(function() { db.clear(); });
- Add an