Skip to content

Commit

Permalink
e2e-test: converted into a CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Rugvip committed Oct 10, 2020
1 parent dc0261e commit 7de1004
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-falcons-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'e2e-test': minor
---

Converted into a CLI, use `yarn e2e-test run` to run
2 changes: 1 addition & 1 deletion .github/workflows/e2e-win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ jobs:
- name: yarn build
run: yarn build --ignore example-app --ignore example-backend --ignore @techdocs/cli
- name: run E2E test
run: yarn workspace e2e-test start
run: yarn e2e-test run
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
- name: run E2E test
run: |
sudo sysctl fs.inotify.max_user_watches=524288
yarn workspace e2e-test start
yarn e2e-test run
env:
POSTGRES_HOST: localhost
POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ yarn tsc
yarn build
```

Once those tasks have completed, you can now run the test using `yarn start` inside this package.
Once those tasks have completed, you can now run the test using `yarn e2e-test run`.

If you make changes to other packages you will need to rerun `yarn tsc && yarn build`. Changes to this package do not require a rebuild.

Expand Down
7 changes: 5 additions & 2 deletions packages/e2e-test/src/index.js → packages/e2e-test/bin/e2e-test
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
/*
* Copyright 2020 Spotify AB
*
Expand All @@ -14,13 +15,15 @@
* limitations under the License.
*/

const path = require('path');

require('ts-node').register({
transpileOnly: true,
/* eslint-disable-next-line no-restricted-syntax */
project: require('path').resolve(__dirname, '../../../tsconfig.json'),
project: path.resolve(__dirname, '../../../tsconfig.json'),
compilerOptions: {
module: 'CommonJS',
},
});

require('./e2e-test');
require('../src');
7 changes: 6 additions & 1 deletion packages/e2e-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@
"backstage"
],
"license": "Apache-2.0",
"main": "src/index.js",
"main": "src/index.ts",
"scripts": {
"start": "node .",
"lint": "backstage-cli lint",
"test": "backstage-cli test",
"test:e2e": "yarn start"
},
"bin": {
"e2e-test": "bin/e2e-test"
},
"devDependencies": {
"@backstage/cli-common": "^0.1.1-alpha.24",
"@types/fs-extra": "^9.0.1",
"@types/node": "^13.7.2",
"chalk": "^4.0.0",
"commander": "^6.1.0",
"fs-extra": "^9.0.0",
"handlebars": "^4.7.3",
"node-fetch": "^2.6.0",
Expand Down
22 changes: 22 additions & 0 deletions packages/e2e-test/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { CommanderStatic } from 'commander';
import { run } from './run';

export function registerCommands(program: CommanderStatic) {
program.command('run').description('Run e2e tests').action(run);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ import Browser from 'zombie';
import {
spawnPiped,
runPlain,
handleError,
waitForPageWithText,
waitFor,
waitForExit,
print,
} from './helpers';
} from '../lib/helpers';
import pgtools from 'pgtools';
import { findPaths } from '@backstage/cli-common';

/* eslint-disable-next-line no-restricted-syntax */
// eslint-disable-next-line no-restricted-syntax
const paths = findPaths(__dirname);

const templatePackagePaths = [
Expand All @@ -42,7 +41,7 @@ const templatePackagePaths = [
'packages/create-app/templates/default-app/packages/backend/package.json.hbs',
];

async function main() {
export async function run() {
const rootDir = await fs.mkdtemp(resolvePath(os.tmpdir(), 'backstage-e2e-'));
print(`CLI E2E test root: ${rootDir}\n`);

Expand Down Expand Up @@ -413,16 +412,3 @@ async function testBackendStart(appDir: string, isPostgres: boolean) {
print('Backend startup test finished successfully');
}
}

process.on('unhandledRejection', (error: Error) => {
// Try to avoid exiting if the unhandled error is coming from jsdom, i.e. zombie.
// Those are typically errors on the page that should be benign, at least in the
// context of this test. We have other ways of asserting that the page is being
// rendered correctly.
if (error?.stack?.includes('node_modules/jsdom/lib')) {
console.log(`Ignored error inside jsdom, ${error}`);
} else {
handleError(error);
}
});
main().catch(handleError);
58 changes: 58 additions & 0 deletions packages/e2e-test/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import program from 'commander';
import chalk from 'chalk';
import { registerCommands } from './commands';
import { version } from '../package.json';
import { exitWithError } from './lib/helpers';

async function main(argv: string[]) {
program.name('e2e-test').version(version);

registerCommands(program);

program.on('command:*', () => {
console.log();
console.log(chalk.red(`Invalid command: ${program.args.join(' ')}`));
console.log();
program.outputHelp();
process.exit(1);
});

program.parse(argv);
}

process.on('unhandledRejection', (rejection: unknown) => {
// Try to avoid exiting if the unhandled error is coming from jsdom, i.e. zombie.
// Those are typically errors on the page that should be benign, at least in the
// context of this test. We have other ways of asserting that the page is being
// rendered correctly.
if (
rejection instanceof Error &&
rejection?.stack?.includes('node_modules/jsdom/lib')
) {
console.log(`Ignored error inside jsdom, ${rejection?.stack ?? rejection}`);
} else {
if (rejection instanceof Error) {
exitWithError(rejection);
} else {
exitWithError(new Error(`Unknown rejection: '${rejection}'`));
}
}
});

main(process.argv).catch(exitWithError);
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function spawnPiped(cmd: string[], options?: SpawnOptions) {
shell: true,
...options,
});
child.on('error', handleError);
child.on('error', exitWithError);

const logPrefix = cmd.map(s => s.replace(/.+\//, '')).join(' ');
child.stdout?.on(
Expand Down Expand Up @@ -75,7 +75,7 @@ export async function runPlain(cmd: string[], options?: SpawnOptions) {
}
}

export function handleError(err: Error & { code?: unknown }) {
export function exitWithError(err: Error & { code?: unknown }) {
process.stdout.write(`${err.name}: ${err.stack || err.message}\n`);

if (typeof err.code === 'number') {
Expand Down

0 comments on commit 7de1004

Please sign in to comment.