Mocha test case - nested functions () kosher functions?

I have this case where I think I want to have nested it() test cases in a Mocha test. I am sure that this is wrong, and I do not see any recommendations to do what I am doing, but in fact I do not know the best way at the moment -

Basically, I have a “parent” test, and inside the parent test there is a forEach loop with all the “child” tests:

 it('[test] enrichment', function (done) { var self = this; async.each(self.tests, function (json, cb) { //it('[test] ' + path.basename(json), function (done) { var jsonDataForEnrichment = require(json); jsonDataForEnrichment.customer.accountnum = "8497404620452729"; jsonDataForEnrichment.customer.data.accountnum = "8497404620452729"; var options = { url: self.serverURL + ':' + self.serverPort + '/event', json: true, body: jsonDataForEnrichment, method: 'POST' }; request(options,function (err, response, body) { if (err) { return cb(err); } assert.equal(response.statusCode, 201, "Error: Response Code"); cb(null); }); //}); }, function complete(err) { done(err) }); }); 

as you can see, two separate lines are commented out - I want to include them so that I can easily see the results of each individual test, but then I get such an uncomfortable situation when you run a callback for a test along with a callback for async.each.

Has anyone seen this situation time before and knows a good solution where a tester can easily see the results of each test in a cycle?

+5
source share
2 answers

Do not embed it . Call them synchronously.

Nested it calls in Mocha will never be normal. Also, it calls not made asynchronously. (The test may be asynchronous, but you cannot invoke it asynchronously.) Here's a simple test:

 describe("level 1", function () { describe("first level 2", function () { it("foo", function () { console.log("foo"); it("bar", function () { console.log("bar"); }); }); setTimeout(function () { it("created async", function () { console.log("the asyncly created one"); }); }, 500); }); describe("second level 2", function () { // Give time to the setTimeout above to trigger. it("delayed", function (done) { setTimeout(done, 1000); }); }); }); 

If you run this, you will not get a nested bar test that will be ignored, and a test created asynchronously ( delayed ) will also be ignored.

Mocha has no specific semantics for these kinds of challenges. When I ran my test with the latest version of Mocha at the time of writing (2.3.3), it simply ignored them. I remember that an earlier version of Mocha would recognize the tests, but attach them to the incorrect describe block.

+5
source

I think the need for dynamic tests is relatively common (data-based tests?), And for dynamic it and test cases there is a general application.

I think it would be easier to control the completion of a test test if the tests could be executed sequentially. This way you don’t have to worry about managing nested async done . Since request is async (I assume), your test cases will mostly run at the same time.

 describe('[test] enrichment', function () { var self = this; _.each(self.tests, function (json, cb) { it('[test] ' + path.basename(json), function (done) { var jsonDataForEnrichment = require(json); jsonDataForEnrichment.customer.accountnum = "8497404620452729"; jsonDataForEnrichment.customer.data.accountnum = "8497404620452729"; var options = { url: self.serverURL + ':' + self.serverPort + '/event', json: true, body: jsonDataForEnrichment, method: 'POST' }; request(options,function (error, response, body) { if (error) { cb(error); } else{ assert.equal(response.statusCode, 201, "Error: Response Code"); cb(null); } done(); }); }); } }); 
+3
source

Source: https://habr.com/ru/post/1232145/


All Articles