使用ng-bullet的configureTestSuite方法出现异步函数弃用错误的解决求助
configureTestSuite I’ve run into this exact frustration with Angular 11, Karma 6, and Jasmine 3.6—trying to keep test speeds fast with ng-bullet while fighting that annoying deprecation warning (treated as an error). Let’s break down the issue and fix it without ditching ng-bullet:
Why the Warning Happens
Jasmine 3.6+ enforces a strict rule: asynchronous test hooks (like beforeAll) can’t both return a promise and use a done callback. ng-bullet’s configureTestSuite uses beforeAll with a done callback under the hood, but your code returns the promise from compileComponents()—this creates the conflict that triggers the deprecation warning.
Solutions to Keep ng-bullet and Eliminate the Warning
1. Use async/await in the configureTestSuite Callback
Instead of returning the promise directly, wrap the configuration in an async function and await compileComponents(). This stops the callback from returning a promise while letting ng-bullet handle async flow correctly:
import { TestBed } from '@angular/core/testing'; import { configureTestSuite } from 'ng-bullet'; import { ToastComponent } from './toast.component'; describe('ToastComponent', () => { configureTestSuite(async () => { await TestBed.configureTestingModule({ declarations: [ToastComponent], }).compileComponents(); }); it('should create', () => { console.log('do nothing'); }); });
2. Remove compileComponents() (If You Don’t Need It)
If you’re using Angular CLI with default settings, your component templates are already inlined during the build process—meaning compileComponents() is totally unnecessary. Removing it eliminates the promise entirely, which fixes the warning instantly:
configureTestSuite(() => { TestBed.configureTestingModule({ declarations: [ToastComponent], }); });
This is my go-to fix because it simplifies the code and preserves all of ng-bullet’s speed benefits.
3. Patch ng-bullet’s configureTestSuite (Advanced)
If the above options don’t fit your setup, create a custom version of configureTestSuite that avoids the done/promise conflict. Add this utility function to a shared test file:
export function configureTestSuite(callback: () => void | Promise<void>) { const suite = jasmine.getEnv().currentSuite; if (!suite) { throw new Error('configureTestSuite must be called inside a describe block'); } suite.beforeAll(async () => { const callbackResult = callback(); if (callbackResult instanceof Promise) { await callbackResult; } }); }
Then import this custom function instead of the one from ng-bullet. This implementation uses async/await directly in beforeAll without relying on a done callback, aligning perfectly with Jasmine’s current rules.
Verify the Fix
After applying any of these changes, run your tests again—the deprecation warning should vanish, and you’ll still get the fast test execution that makes ng-bullet worth keeping.
内容的提问来源于stack exchange,提问作者christina

