Skip to content

Commit cc04021

Browse files
committed
perf: improves speed of local test after 1st run
1 parent 98c2587 commit cc04021

4 files changed

Lines changed: 39 additions & 9 deletions

File tree

e2e/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ The returned value is an object with those properties and methods:
4040
- `stdout`, _string_: the data written to stdout during the run
4141
- `stderr`, _string_: the data written to stderr during the run
4242
- `output`, _string_: the data written to stdout and stderr during the run
43+
- `outputForSnapshot`, _string_: same as `output`, expect it's sanitized for jest snapshot (time values are replaced with static values, ...)
4344

4445
**Note**: _You can optionally pass the expected status code as the first argument of `run()`. In the case it's not the correct one, it'll write in the console the actual `output` so that you can debug the test case._
4546

e2e/__helpers__/test-case.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ class TestCaseRunDescriptor {
4949
...this._options,
5050
template: this.templateName,
5151
});
52-
if (logOutputUnlessStatusIs != null) {
52+
if (
53+
logOutputUnlessStatusIs != null &&
54+
logOutputUnlessStatusIs !== result.status
55+
) {
5356
console.log(
5457
`Output of test run in "${this.name}" using template "${
5558
this.templateName
56-
}":\n\n`,
59+
} (exit code: ${result.status})":\n\n`,
5760
result.output.trim(),
5861
);
5962
}
@@ -72,6 +75,7 @@ export interface TestRunResult {
7275
stdout: string;
7376
stderr: string;
7477
output: string;
78+
outputForSnapshot: string;
7579
}
7680

7781
export default function configureTestCase(
@@ -111,8 +115,24 @@ export function run(
111115
const output = result.output
112116
? stripAnsiColors(result.output.join('\n\n'))
113117
: '';
118+
const outputForSnapshot = output
119+
.trim()
120+
// removes total and estimated time(s)
121+
.replace(
122+
/^(\s*Time\s*:\s*)[\d.]+m?s(?:(,\s*estimated\s+)[\d.]+m?s)?(\s*)$/gm,
123+
(_, start, estimatedPrefix, end) => {
124+
return `${start}XXs${
125+
estimatedPrefix ? `${estimatedPrefix}YYs` : ''
126+
}${end}`;
127+
},
128+
)
129+
// removes each test time(s)
130+
.replace(
131+
/^(\s*(?:|)\s+.+\s+\()[\d.]+m?s(\)\s*)$/gm,
132+
(_, start, end) => `${start}XXms${end}`,
133+
);
114134

115-
return { status: result.status, stderr, stdout, output };
135+
return { status: result.status, stderr, stdout, output, outputForSnapshot };
116136
}
117137

118138
// from https://stackoverflow.com/questions/25245716/remove-all-ansi-colors-styles-from-strings
@@ -136,10 +156,12 @@ function prepareTest(name: string, template: string): string {
136156
fs.copySync(sourceDir, caseDir);
137157

138158
// link the node_modules dir
139-
fs.symlinkSync(
140-
join(templateDir, 'node_modules'),
141-
join(caseDir, 'node_modules'),
142-
);
159+
if (!fs.existsSync(join(caseDir, 'node_modules'))) {
160+
fs.symlinkSync(
161+
join(templateDir, 'node_modules'),
162+
join(caseDir, 'node_modules'),
163+
);
164+
}
143165

144166
// copy all other files from the template to the case dir
145167
fs.readdirSync(templateDir).forEach(item => {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"test": "npm run test:e2e && npm run test:unit",
1717
"tslint": "tslint 'src/**/*.ts'",
1818
"doc": "doctoc .",
19-
"prepublish": "npm run clean-build",
19+
"prepare": "npm run build",
20+
"prepublishOnly": "npm run clean-build",
2021
"precommit": "lint-staged",
2122
"postcommit": "git reset",
2223
"format": "prettier --single-quote --trailing-comma all --write \"{src,scripts,tests}/**/*.ts\" && prettier --single-quote --trailing-comma es5 --write \"{src,scripts,tests}/**/*.js\""

scripts/e2e.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,18 @@ function setupE2e() {
4141
);
4242

4343
// link locally so we could find it easily
44-
fs.symlinkSync(Paths.e2eWorkDir, Paths.e2eWotkDirLink);
44+
if (!fs.existsSync(Paths.e2eWotkDirLink)) {
45+
fs.symlinkSync(Paths.e2eWorkDir, Paths.e2eWotkDirLink, 'dir');
46+
}
4547

4648
// install with `npm ci` in each template, this is the fastest but needs a package lock file,
4749
// that is why we end with the npm install of our bundle
4850
getDirectories(Paths.e2eWorkTemplatesDir).forEach(tmplDir => {
4951
const dir = path.join(Paths.e2eWorkTemplatesDir, tmplDir);
52+
// TODO: create a hash of package-lock.json as well as the bundle, and test it over one copied in each
53+
// template dir, to know if we should re-install or not
54+
if (fs.existsSync(path.join(dir, 'node_modules'))) return;
55+
5056
if (NodeVersion.major >= 8) {
5157
spawnSync('npm', ['ci'], { cwd: dir, stdio: 'inherit' });
5258
} else {

0 commit comments

Comments
 (0)