Skip to content

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

Closed as not planned
@ice-blaze

Description

@ice-blaze

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions