Skip to content

Commit df88c62

Browse files
authored
fix: do not read configuration files unless a --config parameter is passed [Closes #407] (#426)
1 parent dab86c8 commit df88c62

File tree

2 files changed

+95
-32
lines changed

2 files changed

+95
-32
lines changed

__tests__/cli.test.ts

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,73 @@ describe('CLI', () => {
4040

4141
const FIXTURES_DIR = join(__dirname, 'fixtures');
4242

43-
it('runs inline tests', async () => {
44-
const cli = new CLIMock()
45-
.stdin(
46-
`step('check h2', async () => {
47-
await page.goto(params.url, { timeout: 1500 });
48-
const sel = await page.waitForSelector('h2.synthetics', { timeout: 1500 });
49-
expect(await sel.textContent()).toBe("Synthetics test page");
50-
})`
51-
)
52-
.args(['--inline', '--params', JSON.stringify(serverParams)])
53-
.run();
54-
await cli.waitFor('Journey: inline');
55-
expect(await cli.exitCode).toBe(0);
43+
describe('for inline tests', () => {
44+
it('runs inline tests', async () => {
45+
const cli = new CLIMock()
46+
.stdin(
47+
`step('check h2', async () => {
48+
await page.goto(params.url, { timeout: 1500 });
49+
const sel = await page.waitForSelector('h2.synthetics', { timeout: 1500 });
50+
expect(await sel.textContent()).toBe("Synthetics test page");
51+
})`
52+
)
53+
.args(['--inline', '--params', JSON.stringify(serverParams)])
54+
.run();
55+
await cli.waitFor('Journey: inline');
56+
expect(await cli.exitCode).toBe(0);
57+
});
58+
59+
it('does not load a configuration file without a config param', async () => {
60+
// jest by default sets NODE_ENV to `test`
61+
const original = process.env['NODE_ENV'];
62+
const output = async () => {
63+
const cli = new CLIMock()
64+
.stdin(`step('fake step', async () => {})`)
65+
.args(['--reporter', 'json', '--inline'])
66+
.run({ cwd: FIXTURES_DIR });
67+
await cli.waitFor('journey/start');
68+
expect(await cli.exitCode).toBe(0);
69+
return cli.output();
70+
};
71+
72+
expect(JSON.parse(await output()).payload).not.toMatchObject({
73+
params: { url: 'non-dev' },
74+
});
75+
process.env['NODE_ENV'] = 'development';
76+
expect(JSON.parse(await output()).payload).not.toMatchObject({
77+
params: { url: 'dev' },
78+
});
79+
process.env['NODE_ENV'] = original;
80+
});
81+
82+
it('loads a configuration file when passing a config param', async () => {
83+
// jest by default sets NODE_ENV to `test`
84+
const original = process.env['NODE_ENV'];
85+
const output = async () => {
86+
const cli = new CLIMock()
87+
.stdin(`step('fake step', async () => {})`)
88+
.args([
89+
'--reporter',
90+
'json',
91+
'--inline',
92+
'--config',
93+
join(FIXTURES_DIR, 'synthetics.config.ts'),
94+
])
95+
.run({ cwd: FIXTURES_DIR });
96+
await cli.waitFor('journey/start');
97+
expect(await cli.exitCode).toBe(0);
98+
return cli.output();
99+
};
100+
101+
expect(JSON.parse(await output()).payload).toMatchObject({
102+
params: { url: 'non-dev' },
103+
});
104+
process.env['NODE_ENV'] = 'development';
105+
expect(JSON.parse(await output()).payload).toMatchObject({
106+
params: { url: 'dev' },
107+
});
108+
process.env['NODE_ENV'] = original;
109+
});
56110
});
57111

58112
it('run suites and exit with 0', async () => {
@@ -428,9 +482,9 @@ describe('CLI', () => {
428482
'--config',
429483
join(FIXTURES_DIR, 'synthetics.config.ts'),
430484
'--playwright-options',
431-
JSON.stringify({
485+
JSON.stringify({
432486
...devices['iPad Pro 11'],
433-
})
487+
}),
434488
])
435489
.run();
436490
await cli.waitFor('step/end');
@@ -465,13 +519,14 @@ class CLIMock {
465519
return this;
466520
}
467521

468-
run(): CLIMock {
522+
run(spawnOverrides?: { cwd?: string }): CLIMock {
469523
this.process = spawn(
470524
'node',
471525
[join(__dirname, '..', 'dist', 'cli.js'), ...this.cliArgs],
472526
{
473527
env: process.env,
474528
stdio: 'pipe',
529+
...spawnOverrides,
475530
}
476531
);
477532

src/cli.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ async function prepareSuites(inputs: string[]) {
126126
}
127127

128128
(async () => {
129+
/**
130+
* Transform `.ts` files out of the box by invoking
131+
* the `ts-node` via `transpile-only` mode that compiles
132+
* TS files without doing any extensive type checks.
133+
*
134+
* We must register `ts-node` _before_ loading inline
135+
* scripts too because otherwise we will not be able to
136+
* require `.ts` configuration files.
137+
*/
138+
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
139+
require('ts-node').register({
140+
transpileOnly: true,
141+
compilerOptions: {
142+
esModuleInterop: true,
143+
allowJs: true,
144+
target: 'es2018',
145+
},
146+
});
147+
129148
if (options.inline) {
130149
const source = await readStdin();
131150
loadInlineScript(source);
@@ -141,20 +160,6 @@ async function prepareSuites(inputs: string[]) {
141160
throw new Error(`cannot find module '${name}'`);
142161
}
143162
}
144-
/**
145-
* Transform `.ts` files out of the box by invoking
146-
* the `ts-node` via `transpile-only` mode that compiles
147-
* TS files without doing any extensive type checks
148-
*/
149-
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
150-
require('ts-node').register({
151-
transpileOnly: true,
152-
compilerOptions: {
153-
esModuleInterop: true,
154-
allowJs: true,
155-
target: 'es2018',
156-
},
157-
});
158163
/**
159164
* Handle piped files by reading the STDIN
160165
* ex: ls example/suites/*.js | npx @elastic/synthetics
@@ -174,7 +179,10 @@ async function prepareSuites(inputs: string[]) {
174179
/**
175180
* Validate and handle configs
176181
*/
177-
const config = readConfig(environment, options.config);
182+
const config =
183+
options.config || !options.inline
184+
? readConfig(environment, options.config)
185+
: {};
178186
const params = merge(config.params, options.params || {});
179187

180188
/**
@@ -187,7 +195,7 @@ async function prepareSuites(inputs: string[]) {
187195
headless: options.headless,
188196
chromiumSandbox: options.sandbox,
189197
ignoreHTTPSErrors: options.ignoreHttpsErrors,
190-
}
198+
},
191199
]);
192200

193201
const results = await run({

0 commit comments

Comments
 (0)