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

Error: All tests and hooks must be declared synchronously in your test file, and cannot be nested within other tests or hooks. #1944

Closed
Christilut opened this issue Sep 30, 2018 · 4 comments

Comments

@Christilut
Copy link

After the upgrade from 0.25.0 to 1.0.0-beta8 I'm getting this error because I have a before and after.always hook in a helper file:

test.before(async t => {
  const uri = await mongod.getConnectionString()

  await mongoose.connect(uri, {
    useNewUrlParser: true
  })
})

test.after.always(async t => {
  mongoose.connection.db.dropDatabase()
})

But I'm getting this error Error: All tests and hooks must be declared synchronously in your test file, and cannot be nested within other tests or hooks. now.
I used to import the above helper file in every test file.

I'm wondering what the proper approach is to set up a global before and after hook because I only want to init and destroy my database once... I don't want to do this per file because mongoose will be confused and I want all my tests to run in parallel on the same database since that is a real life scenario that I want to test.

How should I structure this?

@novemberborn
Copy link
Member

I'm wondering what the proper approach is to set up a global before and after hook because I only want to init and destroy my database once... I don't want to do this per file

Could you give more context on how you're using this helper file? Are you putting it in the require config? Or importing from a test file?

As long as you import the helper file, from the test file, it should still work. (Unless this is wrapped in a function which you're calling asynchronously.)

However I think you're implying an expectation that these hooks were only run once per test run. That's definitely not the case. Even the require config is executed per worker process, and each test file gets its own worker. You should spin the database up before you run AVA, and spin it down after.

(I'm closing this issue for housekeeping purposes, but let's keep the conversation going.)

@Christilut
Copy link
Author

Okay so every file has its own mongoose instance. Is it then possible to have all files use the same instance of https://github.com/nodkz/mongodb-memory-server ? Since it would launch a new one for every worker instance, I'm not sure how to re-use an existing one in different workers.

@novemberborn
Copy link
Member

Is it then possible to have all files use the same instance of https://github.com/nodkz/mongodb-memory-server ?

No. But then ideally your test files are independent of each other, so that shouldn't matter.

@Christilut
Copy link
Author

Christilut commented Oct 1, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants