|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { readdirSync, readFileSync } from 'node:fs'; |
| 3 | +import { join } from 'node:path'; |
| 4 | + |
| 5 | +import { CliTest, DEFAULT_RC, USER_FLOW_MOCKS } from '../../utils/setup'; |
| 6 | + |
| 7 | +const DUMMY_USER_FLOW_NAME = 'DUMMY FLOW'; |
| 8 | + |
| 9 | +describe('collect format', () => { |
| 10 | + ['html', 'json', 'md'].forEach((format) => { |
| 11 | + it<CliTest>(`should save the results as a ${format} file`, async ({root, setupFns, cli}) => { |
| 12 | + |
| 13 | + setupFns.setupRcJson(getRcWithFormat([format])); |
| 14 | + setupFns.setupUserFlows(USER_FLOW_MOCKS.MINIMAL); |
| 15 | + |
| 16 | + const { code, stderr } = await cli.run('user-flow', ['collect']); |
| 17 | + |
| 18 | + expect(stderr).toBe(''); |
| 19 | + expect(code).toBe(0); |
| 20 | + |
| 21 | + const resultFileNames = getResultFile(root); |
| 22 | + expect(resultFileNames).toHaveLength(1); |
| 23 | + expect(resultFileNames[0].endsWith(`.${format}`)).toBeTruthy(); |
| 24 | + |
| 25 | + const content = getResultContent(root, resultFileNames[0]); |
| 26 | + expect(isValidFormatedResult(format, content)); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it<CliTest>('should print results to stdout', async ({ setupFns, cli }) => { |
| 31 | + setupFns.setupRcJson(getRcWithFormat(['stdout'])); |
| 32 | + setupFns.setupUserFlows(USER_FLOW_MOCKS.MINIMAL); |
| 33 | + |
| 34 | + const { code, stdout, stderr } = await cli.run('user-flow', ['collect']); |
| 35 | + |
| 36 | + expect(stdout).toContain(`| Gather Mode | Performance | Accessibility | Best Practices | Seo | Pwa |`) |
| 37 | + expect(stdout).toContain(DUMMY_USER_FLOW_NAME); |
| 38 | + expect(stderr).toBe(''); |
| 39 | + expect(code).toBe(0); |
| 40 | + }); |
| 41 | + |
| 42 | + it<CliTest>('should save the results as a HTML, JSON and Markdown files and print to stdout', async ({ root, setupFns, cli }) => { |
| 43 | + setupFns.setupRcJson(getRcWithFormat(['html', 'json', 'md', 'stdout'])); |
| 44 | + setupFns.setupUserFlows(USER_FLOW_MOCKS.MINIMAL); |
| 45 | + |
| 46 | + const { code, stdout, stderr } = await cli.run('user-flow', ['collect']); |
| 47 | + |
| 48 | + const resultFileNames = getResultFile(root); |
| 49 | + expect(resultFileNames).toHaveLength(3); |
| 50 | + |
| 51 | + resultFileNames.forEach((fileName) => { |
| 52 | + const content = getResultContent(root, fileName); |
| 53 | + const format = fileName.split('.').at(-1)!; |
| 54 | + expect(isValidFormatedResult(format, content)).toBeTruthy(); |
| 55 | + }); |
| 56 | + |
| 57 | + expect(stdout).toContain(`| Gather Mode | Performance | Accessibility | Best Practices | Seo | Pwa |`) |
| 58 | + expect(stdout).toContain(DUMMY_USER_FLOW_NAME); |
| 59 | + expect(stderr).toBe(''); |
| 60 | + expect(code).toBe(0); |
| 61 | + }) |
| 62 | +}); |
| 63 | + |
| 64 | + |
| 65 | +function isValidFormatedResult(format: string, result: string) { |
| 66 | + const isValidFile = { |
| 67 | + 'html': (report: string) => report.includes(DUMMY_USER_FLOW_NAME), |
| 68 | + 'json': (report: string) => !!(JSON.parse(report)?.name || '').includes(DUMMY_USER_FLOW_NAME), |
| 69 | + 'md': (report: string) => report.includes(`| Gather Mode | Performance | Accessibility | Best Practices | Seo | Pwa |`) |
| 70 | + }; |
| 71 | + // @ts-ignore |
| 72 | + return isValidFile[format](result); |
| 73 | +} |
| 74 | + |
| 75 | +function getRcWithFormat(format: string[]) { |
| 76 | + const RC = structuredClone(DEFAULT_RC); |
| 77 | + RC.persist.format = format; |
| 78 | + return RC; |
| 79 | +} |
| 80 | + |
| 81 | +function getResultFile(dir: string): string[] { |
| 82 | + return readdirSync(join(dir, DEFAULT_RC.persist.outPath)) |
| 83 | +} |
| 84 | + |
| 85 | +function getResultContent(dir: string, name: string): string { |
| 86 | + return readFileSync(join(dir, DEFAULT_RC.persist.outPath, name), { encoding: 'utf8' }); |
| 87 | +} |
0 commit comments