Jest: Async setupFiles

Created on 28 Apr 2016  路  6Comments  路  Source: facebook/jest

It would be useful to be able to wait for an asynchronous completion of a setupFiles module before starting tests. Is there a way to do that?

Most helpful comment

I don't think we'll support this as it makes everything dealing with the setup a bit more messy.

The way I would suggest to work around this is something like this in your setupFile:

global.it = function(description, fn) {
  pit(description, () => {
    // Assuming it is a promise
    return loadEnvironmentAsync().then(
      () => fn()
    )
  });
}

This will make everyone of your tests async. If you are not using pit you can use it instead (by doing const it = global.it; beforehand to save the jasmine function before). Then you have to use the done flag to work with async operations.

Let me know if this isn't a satisfying solution

All 6 comments

This is not possible. Can you elaborate on why you'd like to do this?

I see, thanks. Just a an async function that preps the test environment that I can't make synchronous unfortunately.

I don't think we'll support this as it makes everything dealing with the setup a bit more messy.

The way I would suggest to work around this is something like this in your setupFile:

global.it = function(description, fn) {
  pit(description, () => {
    // Assuming it is a promise
    return loadEnvironmentAsync().then(
      () => fn()
    )
  });
}

This will make everyone of your tests async. If you are not using pit you can use it instead (by doing const it = global.it; beforehand to save the jasmine function before). Then you have to use the done flag to work with async operations.

Let me know if this isn't a satisfying solution

I just hit a use-case for this where I needed to do some async logic to determine which host the test suite should be running against. I will rework my solution to have the script expose data in an ENV variable for now. Would have been nice to have this though : )

Oh amazing https://github.com/facebook/jest/issues/3832#issuecomment-351463497.

Thanks @xfumihiro!!

@jasonkuhrt happy coding! 馃槃

Was this page helpful?
0 / 5 - 0 ratings