Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initializing the describe and it blocks #4965

Closed
kot99a opened this issue Feb 16, 2023 · 1 comment
Closed

Initializing the describe and it blocks #4965

kot99a opened this issue Feb 16, 2023 · 1 comment
Labels
type: question support question

Comments

@kot99a
Copy link

kot99a commented Feb 16, 2023

However, in my case version is gotten from an async call - which I tried to put in before. However, it looks like the "it" test is done before before returns .

describe('some test suite', function () {
     let response;

     before('precondition', async function () {
         response = await getHttpResponse();        //some async http get req which returns {status: true, data: ...}
     });

     (response.status ? it : it.skip)('test', async function () {
             // do the test  
     });
 });

I get the error TypeError: Cannot read property 'status' of undefined because response is undefined ;)

How does the block initialization happen and how can I solve it? I see that for example the status in my precondition is equal true, but the test is initialized with response === undefined

I saw a similar question years ago, but could not find a solution:
#591 (comment)

@kot99a kot99a added the type: question support question label Feb 16, 2023
@juergba
Copy link
Member

juergba commented Feb 19, 2023

Mocha runs in several cycles. In one (synchronous) phase the test file is parsed and the test hierarchy built. In a later phase the test and hooks are executed. Note that the descibe block is always synchronous, tests and hooks can be both sync/async.

Please see our docs, especially ["When a test file is loaded, Mocha executes all of its suites and finds – but does not execute – any hooks and tests therein."]

For dynamically skipping test at runtime, use this.skip() instead.

@juergba juergba closed this as completed Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question support question
Projects
None yet
Development

No branches or pull requests

2 participants