I am creating some API tests for the product I am creating. One of the tests is as follows:
GET FILTERS β should be restricted (45ms) it should get the filters β should return 200 β should return an object β should close db connections GET USERS COUNT β should be restricted β should throw error when payload is not correct it should get the user count β should return 200 β should return an object β should close db connections GET USERS FILE β should be restricted β should throw error when no queryId is specified it should retrieve the file β should return 200 β should download an excel file β should close db connections UPLOAD PROMOTION IMAGE β should throw error when no file is specified it should save the file β should return 200 β should have named the file with the title of the promotion β should have uploaded the file to S3 (355ms) β should close db connections CREATE PROMOTION it should save the promotion β should return 200 β should return a correct response β should close db connections GET PROMOTIONS β should be restricted it should get the promotions β should return 200 β should be an array of promotions β should contain the previously created promotion UPDATE PROMOTION it should update the promotion β should return 200 β should return a correct response β should close db connections PUT PROMOTION IN BIN it should put the promotion in the bin β should return 200 β should return a correct response β should close db connections GET ARCHIVED PROMOTIONS β should be restricted it should get the promotions β should return 200 β should be an array of promotions β should be an array of archived promotions β should contain the previously archived promotion DELETE PROMOTION it should delete the promotion β should return 200 β should return a correct response β should have deleted the file from S3 (563ms) β should close db connections
As you can see, I tried to put everything related to promotion in one test suite, so that I had some kind of workflow to check what the user would do on the platform.
In this example, I create an ad campaign with an arbitrary creation, and then use the identifier of this advertisement to read it, update it, archive it, and then finally delete it. Each step is connected, and I need to have return values ββfor each costume (i.e.: ID of the inserted promotion or filters ...)
At this point, my promotionions.test.js file is 625, and since I haven't finished yet, I expect it to grow in the coming days.
Is there a way to separate multiple test masks in different files, but so that each test / file can return, as soon as it finishes, a value that I can pass to the next step?
EDIT FOR BOUNTY
At the moment, I just tried something like this:
describe.only("Gifts Workflow", function() { var createdGift; describe("CREATE", function() { require("./GIFTS/CREATE.js")().then(function(data) { createdGift = data; }); }); describe("READ FROM WEB", function() { require("./GIFTS/READ FROM WEB.js")(createdGift).then(function(data) { }); }); });
Content "./GIFTS/CREATE.js"
module.exports = function() { return new Promise(function(resolve, reject) {
};
The problem is that the test is immediately initialized wet, so in the second set of tests "READ FROM WEB" the value passed as createdGift is immediately passed to the test, without waiting for the completion of the first test, and therefore undefined is passed.
Answer Jankapunkt
Here is how I tried in my code:
var create = require("./GIFTS/CREATE"); var read = require("./GIFTS/READ FROM WEB"); describe.only("Gifts Workflow", function() { create(function(createdGift) { read(createdGift); }); });
CREATE
module.exports = function(callback) { var createdGift;
READ FROM WEB
module.exports = function(createdGift) { it("should be restricted", function(done) { request .get(url) .query({}) .end(function(err, res) { expect(res.statusCode).to.equal(400); done(); }); }); describe("it should read all gifts", function() {
And this is the conclusion
Gifts Workflow β should be restricted β should not work when incomplete payload is specified it should insert a gift β should return 200 β should return an object β should have uploaded the image to S3 (598ms) β should close db connections it should read all gifts β should return 200 β should return an array β should contain the previously added gift β should close db connections 10 passing (3s)
It may seem like it works, but, as you can see from the tab, it should read all the gifts, not the Workflow children, but is a child of the root package.
Here's what happens:
- Mocha calls the root package
- Mocha finds the Workflow Gifts collection and executes the create () function inside this package.
- Because the function is asynchronous, Mocha believes the Day Workflow set is complete and returns to the root package
- read ()
- Mocha exits the root package and proceeds to the next tests, because, being asynchronous, it considers that all tests are complete.
- Test No. 3,4,5, ... are never called
Can you confirm that this is your case with more than two tests?