Skip to content

Commit

Permalink
feat: move coverage to coverage folder
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Jun 24, 2022
1 parent 53d5e80 commit 640c820
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions bin/test-storybook.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,47 @@ const cleanup = () => {
log(`Cleaning up ${storiesJsonTmpDir}`);
fs.rmSync(storiesJsonTmpDir, { recursive: true, force: true });
}
process.exit();
};

process.on('SIGINT', cleanup);
process.on('beforeExit', cleanup);
let isWatchMode = false;
async function reportCoverage() {
if (isWatchMode || process.env.STORYBOOK_COLLECT_COVERAGE !== 'true') {
return
}

const coverageFolderE2E = path.resolve(process.cwd(), '.nyc_output');
const coverageFolder = path.resolve(process.cwd(), 'coverage/storybook');

// in case something goes wrong and .nyc_output does not exist, bail
if (!fs.existsSync(coverageFolderE2E)) {
return
}

// if there's no coverage folder, create one
if (!fs.existsSync(coverageFolder)) {
fs.mkdirSync(coverageFolder, { recursive: true });
}

// move the coverage files from .nyc_output folder (coming from jest-playwright) to coverage, then delete .nyc_output
fs.renameSync(
`${coverageFolderE2E}/coverage.json`,
`${coverageFolder}/coverage-storybook-final.json`,
);
fs.rmSync(coverageFolderE2E, { recursive: true });

// --skip-full in case we only want to show not fully covered code
// --check-coverage if we want to break if coverage reaches certain threshold
// .nycrc will be respected for thresholds etc. https://www.npmjs.com/package/nyc#coverage-thresholds
execSync(`npx nyc report --reporter=text -t ${coverageFolder} --report-dir ${coverageFolder}`, { stdio: 'inherit' })
}

const onProcessEnd = () => {
cleanup();
reportCoverage();
}

process.on('SIGINT', onProcessEnd);
process.on('exit', onProcessEnd);

function sanitizeURL(url) {
let finalURL = url;
Expand Down Expand Up @@ -88,14 +124,6 @@ async function executeJestPlaywright(args) {
await jest.run(argv);
}

async function printCoverageReport() {
// --skip-full in case we only want to show not fully covered code
// --check-coverage if we want to break if coverage reaches certain threshold
// idea: pass configuration object for thresholds https://www.npmjs.com/package/nyc#coverage-thresholds
execSync('npx nyc report', { stdio: 'inherit' })
log('For a better, interactive summary of coverage, run: \nnpx nyc report --reporter=lcov\n')
}

async function checkStorybook(url) {
try {
const res = await fetch(url, { method: 'HEAD' });
Expand Down Expand Up @@ -166,6 +194,9 @@ const main = async () => {
process.exit(0);
}

// set this flag to skip reporting coverage in watch mode
isWatchMode = jestOptions.watch;

const targetURL = sanitizeURL(process.env.TARGET_URL || runnerOptions.url);
await checkStorybook(targetURL);

Expand Down Expand Up @@ -211,10 +242,6 @@ const main = async () => {
}

await executeJestPlaywright(jestOptions);

if (process.env.STORYBOOK_COLLECT_COVERAGE === 'true') {
printCoverageReport();
}
};

main().catch((e) => log(e));

0 comments on commit 640c820

Please sign in to comment.