Skip to content

Commit

Permalink
perf: improves speed of local test after 1st run
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Aug 5, 2018
1 parent 98c2587 commit cc04021
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ The returned value is an object with those properties and methods:
- `stdout`, _string_: the data written to stdout during the run
- `stderr`, _string_: the data written to stderr during the run
- `output`, _string_: the data written to stdout and stderr during the run
- `outputForSnapshot`, _string_: same as `output`, expect it's sanitized for jest snapshot (time values are replaced with static values, ...)

**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._

Expand Down
36 changes: 29 additions & 7 deletions e2e/__helpers__/test-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ class TestCaseRunDescriptor {
...this._options,
template: this.templateName,
});
if (logOutputUnlessStatusIs != null) {
if (
logOutputUnlessStatusIs != null &&
logOutputUnlessStatusIs !== result.status
) {
console.log(
`Output of test run in "${this.name}" using template "${
this.templateName
}":\n\n`,
} (exit code: ${result.status})":\n\n`,
result.output.trim(),
);
}
Expand All @@ -72,6 +75,7 @@ export interface TestRunResult {
stdout: string;
stderr: string;
output: string;
outputForSnapshot: string;
}

export default function configureTestCase(
Expand Down Expand Up @@ -111,8 +115,24 @@ export function run(
const output = result.output
? stripAnsiColors(result.output.join('\n\n'))
: '';
const outputForSnapshot = output
.trim()
// removes total and estimated time(s)
.replace(
/^(\s*Time\s*:\s*)[\d.]+m?s(?:(,\s*estimated\s+)[\d.]+m?s)?(\s*)$/gm,
(_, start, estimatedPrefix, end) => {
return `${start}XXs${
estimatedPrefix ? `${estimatedPrefix}YYs` : ''
}${end}`;
},
)
// removes each test time(s)
.replace(
/^(\s*(?:✕|✓)\s+.+\s+\()[\d.]+m?s(\)\s*)$/gm,
(_, start, end) => `${start}XXms${end}`,
);

return { status: result.status, stderr, stdout, output };
return { status: result.status, stderr, stdout, output, outputForSnapshot };
}

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

// link the node_modules dir
fs.symlinkSync(
join(templateDir, 'node_modules'),
join(caseDir, 'node_modules'),
);
if (!fs.existsSync(join(caseDir, 'node_modules'))) {
fs.symlinkSync(
join(templateDir, 'node_modules'),
join(caseDir, 'node_modules'),
);
}

// copy all other files from the template to the case dir
fs.readdirSync(templateDir).forEach(item => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"test": "npm run test:e2e && npm run test:unit",
"tslint": "tslint 'src/**/*.ts'",
"doc": "doctoc .",
"prepublish": "npm run clean-build",
"prepare": "npm run build",
"prepublishOnly": "npm run clean-build",
"precommit": "lint-staged",
"postcommit": "git reset",
"format": "prettier --single-quote --trailing-comma all --write \"{src,scripts,tests}/**/*.ts\" && prettier --single-quote --trailing-comma es5 --write \"{src,scripts,tests}/**/*.js\""
Expand Down
8 changes: 7 additions & 1 deletion scripts/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ function setupE2e() {
);

// link locally so we could find it easily
fs.symlinkSync(Paths.e2eWorkDir, Paths.e2eWotkDirLink);
if (!fs.existsSync(Paths.e2eWotkDirLink)) {
fs.symlinkSync(Paths.e2eWorkDir, Paths.e2eWotkDirLink, 'dir');
}

// install with `npm ci` in each template, this is the fastest but needs a package lock file,
// that is why we end with the npm install of our bundle
getDirectories(Paths.e2eWorkTemplatesDir).forEach(tmplDir => {
const dir = path.join(Paths.e2eWorkTemplatesDir, tmplDir);
// TODO: create a hash of package-lock.json as well as the bundle, and test it over one copied in each
// template dir, to know if we should re-install or not
if (fs.existsSync(path.join(dir, 'node_modules'))) return;

if (NodeVersion.major >= 8) {
spawnSync('npm', ['ci'], { cwd: dir, stdio: 'inherit' });
} else {
Expand Down

0 comments on commit cc04021

Please sign in to comment.