CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

Creating async test cases

Open tjoskar opened this issue 10 years ago • 10 comments

Hi,

Is it possible write async testcase?

Lets say I want to make a HTTP request in a test case:

function httpRequest() {
    // Make a fake http request
    return new Promise(resolve => {
        setTimeout(() => {
            console.log('Data from server');
            resolve('some data');
        }, 5000);
    });
}

Scenario(`Test case`, function*(I) {
    const data = yield httpRequest();
    console.log('Test continues');
    // Some asserts
});

I expected the output to be:

Data from server
Test continues

but it is:

Test continues
Data from server

I would also want the cast case to wait for a returning promise to resolve/reject:

Scenario(`Test case2`, function(I) {
    return httpRequest()
        .then(() => {
            console.log('Test continues');
            // Some asserts
        });
});

It would also be nice if the Before method waits for the promise to resolve/reject:

Before(I => new Promise(resolve => setTimeout(resolve), 5000));

Similar, I expect the testcase to fail if a promise rejects. Is this possible?

Maybe duplicate of: #46

tjoskar avatar May 10 '16 12:05 tjoskar

Was this solved???

Im trying to use console.logs to separate my scenario steps but it all gets written before the steps gets executed.

ecampii avatar Aug 01 '16 05:08 ecampii

@ecampii, I don't think this was ever solved. I was looking into the matter but could not find a simple solution (without rewriting the current async-handler).

tjoskar avatar Aug 02 '16 19:08 tjoskar

@tjoskar - was this ever progressed? I'd like to do some async setup in a test case before doing the tests but can't find a way to do this in Codeceptsjs. Thanks for youre time.

harkinj avatar Nov 20 '16 21:11 harkinj

@harkinj, unfortunately not. Not from my side anyway. I went over to nightmare (and testcafe) to be able to write async setups.

tjoskar avatar Nov 21 '16 05:11 tjoskar

@tjoskar - thanks for your prompt response.I like codeceptjs but this lack of ability to write async setup code is a big missing.Just curious - have u moved off codeceptjs completely for this main reason? Thanks for youre time.

harkinj avatar Nov 21 '16 09:11 harkinj

Hey, sorry, I didn't notice this issue, and didn't provide response in time.

I think what you are trying to achieve can be easily done with CodeceptJS however you will need to stick to its abstractions like Helpers.

Everything in CodeceptJS is scenario-driven so any calls like httpRequest should be called inside I. chain, like: I.sendHttpRequest(). To make this function inside of I you will need to create a helper and enable it in config. Once you do it the method is available. All methods of I. are grouped into a promise chain, this way we ensure that everything is executed synchronously.

There is no need to work with promises the way you did it in Mocha or whenever else. The idea of CodeceptJS test is to hide the technical implementation and focus on test scenarios.

Once again: Everything inside I.xxx calls is synchronous and executed inside promise chain. To implement custom logic on top of that use helpers

DavertMik avatar Nov 21 '16 11:11 DavertMik

@DavertMik (follow up question from @harkinj) I created a helper with an async function (using set timeout), but it doesn't run sync, do I need to return a promise?

shamusharkin avatar Nov 22 '16 17:11 shamusharkin

@harkinj: The reason that I stopped using codeceptjs was a combination of several issues (all of which seems to be solved now) and the fact that I didn't have time to help and fix the issue at the time. What I really love with codeceptjs is however the fact that you can change the target-client (selenium, nightmare, etc) with very little/none effort and the support of x-path.

And as @DavertMik pointed out, it is possible to write test cases with custom async methods (I have not tried yet), so I will give codeceptjs an another chance.

tjoskar avatar Nov 22 '16 17:11 tjoskar

do I need to return a promise?

yes.

@tjoskar thanks! :+1:

Anyway, this project needs some love from community. I wish I had more time to work on it, and implement all the good ideas out there and reply on all issues. Anyway, we are moving forward step-by-step but moving :)

I think I can try to implement the idea you suggested: handling of returned promise in Before/Scenario for async execution. That is how Mocha works, so probably it can be implemented here as well

DavertMik avatar Nov 22 '16 21:11 DavertMik

@DavertMik - Thanks for your responses and the response to @shamusharkin helped.

harkinj avatar Nov 24 '16 19:11 harkinj