Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Question: Way to list all test files without @groups? #43

Open
BenWoodworth opened this issue May 25, 2022 · 2 comments
Open

Question: Way to list all test files without @groups? #43

BenWoodworth opened this issue May 25, 2022 · 2 comments

Comments

@BenWoodworth
Copy link

Is there a way to list all the test files that don't have a runner @group? My team wants to add a git hook that makes sure every test in our project has at least one group (in case we forget to add a group to a new test file, since we want each to have at least one).

Right now I've got a script using jest --listTests to list the test files, and regex parsing each for /*...@group XXX...*/ at the top of the file. If there's a better way I'd love to know! :)

@BenWoodworth
Copy link
Author

This is what I'm going with for now (instead of a regex) after finding how you get the groups with jest-docblock:

import * as fs from "fs";
import * as docblock from "jest-docblock";

// Based on https://github.com/eugene-manuilov/jest-runner-groups/blob/3c9d3cf4cb3e595bdea733100f2bdc8d64f871d7/index.js#L36-L51
function getJestRunnerGroups(file: string): Array<string> {
  const parsed = docblock.parse(fs.readFileSync(file, "utf8"));

  if (!parsed.group) return [];

  const groups = Array.isArray(parsed.group) ? parsed.group : [parsed.group];
  return groups.filter((group) => typeof group === 'string')
}

@SassNinja
Copy link

@BenWoodworth may I ask what groups you use?

I'm just curious because at the moment I'm defining the testing setup for a large project and I'm torn between multiple jest config files (own config for each group) and this jest-runner-groups package.

Since I only consider one group per file (xxxx.unit.test.ts for unit tests and xxxx.component.test.ts for component tests) I tend to rather use multiple configs with different filename regex. However I'm wondering if I oversee some benefits of defining multiple groups per file.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants