Skip to content

Commit

Permalink
feat: e2e (#3501)
Browse files Browse the repository at this point in the history
* feat: add e2e test

* feat: 升级到 G2 正式版

* feat: add tests base url

* fix: web server command

* chore: update snapshots for release version

* feat: ChatGPT cr
  • Loading branch information
lxfu1 authored Mar 27, 2023
1 parent 5671dde commit 9653ae1
Show file tree
Hide file tree
Showing 74 changed files with 250 additions and 5,906 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/cr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Code Review

permissions:
contents: read
pull-requests: write

on:
pull_request:
types: [opened, reopened, synchronize]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: anc95/ChatGPT-CodeReview@main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
# Optional
LANGUAGE: Chinese
MODEL:
top_p: 1
temperature: 1
27 changes: 27 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ coverage
# Website cache byb dumi
site/.dumi/tmp
site/.dumi/tmp-production
/test-results/
/playwright-report/
/playwright/.cache/
/test-results/
/playwright-report/
/playwright/.cache/
/test-results/
/playwright-report/
/playwright/.cache/
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

npm run lint-staged
npm run lint
# npm run test
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
auto-install-peers=true
strict-peer-dependencies=false
scripts-prepend-node-path=true
public-hoist-pattern[]=@antv/*
public-hoist-pattern[]=d3-*
87 changes: 0 additions & 87 deletions __tests__/integration/canvas.ts

This file was deleted.

1 change: 1 addition & 0 deletions __tests__/integration/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const renderers = {
const app = document.getElementById('app') as HTMLElement;
// Create a plot container.
const container = document.createElement('div');
container.className = 'container';
// Create a case select.
const caseSelect = document.createElement('select') as HTMLSelectElement;
caseSelect.style.margin = '12px';
Expand Down
56 changes: 0 additions & 56 deletions __tests__/integration/snapshot.spec.ts

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed __tests__/integration/snapshots/lineArea-actual.png
Binary file not shown.
Binary file removed __tests__/integration/snapshots/lineArea-diff.png
Binary file not shown.
Binary file removed __tests__/integration/snapshots/lineArea.png
Binary file not shown.
Binary file removed __tests__/integration/snapshots/lineBasic-actual.png
Binary file not shown.
Binary file removed __tests__/integration/snapshots/lineBasic-diff.png
Binary file not shown.
Binary file removed __tests__/integration/snapshots/lineBasic.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed __tests__/integration/snapshots/lineColorMapping.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed __tests__/integration/snapshots/linePointShape.png
Binary file not shown.
Binary file not shown.
Binary file removed __tests__/integration/snapshots/lineSeries-diff.png
Binary file not shown.
Binary file removed __tests__/integration/snapshots/lineSeries.png
Binary file not shown.
Binary file not shown.
Binary file removed __tests__/integration/snapshots/lineSlider-diff.png
Diff not rendered.
Binary file removed __tests__/integration/snapshots/lineSlider.png
Diff not rendered.
Diff not rendered.
1 change: 0 additions & 1 deletion __tests__/unit/utils/helper.spec.ts

This file was deleted.

11 changes: 0 additions & 11 deletions __tests__/utils/canvas.ts

This file was deleted.

7 changes: 0 additions & 7 deletions __tests__/utils/delay.ts

This file was deleted.

26 changes: 0 additions & 26 deletions __tests__/utils/dom.ts

This file was deleted.

2 changes: 2 additions & 0 deletions e2e/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# e2e snapshots dir
SNAPSHOTS_DIR=snapshots
20 changes: 20 additions & 0 deletions e2e/plots/line.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import path from 'path';
import { test, expect } from '@playwright/test';
import { createDiff, getSnapshotName, toHump, sleep } from '../utils';

const IMG_DIR = path.resolve(__dirname, '..', process.env.SNAPSHOTS_DIR as string);

const files = getSnapshotName();

test.afterAll(() => {
createDiff();
});

test('Plot snapshots', async ({ page }) => {
for (const filename of files) {
const [name] = filename.split('.');
await page.goto(`/?name=${toHump(name)}`);
await sleep(1000);
await page.locator('.container').screenshot({ path: `${IMG_DIR}/${name}.png` });
}
});
Binary file added e2e/snapshots/column-basic-stable.png
Binary file added e2e/snapshots/column-basic.png
Binary file added e2e/snapshots/line-annotation-line-stable.png
Binary file added e2e/snapshots/line-annotation-line.png
Binary file added e2e/snapshots/line-annotation-range-stable.png
Binary file added e2e/snapshots/line-annotation-range.png
Binary file added e2e/snapshots/line-area-stable.png
Binary file added e2e/snapshots/line-area.png
Binary file added e2e/snapshots/line-basic-stable.png
Binary file added e2e/snapshots/line-basic.png
Binary file added e2e/snapshots/line-color-mapping-stable.png
Binary file added e2e/snapshots/line-color-mapping.png
Binary file added e2e/snapshots/line-condition-rangeY-stable.png
Binary file added e2e/snapshots/line-condition-rangeY.png
Binary file added e2e/snapshots/line-point-shape-stable.png
Binary file added e2e/snapshots/line-point-shape.png
Binary file added e2e/snapshots/line-scrollbar-stable.png
Binary file added e2e/snapshots/line-scrollbar.png
Binary file added e2e/snapshots/line-series-stable.png
Binary file added e2e/snapshots/line-series.png
Binary file added e2e/snapshots/line-slider-stable.png
Binary file added e2e/snapshots/line-slider.png
4 changes: 4 additions & 0 deletions e2e/utils/Index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { createDiff } from './create-diff';
export { getSnapshotName } from './get-snapshot-name';
export { toHump } from './to-hump';
export { sleep } from './sleep';
58 changes: 58 additions & 0 deletions e2e/utils/create-diff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as fs from 'fs';
import path from 'path';
import { PNG } from 'pngjs';
import pixelmatch from 'pixelmatch';

const STABLE = 'stable';

const DIFF = 'diff';

const maxError = 0;

const IMG_DIR = path.resolve(__dirname, `../${process.env.SNAPSHOTS_DIR}`);

export const createDiff = async () => {
const files = fs.readdirSync(IMG_DIR);
files.forEach((filename) => {
if (filename.endsWith(`${STABLE}.png`) || filename.endsWith(`${DIFF}.png`)) return;
diffFile(IMG_DIR, filename);
});
};

/**
* diff between PNGs
*/
const diffFile = (filePath: string, filename: string) => {
const [name, type] = filename.split('.');
// 当前截图生成的图片
const testSnapshotFile = `${filePath}/${filename}`;
// 用于下次比对的图片
const stableFile = `${filePath}/${name}-${STABLE}.${type}`;
// diff 文件
const diffFile = `${filePath}/${name}-${DIFF}.${type}`;
// 不存在时直接写入
if (!fs.existsSync(stableFile)) {
fs.copyFileSync(testSnapshotFile, stableFile);
return;
}

const snap = PNG.sync.read(fs.readFileSync(testSnapshotFile));
const stable = PNG.sync.read(fs.readFileSync(stableFile));
const { width, height } = snap;

const diff = new PNG({ width, height });

// @see https://github.com/mapbox/pixelmatch#pixelmatchimg1-img2-output-width-height-options
const mismatch = pixelmatch(snap.data, stable.data, diff.data, width, height, {
threshold: 0.1,
});

if (mismatch > maxError) {
fs.writeFileSync(diffFile, PNG.sync.write(diff));
} else {
try {
fs.unlinkSync(diffFile);
// eslint-disable-next-line no-empty
} catch (err) {}
}
};
9 changes: 9 additions & 0 deletions e2e/utils/get-snapshot-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as fs from 'fs';
import path from 'path';

const DEMO_DIR = path.resolve(__dirname, `../../__tests__/integration/plots`);

export const getSnapshotName = () => {
const files = fs.readdirSync(DEMO_DIR);
return files.filter((filename) => filename !== 'index.ts');
};
5 changes: 5 additions & 0 deletions e2e/utils/sleep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const sleep = (n: number) => {
return new Promise((resolve) => {
setTimeout(resolve, n);
});
};
5 changes: 5 additions & 0 deletions e2e/utils/to-hump.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const toHump = (name = '') => {
return name.replace(/-(\w)/g, (all, letter) => {
return letter.toUpperCase();
});
};
Loading

0 comments on commit 9653ae1

Please sign in to comment.