Skip to content

Commit 01e1b9c

Browse files
committed
test: separate test vs test setup execution
1 parent df2ca20 commit 01e1b9c

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

tests/legacy-cli/e2e_runner.ts

+43-23
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ const tests = allTests.filter((name) => {
9898
});
9999

100100
// Remove tests that are not part of this shard.
101-
const shardedTests = tests.filter((name, i) => shardId === null || i % nbShards == shardId);
102-
const testsToRun = allSetups.concat(shardedTests);
101+
const testsToRun = tests.filter((name, i) => shardId === null || i % nbShards == shardId);
103102

104-
if (shardedTests.length === 0) {
103+
if (testsToRun.length === 0) {
105104
console.log(`No tests would be ran, aborting.`);
106105
process.exit(1);
107106
}
@@ -114,7 +113,7 @@ console.log(testsToRun.join('\n'));
114113
if (testsToRun.length == allTests.length) {
115114
console.log(`Running ${testsToRun.length} tests`);
116115
} else {
117-
console.log(`Running ${testsToRun.length} tests (${allTests.length + allSetups.length} total)`);
116+
console.log(`Running ${testsToRun.length} tests (${allTests.length} total)`);
118117
}
119118

120119
setGlobalVariable('argv', argv);
@@ -131,8 +130,16 @@ Promise.all([findFreePort(), findFreePort()]).then(async ([httpPort, httpsPort])
131130
let lastTestRun: string | null = null;
132131

133132
try {
133+
for (const [setupIndex, setup] of allSetups.entries()) {
134+
printSetupHeader(setup, setupIndex, allSetups.length);
135+
136+
await runSetup((lastTestRun = setup));
137+
}
138+
134139
for (const [testIndex, test] of testsToRun.entries()) {
135-
await runTest((lastTestRun = test), testIndex);
140+
printTestHeader(test, testIndex, testsToRun.length);
141+
142+
await runTest((lastTestRun = test));
136143
}
137144

138145
console.log(colors.green('Done.'));
@@ -162,7 +169,7 @@ Promise.all([findFreePort(), findFreePort()]).then(async ([httpPort, httpsPort])
162169

163170
function normalizeTestStep(relativeName: string) {
164171
// Make sure this is a windows compatible path.
165-
let absoluteName = path.join(e2eRoot, relativeName);
172+
let absoluteName = path.join(e2eRoot, relativeName).replace(/\.ts$/, '');
166173
if (/^win/.test(process.platform)) {
167174
absoluteName = absoluteName.replace(/\\/g, path.posix.sep);
168175
}
@@ -172,7 +179,18 @@ function normalizeTestStep(relativeName: string) {
172179
return { absoluteName, currentFileName };
173180
}
174181

175-
async function runTest(relativeName: string, testIndex: number) {
182+
async function runSetup(relativeName: string) {
183+
const { absoluteName, currentFileName } = normalizeTestStep(relativeName);
184+
const start = +new Date();
185+
186+
const module = require(absoluteName);
187+
188+
await (typeof module === 'function' ? module : module.default)();
189+
190+
printFooter(currentFileName, start);
191+
}
192+
193+
async function runTest(relativeName: string) {
176194
const { absoluteName, currentFileName } = normalizeTestStep(relativeName);
177195
const start = +new Date();
178196

@@ -190,8 +208,6 @@ async function runTest(relativeName: string, testIndex: number) {
190208
throw new Error('Invalid test module.');
191209
};
192210

193-
printHeader(currentFileName, testIndex);
194-
195211
let clean = true;
196212
let previousDir = process.cwd();
197213
try {
@@ -205,19 +221,18 @@ async function runTest(relativeName: string, testIndex: number) {
205221

206222
console.log('----');
207223

208-
// If we're not in a setup, change the directory back to where it was before the test.
224+
// Change the directory back to where it was before the test.
209225
// This allows tests to chdir without worrying about keeping the original directory.
210-
if (!allSetups.includes(relativeName) && previousDir) {
226+
if (previousDir) {
211227
process.chdir(previousDir);
212228

213229
// Restore env variables before each test.
214230
console.log(' Restoring original environment variables...');
215231
process.env = originalEnvVariables;
216232
}
217233

218-
// Only clean after a real test, not a setup step. Also skip cleaning if the test
219-
// requested an exception.
220-
if (!allSetups.includes(relativeName) && clean) {
234+
// Skip cleaning if the test requested an exception.
235+
if (clean) {
221236
logStack.push(new logging.NullLogger());
222237
try {
223238
await gitClean();
@@ -234,19 +249,24 @@ async function runTest(relativeName: string, testIndex: number) {
234249
}
235250
}
236251

237-
function printHeader(testName: string, testIndex: number) {
238-
const text = `${testIndex + 1} of ${testsToRun.length}`;
239-
const fullIndex =
240-
(testIndex < allSetups.length
241-
? testIndex
242-
: (testIndex - allSetups.length) * nbShards + shardId + allSetups.length) + 1;
243-
const length = tests.length + allSetups.length;
252+
function printSetupHeader(setupName: string, setupIndex: number, count: number) {
253+
const text = `${setupIndex + 1} of ${count}`;
254+
console.log(
255+
colors.green(`Running setup "${colors.bold.blue(setupName)}" (${colors.bold.white(text)})...`),
256+
);
257+
}
258+
259+
function printTestHeader(testName: string, testIndex: number, count: number) {
260+
const text = `${testIndex + 1} of ${count}`;
261+
const fullIndex = testIndex * nbShards + shardId + 1;
244262
const shard =
245263
shardId === null
246264
? ''
247-
: colors.yellow(` [${shardId}:${nbShards}]` + colors.bold(` (${fullIndex}/${length})`));
265+
: colors.yellow(` [${shardId}:${nbShards}]` + colors.bold(` (${fullIndex}/${tests.length})`));
248266
console.log(
249-
colors.green(`Running "${colors.bold.blue(testName)}" (${colors.bold.white(text)}${shard})...`),
267+
colors.green(
268+
`Running test "${colors.bold.blue(testName)}" (${colors.bold.white(text)}${shard})...`,
269+
),
250270
);
251271
}
252272

0 commit comments

Comments
 (0)