Skip to content

Commit

Permalink
Merge pull request #8 from nissy-dev/update-docs
Browse files Browse the repository at this point in the history
update CI and docs
  • Loading branch information
nissy-dev authored Oct 26, 2024
2 parents 2978b77 + c3e2f16 commit 6beb515
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 53 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/example-playwright.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ jobs:
- name: Copy test-results.json to working directory
run: |
if [ -e ./test-results.json ]; then
jq . test-results.json
cp test-results.json examples/playwright
fi
- name: Run test
run: pnpm exec playwright test -c playwright-with-tenbin.config.js
run: |
pnpm exec playwright test -c playwright-with-tenbin.config.js
working-directory: examples/playwright
env:
TENBIN_SHARD: ${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
REPORT_FILE_NAME: report-${{ matrix.shardIndex }}.zip
- name: Upload blob report
if: github.ref_name == 'main'
uses: actions/upload-artifact@v4
Expand All @@ -64,7 +67,8 @@ jobs:
pattern: blob-report-*
merge-multiple: true
- name: Merge json reports
run: pnpm exec playwright merge-reports --reporter json ./all-blob-reports
run: |
pnpm dlx playwright merge-reports --reporter json ./all-blob-reports > test-results.json
- name: Cache test-results.json
uses: actions/cache/save@v4
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,4 @@ dist
tenbin-report.json
test-results.json
test-results
all-reports
6 changes: 3 additions & 3 deletions examples/playwright/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

| shard | default | use tenbin |
| ----- | ------- | -----------|
| 1/3 | 23s | 19s |
| 2/3 | 19s | 19s |
| 3/3 | 15s | 20s |
| 1/3 | 12s | 17s |
| 2/3 | 17s | 17s |
| 3/3 | 26s | 19s |

see: https://github.com/nissy-dev/tenbin/actions/workflows/example-playwright.yaml
3 changes: 2 additions & 1 deletion examples/playwright/playwright-with-tenbin.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export default defineConfig({
fullyParallel: false,
workers: 1,
testMatch: splitTests({
shard: process.env.TENBIN_SHARD,
pattern: ["tests/**.test.ts"],
reportFile: "./test-results.json",
}),
reporter: [["blob"]],
reporter: [["blob", { fileName: process.env.REPORT_FILE_NAME }]],
});
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tenbin/core",
"version": "0.4.0",
"version": "0.5.0",
"repository": {
"type": "git",
"url": "https://github.com/nissy-dev/tenbin.git",
Expand Down
2 changes: 1 addition & 1 deletion packages/jest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tenbin/jest",
"version": "0.4.0",
"version": "0.5.0",
"repository": {
"type": "git",
"url": "https://github.com/nissy-dev/tenbin.git",
Expand Down
73 changes: 45 additions & 28 deletions packages/playwright/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ npm i @tenbin/playwright -D
Playwright configuration:

```js
/** @type {import('jest').Config} */
const config = {
// TODO
};
import { defineConfig } from "@playwright/test";
import { splitTests } from "@tenbin/playwright";

module.exports = config;
export default defineConfig({
testMatch: splitTests({
shard: process.env.TENBIN_SHARD,
pattern: ["tests/**.test.ts"],
reportFile: "./test-results.json",
}),
reporter: [["blob", { fileName: process.env.REPORT_FILE_NAME }]],
});
```

GitHub Actions:
Expand All @@ -29,7 +34,7 @@ on:
push:

jobs:
use-tenbin-jest:
use-tenbin-playwright:
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand All @@ -49,41 +54,53 @@ jobs:
run: pnpm install
- name: Run build
run: pnpm run build
# Restore the tenbin-report.json file, which records the execution time of each test file.
# @tenbin/jest/sequencer use this file for sharding.
- name: Restore tenbin-report.json
id: tenbin-report-cache
# Restore test-results.json file, which records the execution time of each test file.
# splitTests function use this file for sharding.
- name: Restore test-results.json
uses: actions/cache/restore@v4
with:
path: tenbin-report.json
key: tenbin-report
path: test-results.json
key: test-results
restore-keys: |
tenbin-report-*
test-results-*
- name: Run test
run: pnpx jest --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
# @tenbin/jest/reporter generates a tenbin-report.json for each shard.
- name: Upload tenbin-report.json
run: pnpm exec playwright test -c playwright-with-tenbin.config.js
env:
# splitTests function use these environment variables
TENBIN_SHARD: ${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
REPORT_FILE_NAME: report-${{ matrix.shardIndex }}.zip
# see: https://playwright.dev/docs/test-sharding#github-actions-example
- name: Upload blob report
if: github.ref_name == 'main'
uses: actions/upload-artifact@v4
with:
name: tenbin-report-${{ matrix.shardIndex }}
path: tenbin-report.json
name: blob-report-${{ matrix.shardIndex }}
path: blob-report

# Merge and cache tenbin-report.json
cache-tenbin-report:
# Merge and cache test-results.json
cache-test-results:
if: github.ref_name == 'main'
runs-on: ubuntu-latest
needs: [use-tenbin-jest]
needs: [use-tenbin-playwright]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- uses: pnpm/action-setup@v4
with:
version: "9"
- uses: actions/download-artifact@v4
with:
path: tenbin-report
pattern: tenbin-report-*
- name: Merge tenbin-report
run: jq -s add tenbin-report/**/tenbin-report.json > tenbin-report.json
- name: Cache tenbin-report.json
path: all-blob-reports
pattern: blob-report-*
merge-multiple: true
- name: Merge blob reports into json
run: pnpm dlx playwright merge-reports --reporter json ./all-blob-reports > test-results.json
- name: Cache test-results.json
uses: actions/cache/save@v4
with:
path: tenbin-report.json
key: tenbin-report-${{ github.run_id }}
path: test-results.json
key: test-results-${{ github.run_id }}
```
2 changes: 1 addition & 1 deletion packages/playwright/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tenbin/playwright",
"version": "0.4.0",
"version": "0.5.0",
"repository": {
"type": "git",
"url": "https://github.com/nissy-dev/tenbin.git",
Expand Down
23 changes: 8 additions & 15 deletions packages/playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { globSync } from "glob";
const FALLBACK_DURATION = 0.1;

type Config = {
shard: string;
reportFile: string;
pattern: string[];
};
Expand All @@ -18,11 +19,10 @@ type Test = {
};

export function splitTests(config: Config): string[] {
const { reportFile, pattern } = config;
const { shard, reportFile, pattern } = config;
const tests: Test[] = globSync(pattern).map((path) => ({ path }));
const { shardCount, shardIndex } = extractShardConfig();
const { shardCount, shardIndex } = extractShardConfig(shard);
const durations = loadDurations(reportFile);

for (const test of tests) {
test.duration = durations[test.path] ?? FALLBACK_DURATION;
}
Expand All @@ -39,20 +39,13 @@ type ShardConfig = {
shardIndex: number;
};

function extractShardConfig(): ShardConfig {
const shardEnv = process.env.TENBIN_SHARD;
if (!shardEnv) {
throw new Error(
"Missing TENBIN_SHARD environment variable. @tenbin/playwright requires this variable.",
);
}

function extractShardConfig(shard: string): ShardConfig {
try {
const [shardIndex, shardCount] = shardEnv.split("/");
const [shardIndex, shardCount] = shard.split("/");
return { shardCount: Number(shardCount), shardIndex: Number(shardIndex) };
} catch (err) {
throw new Error(
"TENBIN_SHARD variable is invalid. The value requires <shardCount>/<shardIndex> format.",
"The shard value is invalid. The value requires '<shardCount>/<shardIndex>' format.",
);
}
}
Expand All @@ -76,12 +69,12 @@ function calculateDuration(suite: JSONReportSuite): number {
for (const spec of suite.specs) {
for (const test of spec.tests) {
for (const result of test.results) {
duration += result.duration;
duration += result.duration / 1000;
}
}
}
for (const test of suite.suites ?? []) {
duration += calculateDuration(test);
}
return duration / 1000;
return duration;
}
2 changes: 1 addition & 1 deletion packages/vitest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tenbin/vitest",
"version": "0.4.0",
"version": "0.5.0",
"repository": {
"type": "git",
"url": "https://github.com/nissy-dev/tenbin.git",
Expand Down

0 comments on commit 6beb515

Please sign in to comment.