feat: basic test coverage

This commit is contained in:
DarkSky 2023-02-05 18:41:18 +08:00
parent dd9d30de37
commit c4d34ddfea
28 changed files with 323 additions and 1081 deletions

View File

@ -1,7 +0,0 @@
**/webpack.config.js
**/scripts/*.js
**/node_modules/**
.github/**
**/__tests__/**
**/tests/**

2
.gitignore vendored
View File

@ -4,6 +4,8 @@
*dist
/tmp
/out-tsc
.nyc_output
.coverage
# dependencies
node_modules

View File

@ -8,12 +8,13 @@
"scripts": {
"dev": "pnpm --filter=!@affine/app build && pnpm --filter @affine/app dev",
"dev:ac": "pnpm --filter=!@affine/app build && NODE_API_SERVER=ac pnpm --filter @affine/app dev",
"dev:local": "pnpm --filter=!@affine/app build && NODE_API_SERVER=local pnpm --filter @affine/app dev",
"dev:local": "pnpm --filter=!@affine/app build && cross-env NODE_API_SERVER=local pnpm --filter @affine/app dev",
"build": " pnpm --filter=!@affine/app build && pnpm --filter!=@affine/datacenter -r build",
"export": "pnpm --filter @affine/app export",
"start": "pnpm --filter @affine/app start",
"lint": "pnpm --filter @affine/app lint",
"test": "playwright test",
"test:coverage": "cross-env COVERAGE=true pnpm test && nyc report -t .nyc_output --report-dir .coverage --reporter=lcov",
"test:dc": "pnpm --filter @affine/datacenter test",
"test:e2e:codegen": "npx playwright codegen http://localhost:8080",
"test:unit": "playwright test --config=playwright.config.unit.ts",
@ -28,12 +29,13 @@
},
"devDependencies": {
"@changesets/cli": "^2.26.0",
"@jest/globals": "^29.3.1",
"@playwright/test": "^1.29.1",
"@types/eslint": "^8.4.10",
"@types/node": "^18.11.17",
"@typescript-eslint/eslint-plugin": "^5.47.0",
"@typescript-eslint/parser": "^5.47.0",
"babel-plugin-istanbul": "^6.1.1",
"cross-env": "^7.0.3",
"eslint": "^8.30.0",
"eslint-config-next": "12.3.1",
"eslint-config-prettier": "^8.5.0",
@ -41,8 +43,8 @@
"fake-indexeddb": "4.0.1",
"got": "^12.5.3",
"husky": "^8.0.2",
"jest": "^29.3.1",
"lint-staged": "^13.1.0",
"nyc": "^15.1.0",
"prettier": "^2.7.1",
"typescript": "^4.9.3"
},
@ -67,9 +69,16 @@
},
"reportUnusedDisableDirectives": true,
"ignorePatterns": [
"src/**/*.test.ts",
"package/**/dist/*",
"package/**/sync.js"
"package/**/.babelrc.js",
"package/**/sync.js",
"src/**/*.test.ts",
"**/webpack.config.js",
"**/scripts/*.js",
"**/node_modules/**",
".github/**",
"**/__tests__/**",
"**/tests/**"
]
}
}

13
packages/app/.babelrc.js Normal file
View File

@ -0,0 +1,13 @@
const plugins = [];
if (process.env.NODE_ENV === 'development') {
console.log(
'Detected development environment. Instrumenting code for coverage.'
);
plugins.push('istanbul');
}
module.exports = {
presets: ['next/babel'],
plugins,
};

View File

@ -55,7 +55,7 @@ test.describe.serial('workspace meta collection observable', () => {
workspaceUnitCollection.once(
'change',
(event: WorkspaceUnitCollectionChangeEvent) => {
expect(event.deleted?.id).toEqual('123');
expect(event.deleted?.[0]?.id).toEqual('123');
}
);
scope.remove('123');

View File

@ -35,6 +35,9 @@ const config: PlaywrightTestConfig = {
port: 8080,
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
env: {
COVERAGE: process.env.COVERAGE || 'false',
},
},
};

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
import { clickPageMoreActions } from './libs/page-logic.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
loadPage();

52
tests/libs/playwright.ts Normal file
View File

@ -0,0 +1,52 @@
import { test as baseTest } from '@playwright/test';
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import crypto from 'node:crypto';
const istanbulTempDir = process.env.ISTANBUL_TEMP_DIR
? path.resolve(process.env.ISTANBUL_TEMP_DIR)
: path.join(process.cwd(), '.nyc_output');
function generateUUID() {
return crypto.randomUUID();
}
const enableCoverage = !!process.env.CI || !!process.env.COVERAGE;
export const test = baseTest.extend({
context: async ({ context }, use) => {
if (enableCoverage) {
await context.addInitScript(() =>
window.addEventListener('beforeunload', () =>
// @ts-expect-error
window.collectIstanbulCoverage(JSON.stringify(window.__coverage__))
)
);
await fs.promises.mkdir(istanbulTempDir, { recursive: true });
await context.exposeFunction(
'collectIstanbulCoverage',
(coverageJSON?: string) => {
if (coverageJSON)
fs.writeFileSync(
path.join(
istanbulTempDir,
`playwright_coverage_${generateUUID()}.json`
),
coverageJSON
);
}
);
}
await use(context);
if (enableCoverage) {
for (const page of context.pages()) {
await page.evaluate(() =>
// @ts-expect-error
window.collectIstanbulCoverage(JSON.stringify(window.__coverage__))
);
}
}
},
});

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
import { newPage } from './libs/page-logic.js';

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
import { newPage, clickPageMoreActions } from './libs/page-logic.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
import { newPage, clickPageMoreActions } from './libs/page-logic.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
import { newPage, clickPageMoreActions } from './libs/page-logic.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
import { newPage } from './libs/page-logic.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
import { newPage } from './libs/page-logic.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
import { newPage } from './libs/page-logic.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
import { newPage, clickPageMoreActions } from './libs/page-logic.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
import { newPage } from './libs/page-logic.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
loadPage();

View File

@ -1,4 +1,4 @@
import { test, expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect, type Page } from '@playwright/test';
import { expect, type Page } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
import { withCtrlOrMeta } from './libs/keyboard.js';
import { newPage } from './libs/page-logic.js';

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
loadPage();

View File

@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { test } from './libs/playwright.js';
import { loadPage } from './libs/load-page.js';
loadPage();