The tests use mocha for testing and nock for mocking responses from a gitlab server.
Start out by writing an outline of what you want to test by adding pending tests to a .js file in the test directory, (inside the appropriate describe blocks)
like:
it('should do xyz when abc happens');
it('should do lmn when opq happens');
... as many as necessary
Now running npm test will show your tests as "pending" and marked with a "-".
Next, observe the "happy path", the existing behavior of the code by using util.inspect to log the parameters passed to the function to be tested when the codepath is run (ensure that the codepath is run by using the Strider web gui if needed), in order to understand what needs to be passed to the function you want to test in the test file.
In order to sniff and record the http traffic so that you can create a nock mock of the gitlab server, use code similar to the following inside the codepath that you want to test:
In file lib/api.js ...
var nock = require('nock');
...
//for example, inside the removeDeployKey function in lib/api.js
var appendLogToFile = function(content) {
fs.appendFile('/noderoot/gitlab_remove_deploykey.txt', content);
};
nock.recorder.rec({
logging: appendLogToFile,
});
In the above example, the file gitlab_remove_deploykey.txt file will contain the statements generated by nock.
Next write out the body of the pending tests, one at a time. After writing out the body of a test, do an 'npm test' run, with nock still recording. Before doing the npm test run, ensure that only the current describe() block is active by adding the .only suffix. Like so,
describe.only('parseRepo', function(){
it('should correctly parse repo information as received from gitlab server into a repo object');
it('should throw an error if it gets an empty parameter as repo ?');
...
This will ensure that only the tests for functionality currently under consideration will be run. Also, mark all tests other than the current one to be inactive by changing the "it('should ..." to "xit('should ...". Else, you can ensure that only the current test is run by changing 'it(' to 'it.only('
Before the run, you must also comment out any code that asks nock to intercept calls - the usual place for which is in the before, after or beforeEach/afterEach blocks. For example, comment out all the lines inside the following blocks
before('Setup the mock gitlab server', function setupNock() {
nock.cleanAll(); //remove all interceptors
require('./mocks/gitlab_add_key.js')(); //pull in the mock code to simulate the server for this block
});
after('Tear down mock Gitlab server', function tearDownNock() {
nock.cleanAll(); //remove all interceptors
});
This will ensure that the actual interaction between our code and the gitlab server will be recorded by nock. Copy out the code generated by nock into a .js file in the mocks subdirectory (see existing files in mocks/ for example)
Before moving on to writing the next test, empty out the file used by nock for recording, and mark the test already written with a "xit(" or a "it.skip("
After you are done with writing the tests, remove or comment out the nock.recorder.rec code so that nock stops recording. Then, enable all the tests in the block and enable/de-comment out the code that asks nock to disableNetConnect and mock, and re-run "npm test" to see all your tests run with the gitlab server mocked out of the picture.