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

Procedurally generated tests with an async dependency #1670

Closed
calebmer opened this issue Sep 10, 2016 · 4 comments
Closed

Procedurally generated tests with an async dependency #1670

calebmer opened this issue Sep 10, 2016 · 4 comments

Comments

@calebmer
Copy link
Contributor

calebmer commented Sep 10, 2016

I have a project which will automatically generate an API based on some schema data which must be fetched asynchronously. I’d like to provide a testing interface for these automatically generated APIs, say the function testApi(api) which will run the necessary describes and its to create an entire test suite for that api. So here’s an example:

// `testApi` is being called in the global file scope,
// `await` is just demonstrating what I want to do here.
testApi(await generateApi())

function testApi (api) {
  describe(api.name, () => {
    api.getRoutes().forEach(route => {
      describe(route.path, () => {
        it('works', () => {
          expect(true).toBe(true)
        })
      })
    })
  })
}

My first idea was to do something like this:

let api

beforeAll(async () => {
  api = await generateApi()
})

describe('my API tests', () => {
  testApi(api)
})

However, there are two incorrect assumptions here, first is that Jest supports beforeAll (as far as I know) and that describe is executed lazily (i.e. after beforeAll resolves). Any ideas?

This is most definitely a niche use case, but I’d like to hear if there is a solution and/or interest to support this use case.

@cpojer
Copy link
Member

cpojer commented Sep 11, 2016

In Jasmine you need to define all your tests (calls to it or describe) before the end of the test. This is a limitation we can't do so much about right now. Can you make it so your tests are generated synchronously? beforeAll definitely does work in Jest, however and can be made async too. beforeAll is called before the first call to it.

@aaronabramov
Copy link
Contributor

if your definition is fetched asynchronously there is nothing we can really do to make it work :(
i think the best solution would be to stick it all into one test case and provide nice messages if something failed:

test('some api', () => {
  generateApi().then(api => {
    return promiseAll(api.routes.map(route => {
      return new Promise(route => invariant(true, `${api.name} ${route.name} test failed because of something`));
    });
  });
});

@calebmer
Copy link
Contributor Author

Yeah, probably the direction I’ll go. Although I have been witnessing a Twitter firestorm recently on top level awaits lol, maybe one day that can be a solution 🙃

(lot’s of sarcasm, please don’t take seriously)

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants