|
1 | | -import { mergeTests, test as base, expect } from '@playwright/test' |
2 | | -import { test as withCssCoverage, slugify } from './playwright/index.js' |
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | +import { save_css_coverage, slugify } from './playwright/index.js' |
| 3 | +import type { Coverage } from './lib/parse-coverage.js' |
3 | 4 | import * as fs from 'node:fs/promises' |
4 | 5 | import * as os from 'node:os' |
5 | 6 | import * as path from 'node:path' |
6 | 7 |
|
7 | | -const test = mergeTests(base, withCssCoverage) |
| 8 | +const MOCK_COVERAGE: Coverage[] = [ |
| 9 | + { |
| 10 | + url: 'http://example.com/style.css', |
| 11 | + text: 'body { color: red }', |
| 12 | + ranges: [{ start: 0, end: 19 }], |
| 13 | + }, |
| 14 | +] |
8 | 15 |
|
9 | | -const TEST_CONTENT = '<style>body { margin: 0 }</style><body></body>' |
10 | | - |
11 | | -function expected_filename(title_path: string[]): string { |
12 | | - return title_path.map(slugify).join('-') + '.json' |
13 | | -} |
14 | | - |
15 | | -test.describe('cssCoverageDir option', () => { |
16 | | - let custom_dir = path.join(os.tmpdir(), 'css-code-coverage-fixture-dir-test') |
17 | | - let filename = '' |
18 | | - |
19 | | - test.use({ cssCoverageDir: custom_dir }) |
| 16 | +test.describe('save_css_coverage', () => { |
| 17 | + let dir = path.join(os.tmpdir(), 'css-code-coverage-save-test') |
20 | 18 |
|
21 | 19 | test.beforeAll(async () => { |
22 | | - await fs.rm(custom_dir, { recursive: true, force: true }) |
23 | | - }) |
24 | | - |
25 | | - test('writes coverage to the configured directory', async ({ page, cssCoverage }, testInfo) => { |
26 | | - await page.setContent(TEST_CONTENT) |
27 | | - filename = expected_filename(testInfo.titlePath) |
| 20 | + await fs.rm(dir, { recursive: true, force: true }) |
28 | 21 | }) |
29 | 22 |
|
30 | 23 | test.afterAll(async () => { |
31 | | - await expect(fs.access(path.join(custom_dir, filename))).resolves.toBeUndefined() |
32 | | - await fs.rm(custom_dir, { recursive: true, force: true }) |
| 24 | + await fs.rm(dir, { recursive: true, force: true }) |
33 | 25 | }) |
34 | | -}) |
35 | | - |
36 | | -test.describe('default cssCoverageDir', () => { |
37 | | - let default_dir = path.join(process.cwd(), 'css-coverage') |
38 | | - let filename = '' |
39 | 26 |
|
40 | | - test('defaults to css-coverage relative to cwd', async ({ page, cssCoverage }, testInfo) => { |
41 | | - await page.setContent(TEST_CONTENT) |
42 | | - filename = expected_filename(testInfo.titlePath) |
| 27 | + test('writes file to the configured directory', async () => { |
| 28 | + let title_path = ['writes file to the configured directory'] |
| 29 | + await save_css_coverage(MOCK_COVERAGE, { dir, title_path }) |
| 30 | + await expect( |
| 31 | + fs.access(path.join(dir, title_path.map(slugify).join('-') + '.json')), |
| 32 | + ).resolves.toBeUndefined() |
43 | 33 | }) |
44 | 34 |
|
45 | | - test.afterAll(async () => { |
46 | | - await expect(fs.access(path.join(default_dir, filename))).resolves.toBeUndefined() |
| 35 | + test('defaults dir to css-coverage relative to cwd', async () => { |
| 36 | + let default_dir = path.join(process.cwd(), 'css-coverage') |
| 37 | + let title_path = ['defaults dir to css-coverage relative to cwd'] |
| 38 | + await save_css_coverage(MOCK_COVERAGE, { title_path }) |
| 39 | + await expect( |
| 40 | + fs.access(path.join(default_dir, title_path.map(slugify).join('-') + '.json')), |
| 41 | + ).resolves.toBeUndefined() |
47 | 42 | await fs.rm(default_dir, { recursive: true, force: true }) |
48 | 43 | }) |
49 | | -}) |
50 | | - |
51 | | -test.describe('file naming', () => { |
52 | | - let dir = path.join(os.tmpdir(), 'css-code-coverage-fixture-naming-test') |
53 | | - let filename = '' |
54 | | - |
55 | | - test.use({ cssCoverageDir: dir }) |
56 | | - |
57 | | - test.beforeAll(async () => { |
58 | | - await fs.rm(dir, { recursive: true, force: true }) |
59 | | - }) |
60 | 44 |
|
61 | | - test('derives filename from the test title path', async ({ page, cssCoverage }, testInfo) => { |
62 | | - await page.setContent(TEST_CONTENT) |
63 | | - filename = expected_filename(testInfo.titlePath) |
64 | | - }) |
65 | | - |
66 | | - test.afterAll(async () => { |
| 45 | + test('derives filename from title_path', async () => { |
| 46 | + let title_path = ['My Suite', 'my test/name.ts'] |
| 47 | + await save_css_coverage(MOCK_COVERAGE, { dir, title_path }) |
67 | 48 | let files = await fs.readdir(dir) |
68 | | - expect(files).toContain(filename) |
69 | | - await fs.rm(dir, { recursive: true, force: true }) |
70 | | - }) |
71 | | -}) |
72 | | - |
73 | | -test.describe('file content', () => { |
74 | | - let dir = path.join(os.tmpdir(), 'css-code-coverage-fixture-content-test') |
75 | | - |
76 | | - test.use({ cssCoverageDir: dir }) |
77 | | - |
78 | | - test.beforeAll(async () => { |
79 | | - await fs.rm(dir, { recursive: true, force: true }) |
80 | | - }) |
81 | | - |
82 | | - test('writes valid JSON', async ({ page, cssCoverage }) => { |
83 | | - await page.setContent(TEST_CONTENT) |
| 49 | + expect(files).toContain('my-suite-my-test-name-ts.json') |
84 | 50 | }) |
85 | 51 |
|
86 | | - test('content is an array of coverage entries', async ({ page, cssCoverage }) => { |
87 | | - await page.setContent('<style>h1 { color: red }</style><body><h1>hi</h1></body>') |
| 52 | + test('calls attach with correct arguments', async () => { |
| 53 | + let attached: { name: string; path: string; contentType: string } | undefined |
| 54 | + let title_path = ['calls attach with correct arguments'] |
| 55 | + await save_css_coverage(MOCK_COVERAGE, { |
| 56 | + dir, |
| 57 | + title_path, |
| 58 | + attach: async (name, opts) => { |
| 59 | + attached = { name, ...opts } |
| 60 | + }, |
| 61 | + }) |
| 62 | + expect(attached?.name).toBe('css-coverage') |
| 63 | + expect(attached?.contentType).toBe('application/json') |
| 64 | + expect(attached?.path).toContain('.json') |
88 | 65 | }) |
89 | 66 |
|
90 | | - test.afterAll(async () => { |
91 | | - let files = await fs.readdir(dir) |
92 | | - for (let file of files) { |
93 | | - let content = await fs.readFile(path.join(dir, file), 'utf-8') |
94 | | - let parsed = JSON.parse(content) |
95 | | - expect(Array.isArray(parsed)).toBe(true) |
96 | | - for (let entry of parsed) { |
97 | | - expect(entry).toMatchObject({ |
98 | | - url: expect.any(String), |
99 | | - text: expect.any(String), |
100 | | - ranges: expect.any(Array), |
101 | | - }) |
102 | | - } |
103 | | - } |
104 | | - await fs.rm(dir, { recursive: true, force: true }) |
| 67 | + test('file content matches the coverage input', async () => { |
| 68 | + let title_path = ['file content matches the coverage input'] |
| 69 | + await save_css_coverage(MOCK_COVERAGE, { dir, title_path }) |
| 70 | + let file_path = path.join(dir, title_path.map(slugify).join('-') + '.json') |
| 71 | + let content = JSON.parse(await fs.readFile(file_path, 'utf-8')) |
| 72 | + expect(content).toEqual(MOCK_COVERAGE) |
105 | 73 | }) |
106 | 74 | }) |
0 commit comments