-
-
Notifications
You must be signed in to change notification settings - Fork 42
fix: await Meteor.startup() queue before running tests #177
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -204,7 +204,14 @@ function clientTests() { | |
| } | ||
|
|
||
| // Before Meteor calls the `start` function, app tests will be parsed and loaded by Mocha | ||
| function start() { | ||
| async function start() { | ||
| // Wait for all Meteor.startup() callbacks (including async ones) to complete. | ||
| // In Meteor 3.x, async startup callbacks and top-level await can cause the | ||
| // startup queue to still be draining when the test driver's start() is called. | ||
| // Adding a callback at the end of the queue ensures it runs after all prior | ||
| // callbacks have finished. See: https://github.com/Meteor-Community-Packages/meteor-mocha/issues/176 | ||
| await new Promise(resolve => Meteor.startup(resolve)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if any previously-registered |
||
|
|
||
| const args = setArgs(); | ||
| runnerOptions = args.runnerOptions; | ||
| coverageOptions = args.coverageOptions; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* eslint-env mocha */ | ||
| import { Meteor } from 'meteor/meteor'; | ||
| import assert from 'assert'; | ||
|
|
||
| // Simulate async startup work (e.g. ensuring MongoDB indices, initializing | ||
| // collections). Without the fix in server.js, mocha.run() can fire before | ||
| // this callback completes, causing the test below to fail. | ||
| let startupCompleted = false; | ||
|
|
||
| Meteor.startup(async () => { | ||
| await new Promise(resolve => setTimeout(resolve, 50)); | ||
| startupCompleted = true; | ||
| }); | ||
|
|
||
| describe('async Meteor.startup()', function () { | ||
| it('should complete before tests run', function () { | ||
| assert.strictEqual(startupCompleted, true, | ||
| 'Async Meteor.startup() callback did not complete before tests ran. ' + | ||
| 'See https://github.com/Meteor-Community-Packages/meteor-mocha/issues/176'); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The returned Promise from start() is silently discarded in
packages/meteor/test_environment.js:59-61Making
start()async means it now returns a Promise. The Meteor framework does not await it. Any rejection inside start() becomes an unhandled Promise rejection (which in Node 18+ terminates the process with a cryptic error, not the actual stack trace).I reccommend you to handle it in test_enviroment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to avoid the async/await snallbal, try to handle it like