Skip to content

Commit 8949e04

Browse files
authored
Run e2e tests on compiled binaries (#249)
* Make e2e test run on binary in everygreen * Add e2e tests support for node >= 12.16 * Add and use `waitForExit` to fix e2e tests occasionally failing testing `--version`
1 parent b545071 commit 8949e04

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

.evergreen.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ functions:
9494
set -e
9595
source .evergreen/.setup_env
9696
export EVERGREEN_EXPANSIONS_PATH="$(pwd)/../tmp/expansions.yaml"
97+
export MONGOSH_TEST_EXECUTABLE_PATH="$(pwd)/dist/mongosh"
98+
echo "$MONGOSH_TEST_EXECUTABLE_PATH"
99+
npm run compile-exec
97100
npm run test-ci
98101
test_ps:
99102
- command: expansions.write
@@ -106,6 +109,8 @@ functions:
106109
shell: powershell
107110
script: |
108111
.\.evergreen\SetupEnv
112+
$Env:MONGOSH_TEST_EXECUTABLE_PATH = $(Join-Path -Path '.' -ChildPath 'dist/mongosh.exe' -Resolve)
113+
echo "$Env:MONGOSH_TEST_EXECUTABLE_PATH"
109114
$Env:EVERGREEN_EXPANSIONS_PATH = $(Join-Path -Path '..' -ChildPath 'tmp/expansions.yaml' -Resolve)
110115
npm run test-ci
111116
release_macos:

packages/cli-repl/test/e2e.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ describe('e2e', function() {
1616
it('shows version', async() => {
1717
const shell = TestShell.start({ args: [ '--version' ] });
1818

19-
await eventually(() => {
20-
shell.assertNoErrors();
21-
shell.assertContainsOutput(
22-
require('../package.json').version
23-
);
24-
});
19+
await shell.waitForExit();
20+
21+
shell.assertNoErrors();
22+
shell.assertContainsOutput(
23+
require('../package.json').version
24+
);
2525
});
2626
});
2727

packages/cli-repl/test/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export async function eventually(fn: Function, options: { frequency?: number; timeout?: number } = {}): Promise<any> {
22
options = {
33
frequency: 100,
4-
timeout: 10000,
4+
timeout: process.env.IS_CI ? 10000 : 1000,
55
...options
66
};
77

packages/cli-repl/test/test-shell.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,31 @@ import stripAnsi from 'strip-ansi';
88
import assert from 'assert';
99

1010
const PROMPT_PATTERN = /^> /m;
11-
const ERROR_PATTERN = /Thrown:\n([^>]*)/m;
11+
const ERROR_PATTERN_1 = /Thrown:\n([^>]*)/m; // node <= 12.14
12+
const ERROR_PATTERN_2 = /Uncaught[:\n ]+([^>]*)/m;
1213

1314
/**
1415
* Test shell helper class.
1516
*/
1617
export class TestShell {
1718
private static _openShells: TestShell[] = [];
1819

19-
static start(options: { args: string[] } = { args: [] }): TestShell {
20-
const execPath = path.resolve(__dirname, '..', 'bin', 'mongosh.js');
20+
static start(options: {
21+
args: string[];
22+
} = { args: [] }): TestShell {
23+
let shellProcess: ChildProcess;
2124

22-
const process = spawn('node', [execPath, ...options.args], {
23-
stdio: [ 'pipe', 'pipe', 'pipe' ]
24-
});
25+
if (process.env.MONGOSH_TEST_EXECUTABLE_PATH) {
26+
shellProcess = spawn(process.env.MONGOSH_TEST_EXECUTABLE_PATH, [...options.args], {
27+
stdio: [ 'pipe', 'pipe', 'pipe' ]
28+
});
29+
} else {
30+
shellProcess = spawn('node', [path.resolve(__dirname, '..', 'bin', 'mongosh.js'), ...options.args], {
31+
stdio: [ 'pipe', 'pipe', 'pipe' ]
32+
});
33+
}
2534

26-
const shell = new TestShell(process);
35+
const shell = new TestShell(shellProcess);
2736
TestShell._openShells.push(shell);
2837

2938
return shell;
@@ -38,6 +47,7 @@ export class TestShell {
3847
private _process: ChildProcess;
3948

4049
private _output: string;
50+
private _onClose: Promise<number>;
4151

4252
constructor(shellProcess: ChildProcess) {
4353
this._process = shellProcess;
@@ -54,6 +64,12 @@ export class TestShell {
5464
shellProcess.stderr.on('data', (chunk) => {
5565
this._output += stripAnsi(stderrDecoder.write(chunk));
5666
});
67+
68+
this._onClose = new Promise((resolve) => {
69+
shellProcess.once('close', (code) => {
70+
resolve(code);
71+
});
72+
});
5773
}
5874

5975
get output(): string {
@@ -72,6 +88,10 @@ export class TestShell {
7288
});
7389
}
7490

91+
waitForExit(): Promise<number> {
92+
return this._onClose;
93+
}
94+
7595
kill(): void {
7696
this._process.kill();
7797
}
@@ -132,7 +152,11 @@ export class TestShell {
132152
}
133153

134154
private _getAllErrors(): string[] {
135-
return [...(this._output as any).matchAll(ERROR_PATTERN)]
155+
const output = (this._output as any);
156+
return [
157+
...output.matchAll(ERROR_PATTERN_1),
158+
...output.matchAll(ERROR_PATTERN_2)
159+
]
136160
.map(m => m[1].trim());
137161
}
138162
}

0 commit comments

Comments
 (0)