Skip to content

Commit

Permalink
test: decouple test from file system
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherPHolder authored Feb 27, 2024
2 parents 8150ae1 + 7f60238 commit 8fc229d
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 328 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ jobs:
- name: install
run: npm ci
- name: build
run: npm run nx -- affected:build --base=origin/main --head=HEAD
run: npm run nx -- run-many -t build --skip-nx-cache --parallel=false
- name: test
run: npm run nx -- affected:test --parallel=1 --base=origin/main --head=HEAD
run: npm run nx -- run-many -t test --skip-nx-cache --parallel=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`md-table should print MD table if getStepsTable is called with a reduced result 1`] = `
"| Step Name | Gather Mode | Performance | Accessibility | Best Practices | Seo | Pwa |
| :----------------------------- | :---------: | :---------: | :-----------: | :------------: | :-: | :-: |
| Navigation report (127.0.0.1/) | navigation | - | 100 | 92 | 100 | 30 |
| Timespan report (127.0.0.1/) | timespan | 10/11 | - | 5/7 | - | - |
| Snapshot report (127.0.0.1/) | snapshot | Ø 3/4 | 10/10 | 4/5 | 9/9 | - |"
`;

exports[`md-table should return a Md table comparing to reports if getStepsTable is passed a baseline report 1`] = `
"| Step Name | Gather Mode | Performance | Accessibility | Best Practices | Seo | Pwa |
| :----------------------------- | :---------: | :---------: | :-----------: | :------------: | :------: | :------: |
| Navigation report (127.0.0.1/) | navigation | - | 100 | 92 (-7) | 100 | 30 (+32) |
| Timespan report (127.0.0.1/) | timespan | 10/11 | - | 5/7 | - | - |
| Snapshot report (127.0.0.1/) | snapshot | Ø 3/4 | 10/10 (-1) | 4/5 | 9/9 (-1) | - |"
`;
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { getBudgetTable, getStepsTable } from './md-report';
import FlowResult from 'lighthouse/types/lhr/flow';
import { getReportContent } from 'test-data';
import { writeFileSync } from 'fs';
import { ReducedReport } from '../../collect/utils/report/types';
import { createReducedReport, enrichReducedReportWithBaseline } from '../../collect/utils/report/utils';

const lhr8 = getReportContent<any>('lhr-8.json');
const lhr9 = getReportContent<FlowResult>('lhr-9.json');
const lhr9budgets = getReportContent<FlowResult>('lhr-9-budgets.json');
const LHRREDUCEDMD = getReportContent('lhr-9_reduced.md');
const lhr9Ex2 = getReportContent<FlowResult>('lhr-9-ex-2.json');
const lhr9reduced = getReportContent<ReducedReport>('lhr-9_reduced.json');
const LHRREDUCEDCompareMD = getReportContent('lhr-9_compare.md');
const lhr9ReducedBaseline = getReportContent<ReducedReport>('lhr-9_reduced-baseline.json');

describe('md-table', () => {
Expand Down Expand Up @@ -39,13 +36,13 @@ describe('md-table', () => {
it('should print MD table if getStepsTable is called with a reduced result', () => {
const reducedLhr9 = createReducedReport(lhr9);
const mdTable = getStepsTable(reducedLhr9);
expect(mdTable).toEqual(LHRREDUCEDMD);
expect(mdTable).toMatchSnapshot();
});

it('should return a Md table comparing to reports if getStepsTable is passed a baseline report', () => {
const reducedLhr9 = createReducedReport(lhr9);
const mdTable = getStepsTable(reducedLhr9, lhr9Ex2);
expect(mdTable).toEqual(LHRREDUCEDCompareMD);
expect(mdTable).toMatchSnapshot();
});

it('should return a Md table if getBudgetTable is passed a baseline report', () => {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { join } from 'node:path';
import { existsSync, lstatSync, readdirSync } from 'node:fs';
import { UserFlowProvider } from './types';
import { join } from 'path';
import { existsSync, lstatSync, readdirSync } from 'fs';
import { resolveAnyFile } from '../../../../core/file';
import { CollectRcOptions } from '../../options/types';

export function loadFlow(collect: Pick<CollectRcOptions, 'ufPath'>): ({ exports: UserFlowProvider, path: string })[] {
const { ufPath } = collect;
const path = join(process.cwd(), ufPath);
if (!existsSync(path)) {
throw new Error(`ufPath: ${path} is no directory`);
throw new Error(`ufPath: ${path} is neither a file nor a directory`);
}

let files: string[];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { loadFlow } from './load-flow';
import * as fileHelpers from '../../../../core/file';
import * as fs from 'node:fs';

jest.mock('node:fs');
jest.mock('../../../../core/file');

describe('loading user-flow scripts for execution', () => {

beforeEach(() => {
jest.clearAllMocks();
})

it('should throw if ufPath does not exist', () => {
const existsSyncSpy = jest.spyOn(fs, 'existsSync').mockReturnValue(false);
expect(() => loadFlow({ ufPath: './path' })).toThrow();
expect(existsSyncSpy).toHaveBeenCalled();
});

it('should throw if ufPath points to a file and it does not end in with ts or js', () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
jest.spyOn(fs, 'lstatSync').mockReturnValue({ isDirectory: () => false } as fs.Stats);
const resolveAnyFileSpy = jest.spyOn(fileHelpers, 'resolveAnyFile');
expect(() => loadFlow({ ufPath: './path/file.json' })).toThrow();
expect(resolveAnyFileSpy).not.toHaveBeenCalled();
});

it('should throw if ufPath points to a directory and it does not contain any files that end with ts or js', () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
jest.spyOn(fs, 'lstatSync').mockReturnValue({ isDirectory: () => true } as fs.Stats);
jest.spyOn(fs, 'readdirSync').mockReturnValue(['file.json' as unknown as fs.Dirent])
const resolveAnyFileSpy = jest.spyOn(fileHelpers, 'resolveAnyFile');
expect(() => loadFlow({ ufPath: './path' })).toThrow();
expect(resolveAnyFileSpy).not.toHaveBeenCalled();
});

it('should extract all file paths in the directory if ufPath points to a directory', () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
jest.spyOn(fileHelpers, 'resolveAnyFile').mockReturnValue('Flow Dummy' as any);
const isDirectorySpy = jest.spyOn(fs, 'lstatSync').mockReturnValue({ isDirectory: () => true } as fs.Stats);
const readdirSyncSpy = jest.spyOn(fs, 'readdirSync').mockReturnValue(['file.ts' as unknown as fs.Dirent])
loadFlow({ ufPath: './user-flow-dir-path' });
expect(isDirectorySpy).toHaveBeenCalled();
expect(readdirSyncSpy).toHaveBeenCalledWith(
expect.stringContaining('user-flow-dir-path')
);
});

it('should only resolve files ending in ts or js', () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
jest.spyOn(fs, 'lstatSync').mockReturnValue({ isDirectory: () => true } as fs.Stats);
jest.spyOn(fs, 'readdirSync')
.mockReturnValue(['file.ts', 'file.js', 'file.tsx', 'file.json', 'file.js.json'] as unknown as fs.Dirent[]);
const resolveAnyFileSpy = jest.spyOn(fileHelpers, 'resolveAnyFile');
loadFlow({ ufPath: './path' });
expect(resolveAnyFileSpy).toHaveBeenCalledTimes(2);
expect(resolveAnyFileSpy).toHaveBeenNthCalledWith(
1,
expect.stringContaining('file.ts')
);
expect(resolveAnyFileSpy).toHaveBeenNthCalledWith(
2,
expect.stringContaining('file.js')
);
})
});

This file was deleted.

Loading

0 comments on commit 8fc229d

Please sign in to comment.