Search code examples
jestjstsx

Jest test to see if *.spec.tsx files exist


I have a TypeScript project that contains a bunch of files in different directories all located under src/.

Is it possible to write a Jest test so that it only returns success if each *.tsx file found under the parent directory (src/) has a corresponding *.spec.tsx test file?

In other words if I have the following 3 files:

src/index.tsx
src/foo.tsx
src/folder/component.tsx

The Jest test would fail until such time that the following files are created:

src/index.spec.tsx
src/foo.spec.tsx
src/folder/component.spec.tsx

Solution

  • I ended up using

    import fs from 'fs';
    

    and then using fs.readdir and fs.statSync to determine if a file is a file or a directory so that I can recursively look in every location from my src/ path.