Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add scripts for testing in docker container #352

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/node_modules
23 changes: 0 additions & 23 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,5 @@ jobs:
run: npm run lint
- name: Build Files
run: npm run build
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Tests
run: npm run test
- name: Upload Playwright Report
if: always()
uses: actions/upload-artifact@v4
with:
name: playwright-report
path: ./playwright-report
retention-days: 1
- name: Save PR ID
if: always()
run: |
pr="${{ github.event.pull_request.number }}"
echo $pr > ./pr-id.txt
shell: bash
- name: Create PR Artifact
if: always()
uses: actions/upload-artifact@v4
with:
name: pr
path: ./pr-id.txt
- name: Typecheck
run: npm run typecheck
2 changes: 1 addition & 1 deletion .github/workflows/pr-visual-tests-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: PR Visual Tests Report

on:
workflow_run:
workflows: ['CI']
workflows: ['PR Visual Tests']
types:
- completed

Expand Down
49 changes: 49 additions & 0 deletions .github/workflows/pr-visual-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: PR Visual Tests

on:
pull_request:
branches: [master]

jobs:
visual_tests:
name: Visual Tests
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/playwright:v1.48.2-jammy
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
cache: npm
- name: Install Packages
run: npm ci
- name: Install Demo Packages
run: cd demo && npm i
- name: Build Files
run: npm run build
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Visual Tests
env:
HOME: /root
run: npm run test
- name: Upload Playwright Report
if: always()
uses: actions/upload-artifact@v4
with:
name: playwright-report
path: ./playwright-report
retention-days: 1
- name: Save PR ID
if: always()
run: |
pr="${{ github.event.pull_request.number }}"
echo $pr > ./pr-id.txt
shell: bash
- name: Create PR Artifact
if: always()
uses: actions/upload-artifact@v4
with:
name: pr
path: ./pr-id.txt
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,36 +96,37 @@ If you want to change it, you should add `.env` file in repository's root direct
BASE_URL= # for example:'http://localhost:6006'
```

### Running tests
### Running tests on MacOS and Linux

All tests:

```bash
npm run test
npm run playwright:docker npm run test
```

Single test:

```bash
npm run test test_name.spec.ts
npm run playwright:docker npm run test test_name.spec.ts
```

Several sets of test files from different folders:

```bash
npm run test __tests__/folder1 __tests__/folder2
```

Last failed tests:

```bash
npx playwright test --last-failed
npm run playwright:docker npm run test __tests__/folder1 __tests__/folder2
```

These commands run storybook server before tests.

If storybook server is already running, playwright will use it for tests and won't run another server.

### Running tests on Windows

```bash
# add ':windows'
npm run playwright:docker:windows npm run test
```

### Test reports

To see test reports run:
Expand Down Expand Up @@ -155,7 +156,7 @@ After running tests playwright will create folder for snapshots (if it didn't ex
If reference screenshot is incorrect you can update it:

```bash
npm run test --update-snapshots
npm run playwright:docker npm run test --update-snapshots
```

## License
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@
"deps:install": "npm ci",
"deps:truncate": "npm prune --production",
"typecheck": "tsc -p . --noEmit",
"test": "exit 0",
"test": "npx playwright test --config playwright/playwright.config.ts",
"playwright:docker": "sh ./playwright/playwright-docker.sh",
"playwright:docker:windows": "sh ./playwright/playwright-docker-windows.sh",
"dev": "run-s build _dev:watch",
"_dev:watch": "run-p _build:watch _storybook:watch",
"build:clean": "rm -rf build",
"build:compile": "./esbuild/build.js",
"build:compile": "node esbuild/build.js",
"_build:declarations:esm": "tsc --emitDeclarationOnly -p tsconfig.esm.json",
"_build:declarations:cjs": "tsc --emitDeclarationOnly -p tsconfig.cjs.json",
"build": "run-s build:clean build:compile _build:declarations:*",
"_build:watch": "./esbuild/build.js --watch",
"_build:watch": "node esbuild/build.js --watch",
"_storybook:watch": "cd demo && npm run storybook",
"prepublishOnly": "npm run lint && npm run test && npm run build",
"postinstall": "cd demo && npm i || true",
Expand Down
11 changes: 11 additions & 0 deletions playwright/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM mcr.microsoft.com/playwright:v1.48.2-jammy
WORKDIR /usr/src/app
RUN cd ..
COPY package*.json ./
RUN npm ci
COPY . .
RUN cd demo && \
npm i && \
cd .. && \
npx playwright install && \
npm run build
18 changes: 18 additions & 0 deletions playwright/playwright-docker-windows.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

set -e

source ./playwright/variables.sh

HOST_COMPONENTS_DIR="/src/components"
HOST_REPORT_DIR="/playwright-report"

if [ -z "$1" ]; then
echo "Usage: $0 <command>"
exit 1
fi

COMMAND=$@

docker build -t $IMAGE_NAME -f ./playwright/Dockerfile . &&
docker run --rm -v /${PWD}$HOST_COMPONENTS_DIR:$CONTAINER_COMPONENTS_DIR -v /${PWD}$HOST_REPORT_DIR:$CONTAINER_REPORT_DIR $IMAGE_NAME $COMMAND
18 changes: 18 additions & 0 deletions playwright/playwright-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

set -e

source ./playwright/variables.sh

HOST_COMPONENTS_DIR="./src/components"
HOST_REPORT_DIR="./playwright-report"

if [ -z "$1" ]; then
echo "Usage: $0 <command>"
exit 1
fi

COMMAND=$@

docker build -t $IMAGE_NAME -f ./playwright/Dockerfile . &&
docker run --rm -v $HOST_COMPONENTS_DIR:$CONTAINER_COMPONENTS_DIR -v $HOST_REPORT_DIR:$CONTAINER_REPORT_DIR $IMAGE_NAME $COMMAND
14 changes: 11 additions & 3 deletions playwright.config.ts → playwright/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import {resolve} from 'path';
import {defineConfig, devices} from '@playwright/test';

import {BASE_URL} from './src/components/constants';
import {BASE_URL} from '../src/components/constants';

function pathFromRoot(p: string) {
// replace for Windows users
return resolve(__dirname, '../', p).replace(/\\/g, '/');
}

export default defineConfig({
testDir: pathFromRoot('src/components'),
updateSnapshots: 'missing',
snapshotPathTemplate: '{testFilePath}/../../__screenshots__/{arg}-{projectName}{ext}',
snapshotPathTemplate:
'{testDir}/{testFilePath}/../../__screenshots__/{arg}-{projectName}-{platform}{ext}',
fullyParallel: false,
forbidOnly: true,
retries: 2,
workers: 1,
reporter: 'html',
webServer: {
reuseExistingServer: true,
command: 'npm run _storybook:watch',
command: 'BROWSER=true npm run dev',
url: BASE_URL,
timeout: 120 * 1000,
},
Expand Down
5 changes: 5 additions & 0 deletions playwright/variables.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

IMAGE_NAME="playwright-docker"
CONTAINER_COMPONENTS_DIR="/usr/src/app/src/components"
CONTAINER_REPORT_DIR="/usr/src/app/playwright-report"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"demo/**/*.tsx",
"demo/.storybook/**/*.ts",
"demo/.storybook/**/*.tsx",
"playwright.config.ts"
"playwright/playwright.config.ts"
]
}
Loading