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

Unique ports for each test server instance #3276

Closed
Closed
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
52 changes: 35 additions & 17 deletions e2e-common/test-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ 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 { globSync } from 'glob';

import { getPackageDir } from './get-package-dir';

Expand All @@ -36,25 +35,44 @@ 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];
// this initial port value will be incremented according to which test we're
// running
let testPort = 3010;

// this regex extracts the filename of the e2e test we're running from the
// call stack of this function
const testFileNameRegex = /[\w-]+\.e2e-spec\.ts/;

// get the call stack of this function and find the filename of the e2e test
// we're running in it.

// `Error.prototype.stack` is not strictly standardized but is supported by
// all major JavaScript engines, including Node.js since version 0.10.0:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack
const stack = new Error().stack;
if (!stack) {
throw new Error('could not get caller from stack in Error object');
}
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];
const testFileNameMatch = stack.match(testFileNameRegex);
if (!testFileNameMatch) {
throw new Error("could not derive the current test file's name from this stack: \n" + stack);
}
fs.writeJSONSync(portsFile, usedPorts);
const testFileName = testFileNameMatch[0];

// get an array of all of the e2e test files in this project, and find the
// current test's position in it
const e2eTests = globSync('../**/*.e2e-spec.ts')
.sort()
.map(testFilePath => path.basename(testFilePath));
const thisTestsIndex = e2eTests.indexOf(testFileName);

// increment the test port by the position of this test in the array of all
// tests, to ensure that this test gets a unique port value
testPort += thisTestsIndex;

return mergeConfig(defaultTestConfig, {
apiOptions: {
port: nextPort,
port: testPort,
},
importExportOptions: {
importAssetsDir: path.join(packageDir, 'fixtures/assets'),
Expand Down
Loading