Skip to content

Instantly share code, notes, and snippets.

@designfrontier
Created October 22, 2021 22:25
Show Gist options
  • Save designfrontier/24532cd19340792b57866f3c7ad76667 to your computer and use it in GitHub Desktop.
Save designfrontier/24532cd19340792b57866f3c7ad76667 to your computer and use it in GitHub Desktop.
A simple way to enforce that changes to js/ts files require changes to their corresponding test files in legacy code bases where you are trying to expand code coverage over time.
#!/usr/bin/env node
const IGNORE_FILES = [];
const filesToParse = JSON.parse(process.env.files);
const filesThatNeedTests = filesToParse.filter((f) => {
const fileNameArray = f.split('.');
if (IGNORE_FILES.includes(f)) {
return false;
}
return (
['js', 'ts'].includes(fileNameArray.pop()) && fileNameArray.pop() !== 'test'
);
});
const filesThatAreTests = filesToParse.filter((f) => {
const fileNameArray = f.split('.');
return (
['js', 'ts'].includes(fileNameArray.pop()) && fileNameArray.pop() === 'test'
);
});
const untestedFiles = filesThatNeedTests.filter((f) => {
const fileNameArray = f.split('.');
const ext = fileNameArray.pop();
fileNameArray.push('test');
fileNameArray.push(ext);
return !filesThatAreTests.includes(`${fileNameArray.join('.')}`);
});
if (untestedFiles.length === 0) {
process.exit(0);
}
console.log(`
************************************************
It appears you changed a js file but didn't add
or change a test. Please update the tests for
the file in question.
The following files need updated tests:
${untestedFiles.map((f) => `- ${f}\n`).join('')}
************************************************`);
process.exit(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment