Skip to content

Commit

Permalink
feat: Adds threshold parsing from vitest config
Browse files Browse the repository at this point in the history
  • Loading branch information
davelosert committed Jun 10, 2022
1 parent 8253128 commit 865faf2
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { markdownTable } from 'markdown-table';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { generateSummaryTableData } from './generateSummaryTableData.js';
import { JsonSummary } from './types/JsonSummary';
import * as core from '@actions/core';
import path from 'node:path';
import { parseJsonSummary } from './parseJsonSummary.js';
import { writeSummaryToPR } from './writeSummaryToPR.js';
import * as core from '@actions/core';
import { parseThresholds } from './parseThresholds.js';

const DEFAULT_SUMMARY_PATH = path.join('coverage', 'coverage-summary.json');
const DEFAULT_VITEST_CONFIG_PATH = path.join('vitest.config.js');

const run = async () => {
const jsonSummaryPath = path.resolve(process.cwd(), DEFAULT_SUMMARY_PATH);
const jsonSummaryRaw = await readFile(jsonSummaryPath);
const jsonSummary: JsonSummary = JSON.parse(jsonSummaryRaw.toString());
const jsonSummary = await parseJsonSummary(DEFAULT_SUMMARY_PATH);
const thresholds = await parseThresholds(DEFAULT_VITEST_CONFIG_PATH);

const tableData = generateSummaryTableData(jsonSummary);
const tableData = generateSummaryTableData(jsonSummary, thresholds);

const summary = core.summary
.addHeading('Coverage Summary')
Expand Down
13 changes: 13 additions & 0 deletions src/parseJsonSummary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { JsonSummary } from './types/JsonSummary';

const parseJsonSummary = async (jsonSummaryPath: string): Promise<JsonSummary> => {
const resolvedJsonSummaryPath = path.resolve(process.cwd(), jsonSummaryPath);
const jsonSummaryRaw = await readFile(resolvedJsonSummaryPath);
return JSON.parse(jsonSummaryRaw.toString()) as JsonSummary;
}

export {
parseJsonSummary
};
34 changes: 34 additions & 0 deletions src/parseThresholds.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { describe, it, expect } from 'vitest';
import path from 'path';
import { parseThresholds } from './parseThresholds';

// get current dir name in module environment by url
const moduleDir = path.dirname(new URL(import.meta.url).pathname);

describe('generateTableData', () => {
const mockConfigPath = path.resolve(moduleDir, '..', 'test', 'mockConfig');
const getConfig = (configName: string) => path.resolve(mockConfigPath, configName)

it('reads all the thresholds from the given configuration file.', async (): Promise<void> => {
const thresholds = await parseThresholds(getConfig('vitest.config.all.js'));

expect(thresholds).toEqual({
lines: 60,
branches: 70,
functions: 80,
statements: 90
});
});


it('sets thresholds to 100 if 100 property is true.', async (): Promise<void> => {
const thresholds = await parseThresholds(getConfig('vitest.config.100.js'));

expect(thresholds).toEqual({
lines: 100,
branches: 100,
functions: 100,
statements: 100
});
});
});
36 changes: 36 additions & 0 deletions src/parseThresholds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { Thresholds } from './types/Threshold';

import { loadConfigFromFile, resolveConfig, } from 'vite';

const parseThresholds = async (vitestConfigPath: string): Promise<Thresholds> => {
const resolvedViteConfigPath = path.resolve(process.cwd(), vitestConfigPath);
const configObj = await loadConfigFromFile({command: 'build', mode: 'production' }, resolvedViteConfigPath);

if(!configObj) {
return {};
}

const config = configObj.config;

if(config.test?.coverage?.["100"]) {
return {
lines: 100,
branches: 100,
functions: 100,
statements: 100,
}
}

return {
lines: config.test?.coverage?.lines,
branches: config.test?.coverage?.branches,
functions: config.test?.coverage?.functions,
statements: config.test?.coverage?.statements,
}
}

export {
parseThresholds
};
13 changes: 13 additions & 0 deletions test/mockConfig/vitest.config.100.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from 'vite';

export default defineConfig({
test: {
coverage: {
all: true,
reporter: ['text', 'json-summary'],
include: ['src'],
exclude: ['src/types'],
"100": true
}
}
});
16 changes: 16 additions & 0 deletions test/mockConfig/vitest.config.all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from 'vite';

export default defineConfig({
test: {
coverage: {
all: true,
reporter: ['text', 'json-summary'],
include: ['src'],
exclude: ['src/types'],
lines: 60,
branches: 70,
functions: 80,
statements: 90
}
}
});
4 changes: 4 additions & 0 deletions test/mockConfig/vitest.config.none.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { defineConfig } from 'vite';

export default defineConfig({
});

0 comments on commit 865faf2

Please sign in to comment.