Skip to content

Commit dc74a2e

Browse files
committed
More reliable environment setup for Jest running context
1 parent 71ff74a commit dc74a2e

File tree

4 files changed

+66
-26
lines changed

4 files changed

+66
-26
lines changed

packages/tests/src/_jest/globalSetup.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
// This product includes software developed at Datadog (https://www.datadoghq.com/).
33
// Copyright 2019-Present Datadog, Inc.
44

5-
import { logTips } from './helpers/tips';
5+
import { getEnv, logEnv, setupEnv } from './helpers/env';
66

77
const globalSetup = () => {
8+
const env = getEnv(process.argv);
9+
// Setup the environment.
10+
setupEnv(env);
811
// Log some tips to the console.
9-
logTips();
12+
logEnv(env);
13+
1014
};
1115

1216
export default globalSetup;

packages/tests/src/_jest/helpers/constants.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,3 @@ export const BUNDLER_VERSIONS: Record<BundlerFullName, string> = {
2020
webpack4: require('webpack4').version,
2121
webpack5: require('webpack5').version,
2222
};
23-
24-
// Handle --cleanup flag.
25-
export const NO_CLEANUP = process.argv.includes('--cleanup=0');
26-
27-
// Handle --build flag.
28-
export const NEED_BUILD = process.argv.includes('--build=1');
29-
30-
// Handle --bundlers flag.
31-
export const REQUESTED_BUNDLERS = process.argv.includes('--bundlers')
32-
? process.argv[process.argv.indexOf('--bundlers') + 1].split(',')
33-
: process.argv
34-
.find((arg) => arg.startsWith('--bundlers='))
35-
?.split('=')[1]
36-
.split(',') ?? [];

packages/tests/src/_jest/helpers/tips.ts renamed to packages/tests/src/_jest/helpers/env.ts

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,59 @@ import { FULL_NAME_BUNDLERS } from '@dd/core/constants';
66
import type { BundlerFullName } from '@dd/core/types';
77
import { bgYellow, dim, green, red } from '@dd/tools/helpers';
88

9-
import { NEED_BUILD, NO_CLEANUP, REQUESTED_BUNDLERS } from './constants';
9+
type TestEnv = {
10+
NO_CLEANUP: boolean;
11+
NEED_BUILD: boolean;
12+
REQUESTED_BUNDLERS: string[];
13+
};
14+
15+
export const getEnv = (argv: string[]): TestEnv => {
16+
// Handle --cleanup flag.
17+
const NO_CLEANUP = argv.includes('--cleanup=0');
18+
19+
// Handle --build flag.
20+
const NEED_BUILD = argv.includes('--build=1');
21+
22+
// Handle --bundlers flag.
23+
const REQUESTED_BUNDLERS = argv.includes('--bundlers')
24+
? argv[argv.indexOf('--bundlers') + 1].split(',')
25+
: argv
26+
.find((arg) => arg.startsWith('--bundlers='))
27+
?.split('=')[1]
28+
.split(',') ?? [];
29+
30+
return {
31+
NO_CLEANUP,
32+
NEED_BUILD,
33+
REQUESTED_BUNDLERS,
34+
};
35+
};
36+
37+
export const setupEnv = (env: TestEnv): void => {
38+
const { NO_CLEANUP, NEED_BUILD, REQUESTED_BUNDLERS } = env;
1039

11-
export const logTips = () => {
1240
if (NO_CLEANUP) {
13-
console.log(bgYellow(" Won't clean up "));
41+
process.env.NO_CLEANUP = '1';
1442
}
1543

1644
if (NEED_BUILD) {
17-
console.log(bgYellow(' Will also build used plugins '));
45+
process.env.NEED_BUILD = '1';
46+
}
47+
48+
if (REQUESTED_BUNDLERS.length) {
49+
process.env.REQUESTED_BUNDLERS = REQUESTED_BUNDLERS.join(',');
50+
}
51+
};
52+
53+
export const logEnv = (env: TestEnv) => {
54+
const { NO_CLEANUP, NEED_BUILD, REQUESTED_BUNDLERS } = env;
55+
const envLogs = [];
56+
if (NO_CLEANUP) {
57+
envLogs.push(bgYellow(" Won't clean up "));
58+
}
59+
60+
if (NEED_BUILD) {
61+
envLogs.push(bgYellow(' Will also build used plugins '));
1862
}
1963

2064
if (REQUESTED_BUNDLERS.length) {
@@ -32,7 +76,7 @@ export const logTips = () => {
3276
);
3377
}
3478
const bundlersList = REQUESTED_BUNDLERS.map((bundler) => green(bundler)).join(', ');
35-
console.log(`Running ${bgYellow(' ONLY ')} for ${bundlersList}.`);
79+
envLogs.push(`Running ${bgYellow(' ONLY ')} for ${bundlersList}.`);
3680
}
3781

3882
if (!NO_CLEANUP || !NEED_BUILD || REQUESTED_BUNDLERS.length) {
@@ -46,6 +90,10 @@ export const logTips = () => {
4690
if (!REQUESTED_BUNDLERS.length) {
4791
tips.push(` ${green('--bundlers=webpack4,esbuild')} to only use specified bundlers.`);
4892
}
49-
console.log(dim(`\nYou can also use : \n${tips.join('\n')}\n`));
93+
envLogs.push(dim(`\nYou can also use : \n${tips.join('\n')}\n`));
94+
}
95+
96+
if (envLogs.length) {
97+
console.log(`\n${envLogs.join('\n')}\n`);
5098
}
5199
};

packages/tests/src/_jest/helpers/runBundlers.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import {
2020
getWebpack4Options,
2121
getWebpack5Options,
2222
} from './configBundlers';
23-
import { NEED_BUILD, NO_CLEANUP, PLUGIN_VERSIONS, REQUESTED_BUNDLERS } from './constants';
24-
import { defaultDestination } from './mocks';
25-
import type { Bundler, BundlerRunFunction, CleanupFn } from './types';
23+
24+
// Get the environment variables.
25+
const { NO_CLEANUP, NEED_BUILD, REQUESTED_BUNDLERS } = process.env;
2626

2727
const xpackCallback = (
2828
err: Error | null,
@@ -272,8 +272,9 @@ const allBundlers: Bundler[] = [
272272
},
273273
];
274274

275+
const requestedBundlers = REQUESTED_BUNDLERS ? REQUESTED_BUNDLERS.split(',') : [];
275276
export const BUNDLERS: Bundler[] = allBundlers.filter(
276-
(bundler) => REQUESTED_BUNDLERS.length === 0 || REQUESTED_BUNDLERS.includes(bundler.name),
277+
(bundler) => requestedBundlers.length === 0 || requestedBundlers.includes(bundler.name),
277278
);
278279

279280
// Build only if needed.
@@ -284,6 +285,7 @@ if (NEED_BUILD) {
284285

285286
for (const bundler of bundlersToBuild) {
286287
console.log(`Building ${green(bundler)}...`);
288+
// Can't do parallel builds because no await at root.
287289
executeSync('yarn', ['workspace', bundler, 'run', 'build']);
288290
}
289291
}

0 commit comments

Comments
 (0)