Created
May 25, 2022 19:02
-
-
Save coryhouse/ca35c6a3543ee104c5a957ae5f5079f4 to your computer and use it in GitHub Desktop.
Enforce tests exist for each component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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