diff --git a/.gitignore b/.gitignore index 31b027bfcb..5da5af9029 100644 --- a/.gitignore +++ b/.gitignore @@ -19,7 +19,6 @@ docs/static/intro.css* docs/public docs/data/build.json yarn-error.log -e2e-common/ports.json # Sensitive or high-churn files: .idea/**/dataSources/ diff --git a/docs/docs/guides/developer-guide/testing/index.md b/docs/docs/guides/developer-guide/testing/index.md index 02cd50a0fa..4bdf097f49 100644 --- a/docs/docs/guides/developer-guide/testing/index.md +++ b/docs/docs/guides/developer-guide/testing/index.md @@ -195,7 +195,7 @@ All that's left is to run your tests to find out whether your code behaves as ex :::caution **Note:** When using **Vitest** with multiple test suites (multiple `.e2e-spec.ts` files), it will attempt to run them in parallel. If all the test servers are running -on the same port (the default in the `testConfig` is `3050`), then this will cause a port conflict. To avoid this, you can manually set a unique port for each test suite. Be aware that `mergeConfig` is used here: +on the same port (the default in the `testConfig` is `3050`), then this will cause a port conflict. To avoid this, you can use the `VITEST_WORKER_ID` environment variable to set a unique port for each test suite. Be aware that `mergeConfig` is used here: ```ts title="src/plugins/my-plugin/e2e/my-plugin.e2e-spec.ts" import { createTestEnvironment, testConfig } from '@vendure/testing'; @@ -208,7 +208,7 @@ describe('my plugin', () => { const {server, adminClient, shopClient} = createTestEnvironment(mergeConfig(testConfig, { // highlight-start apiOptions: { - port: 3051, + port: 3050 + Number(process.env.VITEST_WORKER_ID), }, // highlight-end plugins: [MyPlugin], diff --git a/e2e-common/test-config.ts b/e2e-common/test-config.ts index 06f594a32a..5626c5f6bb 100644 --- a/e2e-common/test-config.ts +++ b/e2e-common/test-config.ts @@ -6,10 +6,8 @@ import { SqljsInitializer, testConfig as defaultTestConfig, } from '@vendure/testing'; -import fs from 'fs-extra'; import path from 'path'; import { DataSourceOptions } from 'typeorm'; -import { fileURLToPath } from 'url'; import { getPackageDir } from './get-package-dir'; @@ -36,25 +34,16 @@ registerInitializer('mysql', new MysqlInitializer()); registerInitializer('mariadb', new MysqlInitializer()); export const testConfig = () => { - // @ts-ignore - const portsFile = fileURLToPath(new URL('ports.json', import.meta.url)); - fs.ensureFileSync(portsFile); - let usedPorts: number[]; - try { - usedPorts = fs.readJSONSync(portsFile) ?? [3010]; - } catch (e: any) { - usedPorts = [3010]; + const testWorkerId = Number(process.env.VITEST_WORKER_ID); + if (!testWorkerId || isNaN(testWorkerId)) { + console.warn( + `VITEST_WORKER_ID has value ${testWorkerId} in the environment; ` + + "port numbers can't be reliably allocated to test suites", + ); } - const nextPort = Math.max(...usedPorts) + 1; - usedPorts.push(nextPort); - if (100 < usedPorts.length) { - // reset the used ports after it gets 100 entries long - usedPorts = [3010]; - } - fs.writeJSONSync(portsFile, usedPorts); return mergeConfig(defaultTestConfig, { apiOptions: { - port: nextPort, + port: 3010 + testWorkerId, }, importExportOptions: { importAssetsDir: path.join(packageDir, 'fixtures/assets'),