Skip to content

Commit

Permalink
test: smoke test on build react-ui package and open page with controls
Browse files Browse the repository at this point in the history
  • Loading branch information
v_sorokin committed Feb 18, 2020
1 parent b0853da commit a1139b6
Show file tree
Hide file tree
Showing 17 changed files with 1,753 additions and 23 deletions.
7 changes: 7 additions & 0 deletions packages/tests/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
presets: [[
'@babel/preset-env', { loose: true, modules: 'commonjs' }],
'@babel/typescript',
],
plugins: [['@babel/plugin-transform-runtime', { version: '7.8.3' }]],
};
25 changes: 25 additions & 0 deletions packages/tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "tests",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "cross-env BABEL_ENV=development jest"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/preset-typescript": "^7.8.3",
"@types/puppeteer": "^2.0.0",
"@types/wait-on": "^4.0.0",
"cross-env": "^7.0.0",
"jest": "^25.1.0",
"jest-teamcity-reporter": "^0.9.0",
"puppeteer": "^2.1.1",
"wait-on": "^4.0.0"
},
"dependencies": {},
"jest": {
"testResultsProcessor": "jest-teamcity-reporter"
}
}
146 changes: 146 additions & 0 deletions packages/tests/smoke.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import path from 'path';
import fs from 'fs';
import os from 'os'
import { ChildProcess, spawn, execSync } from 'child_process';

import puppeteer from 'puppeteer';
import waitOn from 'wait-on';

describe('Smoke', () => {
let buildServerProcess: ChildProcess | undefined;
const globalConsoleError = console.error;
const runOnTeamcity = 'TEAMCITY_VERSION' in process.env;

const appName = 'test-app-temp';
const resourcesDirectory = path.join(__dirname, 'smokeResources');
const appDirectory = path.join(resourcesDirectory, appName);

const reactUIPackagePath = runOnTeamcity
? getPackagePathOnTeamcity()
: path.join(resourcesDirectory, "react-ui.tgz");

beforeEach(() => {
console.error = jest.fn(globalConsoleError);

clearApplicationFolder(appDirectory);
if (!runOnTeamcity) {
buildReactUI(reactUIPackagePath);
}
}, 120000);

afterEach(() => {
console.error = globalConsoleError;
if (buildServerProcess && !buildServerProcess.killed) {
buildServerProcess.kill();
}
});

it('Build and render all controls', async () => {
initApplication(resourcesDirectory, appDirectory, reactUIPackagePath);
buildServerProcess = runDevServer(appDirectory);
await openPageOnBrowser(resourcesDirectory);

expect(console.error).not.toBeCalled();
}, 120000);
});

function buildReactUI(reactUIPackagePath: string) {
const reactUIPath = path.join(__dirname, "../react-ui");
const buildPath = path.join(reactUIPath, "build");

execSync(
`yarn prerelease`,
{ cwd: reactUIPath, stdio: 'inherit' }
);

execSync(
`yarn pack --filename ${reactUIPackagePath}`,
{ cwd: buildPath, stdio: 'inherit' }
);
}

function clearApplicationFolder(appDirectory: string) {
if (fs.existsSync(appDirectory)) {
execSync(`git clean ${appDirectory} -fdxq`, { stdio: 'inherit' });
}
}

function initApplication(resourcesDirectory: string, appDirectory: string, reactUIPackagePath: string) {
const craTemplateDirectory = path.join(resourcesDirectory, 'cra-template-react-ui');

execSync(
`npx create-react-app@next ${appDirectory} --template file:${craTemplateDirectory}`,
{ stdio: 'inherit' }
);

execSync(
`yarn install`,
{ cwd: appDirectory, stdio: 'inherit' }
);

// yarn save package in cache
execSync(
`npm install ${reactUIPackagePath}`,
{ cwd: appDirectory, stdio: 'inherit' }
);
}

function runDevServer(appFolder: string): ChildProcess {
return spawn(
"node",
["node_modules/react-scripts/bin/react-scripts.js", "start"],
{ env: {
...process.env,
BROWSER: "none",
SKIP_PREFLIGHT_CHECK: "true"
},
cwd: appFolder,
stdio: 'inherit',
});
}

async function openPageOnBrowser(resourceDirectory: string) {
const browser = await puppeteer.launch();
const page = await browser.newPage();

await page.setViewport({
width: 1920,
height: 1024,
});

page.on('console', msg => {
if (msg.type() === "error") {
console.error(`BROWSER: ${msg.text()}`);
}
});

page.on('error', msg => {
console.error(`BROWSER: name: ${msg.name}\nmessage: ${msg.message}\nstack: ${msg.stack}`);
});

page.on("pageerror", msg => {
console.error(`BROWSER: name: ${msg.name}\nmessage: ${msg.message}\nstack: ${msg.stack}`);
});

const applicationHost = `http://${os.hostname()}:3000/`;
await waitOn({ resources: [applicationHost], timeout: 30000 });
await page.goto(applicationHost);
await page.screenshot({ path: path.join(resourceDirectory, 'reactUIcontrols.png') });

if (await page.$(`#react-ui-page`) === null) {
console.error(`BROWSER: Failed to load controls page.`);
}

await browser.close();
}

function getPackagePathOnTeamcity(): string {
const repositoryPath = path.join(__dirname, '..', '..');
const files = fs.readdirSync(repositoryPath);
const packageName = files.find(file => file.startsWith('skbkontur-react-ui-') && file.endsWith(".tgz"));

if (packageName) {
return path.join(repositoryPath, packageName);
}
throw new Error(`react-ui tgz package not found into "${repositoryPath}" directory`);
}
3 changes: 3 additions & 0 deletions packages/tests/smokeResources/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test-app-temp
reactUIcontrols.png
react-ui.tgz
20 changes: 20 additions & 0 deletions packages/tests/smokeResources/cra-template-react-ui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# cra-template-typescript

This is the official TypeScript template for [Create React App](https://github.com/facebook/create-react-app).

To use this template, add `--template typescript` when creating a new app.

For example:

```sh
npx create-react-app my-app --template typescript

# or

yarn create react-app my-app --template typescript
```

For more information, please refer to:

- [Getting Started](https://create-react-app.dev/docs/getting-started) – How to create a new app.
- [User Guide](https://create-react-app.dev) – How to develop apps bootstrapped with Create React App.
20 changes: 20 additions & 0 deletions packages/tests/smokeResources/cra-template-react-ui/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "cra-template-react-ui",
"version": "0.0.1",
"keywords": [
"react",
"create-react-app",
"template",
"typescript"
],
"description": "The base TypeScript template for Create React App.",
"main": "template.json",
"license": "MIT",
"engines": {
"node": ">=10.0"
},
"files": [
"template",
"template.json"
]
}
16 changes: 16 additions & 0 deletions packages/tests/smokeResources/cra-template-react-ui/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"package": {
"dependencies": {
"@testing-library/react": "^9.3.2",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/user-event": "^7.1.2",
"@types/node": "^12.0.0",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/jest": "^24.0.0",
"typescript": "~3.7.4",
"@skbkontur/react-icons": "^3.2.0",
"stylis-plugin-extra-scope": "^0.2.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br />
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.<br />
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.<br />
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br />
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="description" content="Smoke tests for React UI" />
<title>React UI</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="react-ui-page"></div>
<div id="root"></div>
</body>
</html>
Loading

0 comments on commit a1139b6

Please sign in to comment.