Skip to content

Instantly share code, notes, and snippets.

@cdoremus
Forked from coryhouse/AllComponents.test.ts
Created May 26, 2022 03:49
Show Gist options
  • Save cdoremus/4eedce7729889524453093653fb632d3 to your computer and use it in GitHub Desktop.
Save cdoremus/4eedce7729889524453093653fb632d3 to your computer and use it in GitHub Desktop.
Enforce tests exist for each component
// This file assures that a Jest test file and Cypress test file exists for each component.
import path from "path";
import fs from "fs";
function getFiles(filepath: string) {
return fs.readdirSync(filepath).filter(function (file) {
return fs.statSync(path.join(filepath, file)).isFile();
});
}
function getComponentNames() {
const componentsPath = path.join(__dirname, "./");
const componentFiles = getFiles(componentsPath)
.filter((f) => f.endsWith(".tsx")) // The component files should all have a .tsx extension
.filter((f) => !f.endsWith(".test.tsx")) // Ignore tests
return componentFiles.map((filename) => {
const filenameWithoutExtension = filename.replace(".tsx", "");
return filenameWithoutExtension;
});
}
const components = getComponentNames().map((f) => [f, true]);
test.each(components)(
"Jest unit tests should exist for %s",
(input, output) => {
// By convention, each Jest test is stored in this path,
// and should have the same name as the component, with a ".test.tsx" extension.
const expectedTestPath = path.join(__dirname, `${input}.test.tsx`);
expect(fs.existsSync(expectedTestPath)).toBe(output);
}
);
test.each(components)(
"Cypress integration test should exist for %s",
(input, output) => {
// By convention, each Cypress test is stored in this path,
// and should have the same name as the component, with a ".spec.ts" extension.
const expectedTestPath = path.join(
__dirname,
"../",
"cypress",
"integration",
`${input}.spec.ts`
);
expect(fs.existsSync(expectedTestPath)).toBe(output);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment