I'm currently in the process of rewriting postman tests to run via node with mocha/chai in hopes of streamline the code to be much more reusable. At a high level I have to make a series of async requests, using the data from one response to make the next request. All the test cases use the same requests, in the same order, but with different data (based on the responses), different tests, and may skip some requests. So for instance, my first test case I want the first two async requests to respond with a 200 status, make a third async call and then validate some data points in the response. However my second test case I may want to only make the first async call and test some data points in the response.
In postman all these tests are replicated as there are no way to reuse them, so I've consolidated them into their own files (each request is its own file, and groups of related tests are in are their own as well), and am trying to use require to run them.
In my main.js, I've set up the following based on what I've found on stack overflow here and here. I've also tried using beforeEach as shown here as i've be ok with just always running the same 3 async requests with all tests cases. I tried following this approach as well, but I still get the same issue of none of the tests executing in the require files.
Here's how I have my main.js set up:
async function doRequest(path,requestObj) {
return await require(path)(requestObj);
}
async function doTest(path, response, options){
return await require(path)(response,options);
}
async function runTestCase(path, requestObj, tests){
let response = await doRequest(path, requestObj);
for(var i=0; i<tests.length; i++){
doTest(tests[i].path, response, tests[i].options);
}
}
for(i=0; i<global.testDataArray.length; i++){
jsonRequest.setRequestData(global.testDataArray[i].prop1, global.testDataArray[i].prop2);
testTitle = 'Testing ' + global.testDataArray[i].testCase + " using: " + jsonRequest.getRequestData().prop1 + ":" + jsonRequest.getRequestData().prop2;
curTestConfig = global.testConfig.testCases[global.testDataArray[i].testCase];
tests = curTestConfig.steps;
describe(testTitle, function () {
// TESTS
this.timeout(global.constants.timeout);
runTestCase(tests[i].requestPath,jsonRequest.getRequestData(),tests[i].tests);
});
}
My test config is set up like this:
module.exports = {
testCases : {
'Test Case 1' : {
steps : [
{ requestPath : '../test/lib/requests/search.js',
tests : [
{ path : '../test/lib/test1.js', options : { response: 200 } },
{ path : '../test/lib/test2.js', options : { value: 100 } },
{ path : '../test/lib/test3.js', options : { hasLink: true} }
]
},
{ requestPath : '../test/lib/requests/getDetails.js',
tests : [
{ path : '../test/lib/test1.js', options : { response: 200 } },
{ path : '../test/lib/test2.js', options : { value: 365 } },
{ path : '../test/lib/test3.js', options : { hasLink: false} }
]
}
]
},
'Test Case 2' : {
steps : [
{ requestPath : '../test/lib/requests/search.js',
tests : [
{ path : '../test/lib/test1.js', options : { response: 200 } },
{ path : '../test/lib/test3.js', options : { hasLink: false} }
]
},
{ requestPath : '../test/lib/requests/setDetails.js',
tests : [
{ path : '../test/lib/test1.js', options : { response: 200 } },
{ path : '../test/lib/test4.js', options : { fName: "jane", lName: "doe" } },
{ path : '../test/lib/test5.js', options : { location: "usa"} }
]
}
]
}
}
}
and the test files (test1.js in the below) are set up like this:
let chai = require('chai');
let chaiHttp = require('chai-http');
let chaiXml = require('chai-xml');
chai.use(chaiHttp);
chai.use(chaiXml);
let expect = require('chai').expect;
let should = chai.should();
module.exports = function(result, options) {
var testTitle = 'should have a status of ' + options.statusCode;
it(testTitle, function () {
expect(result).to.have.status(options.statusCode);
});
};
It doesn't seem like I'm trying to do anything that node, mocha/chai aren't capable of doing, but none of the it()'s ever execute. thanks for any help in advance.