|
|
@@ -1,7 +1,7 @@
|
|
|
/* eslint-disable no-console */
|
|
|
import { INestApplicationContext } from '@nestjs/common';
|
|
|
import { LanguageCode } from '@vendure/common/lib/generated-types';
|
|
|
-import { VendureConfig } from '@vendure/core';
|
|
|
+import { ConfigService, isInspectableJobQueueStrategy, VendureConfig } from '@vendure/core';
|
|
|
import { importProductsFromCsv, populateCollections, populateInitialData } from '@vendure/core/cli';
|
|
|
|
|
|
import { TestServerOptions } from '../types';
|
|
|
@@ -23,6 +23,7 @@ export async function populateForTesting<T extends INestApplicationContext>(
|
|
|
config.authOptions.requireVerification = false;
|
|
|
|
|
|
const app = await bootstrapFn(config);
|
|
|
+ await awaitOutstandingJobs(app);
|
|
|
|
|
|
const logFn = (message: string) => (logging ? console.log(message) : null);
|
|
|
|
|
|
@@ -35,6 +36,40 @@ export async function populateForTesting<T extends INestApplicationContext>(
|
|
|
return app;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Sometimes there will be jobs created during the bootstrap process, e.g. when
|
|
|
+ * a plugin needs to create certain entities during bootstrap, which might then
|
|
|
+ * trigger e.g. search index update jobs. This can lead to very hard-to-debug
|
|
|
+ * failures in e2e tests suites (specifically at this moment, consistent failures
|
|
|
+ * of the sql.js tests on Node v20).
|
|
|
+ *
|
|
|
+ * This function will wait for all outstanding jobs to finish before returning.
|
|
|
+ */
|
|
|
+async function awaitOutstandingJobs(app: INestApplicationContext) {
|
|
|
+ const { jobQueueStrategy } = app.get(ConfigService).jobQueueOptions;
|
|
|
+ const maxAttempts = 10;
|
|
|
+ let attempts = 0;
|
|
|
+ if (isInspectableJobQueueStrategy(jobQueueStrategy)) {
|
|
|
+ const inspectableJobQueueStrategy = jobQueueStrategy;
|
|
|
+
|
|
|
+ function waitForJobQueueToBeIdle() {
|
|
|
+ return new Promise<void>(resolve => {
|
|
|
+ const interval = setInterval(async () => {
|
|
|
+ attempts++;
|
|
|
+ const { items } = await inspectableJobQueueStrategy.findMany();
|
|
|
+ const jobsOutstanding = items.filter(i => i.state === 'RUNNING' || i.state === 'PENDING');
|
|
|
+ if (jobsOutstanding.length === 0 || attempts >= maxAttempts) {
|
|
|
+ clearInterval(interval);
|
|
|
+ resolve();
|
|
|
+ }
|
|
|
+ }, 500);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ await waitForJobQueueToBeIdle();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
async function populateProducts(
|
|
|
app: INestApplicationContext,
|
|
|
productsCsvPath: string | undefined,
|