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

fix(e2e): Use VITEST_WORKER_ID to generate test server ports #3248

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/guides/developer-guide/testing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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],
Expand Down
25 changes: 7 additions & 18 deletions e2e-common/test-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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'),
Expand Down
Loading