5

Below is my interface and test file. When I run jest tests against it for code coverage, it keeps saying that lines 1 and 2 are not covered by the tests. Is code coverage for interfaces something that is even possible? Or should I exclude any interfaces from the coverage reports?

index.tsx

export interface StoreState {
    languageName: string;
    enthusiasmLevel: number;
}


index.test.tsx

import { StoreState } from '../types';

it('has a languageName of "TypeScript"', () => {
    const state: StoreState = { languageName: 'TypeScript', enthusiasmLevel: 3 };
    expect(state.languageName).toEqual('TypeScript');
});

it('has an enthusiasm level of 3', () => {
  const state: StoreState = { languageName: 'TypeScript', enthusiasmLevel: 3 };
  expect(state.enthusiasmLevel).toEqual(3);
});

1 Answer 1

6

This is a known issue with ts-jest (Issue #378). The project owners currently recommend excluding interface files from Jest coverage:

kulshekhar commented on Jan 1, 2018:

I don't think there's anything that can be done in ts-jest to fix this. I've taken a closer look at this issue and there are two ways to get the desired outcome:

  • exclude files that contain only types from coverage
  • add and export a dummy function/variable from a file that contains only types

When TypeScript transpiles files, it doesn't convert 'pure type' imports to require statements. This results in Jest not picking and passing those files to ts-jest.

GeeWee commented on Jan 2, 2018:

I don't think we can exclude files that contain only types. I also think this is a wontfix unless jest adds the capabillity for transformers to opt files out of coverage or something akin to that.

For example, you could use coveragePathIgnorePatterns to exclude files based on a naming convention for files that contain only types (e.g., an I prefix followed by a capital letter, such as "ISerializable"):

{
  "jest": {
    ...
    "coveragePathIgnorePatterns": [
      "**/I[A-Z]*.{ts}"
    ]
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

For me, I needed to use the following regexp: "src/*/I[A-z]*.ts". Double '*' resulted in an Error.
Thanks @Sophia! This worked for me "<rootDir>/.+/I[A-z]+.ts"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.