Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs]: Overriding the coverage threshold for some files #15400

Closed
ice-blaze opened this issue Dec 5, 2024 · 3 comments
Closed

[Docs]: Overriding the coverage threshold for some files #15400

ice-blaze opened this issue Dec 5, 2024 · 3 comments

Comments

@ice-blaze
Copy link

Page(s)

https://jestjs.io/docs/configuration#coveragethreshold-object

Description

After seeing the issue about overriding the coverage threshold hasn't that much of interest #7529 I was wondering if we should at least show that there is a workaround for that. The issue is close therefore I can't give my example there but I have the feeling it might still interest some people within the doc. If you think it's a good thing I can do the PR to update the documentation.

Here is my proposed solution of the config jest.config.ts

import * as fs from 'fs';
import * as path from 'path';

function discoverFiles(dir, filelist = []): string[] {
  fs.readdirSync(dir).forEach((file) => {
    const dirFile = path.join(dir, file);
    try {
      filelist = discoverFiles(dirFile, filelist);
    } catch (err) {
      if (err.code === `ENOTDIR` || err.code === `EBUSY`)
        filelist = [...filelist, dirFile];
      else throw err;
    }
  });
  return filelist;
}

const allFiles = discoverFiles(`./src/app/`)
  .filter((file) => file.endsWith(`.ts`))
  .filter((file) => false === file.endsWith(`.spec.ts`));

const thresholds = {
  branches: 80,
};

const defaultThresholdForAllFiles = Object.assign(
  {},
  ...allFiles.map((x) => ({ [x]: { ...thresholds } }))
);

module.exports = {
  // ...
  coverageThreshold: {
    ...defaultThresholdForAllFiles,
    "some/file/path/that/exists/in/variable/above.ts": { branches: 0 },
  },
};

It feels to big to end up in the documentation in my opinion. Maybe just a text that mention

The **/*.ts glob pattern does not allow direct overriding of files found during the search. However, a workaround exists: if a file is listed multiple times, the last occurrence will take precedence, effectively overriding earlier ones. Programmatically you could generate an object containing all .ts files of your project and then manually overriding them. You can find an example to this github issue #7529

Copy link

github-actions bot commented Jan 4, 2025

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Jan 4, 2025
Copy link

github-actions bot commented Feb 3, 2025

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 3, 2025
Copy link

github-actions bot commented Feb 3, 2025

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

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

No branches or pull requests

1 participant