Unable to produce a Jest code coverage report

I am currently developing a Next.js project in Typescript and using the SWC compiler. For testing, I have integrated @swc/jest. Although all my tests are passing successfully, the coverage report appears empty. Below is an excerpt from my jest.config.js:

module.exports = {
  'roots': ['<rootDir>/../src'],
  'moduleDirectories': ['node_modules', 'src'],
  'setupFilesAfterEnv': ['<rootDir>/setup-tests.js'],
  'coverageDirectory': '<rootDir>/../coverage',
  'verbose': true,
  'collectCoverage': true,
  'transform': {
    '^.+\\.(t|j)sx?$': [
      '@swc/jest',
      {
        'jsc': {
          target: 'es2021',
        },
        'sourceMaps': true,
      },
    ],
  },
  'collectCoverageFrom': [
    '<rootDir>/../src/**.{ts,js,tsx,jsx}',
    '!**/node_modules/**',
  ],
}

Here is how my file structure looks:

.
├── coverage
├── jest
│   ├── jest.config.js
│   ├── setup-tests.js
├── src/
├── tsconfig.json

The generated output displays as follows:

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------

Test Suites: 11 passed, 11 total
Tests:       25 passed, 25 total
Snapshots:   0 total
Time:        1.307 s
Ran all test suites.

I have raised an issue on @swc/jest's repository but considering other possibilities, I would like to seek advice here as well.

Answer №1

The configuration setting for 'collectCoverageFrom' in your jest setup appears to be incorrect. To properly include all directories and sub-directories, use a double asterisk **. You can also use a single * as a wildcard for directory or file names.

If you want to capture all files in all directories, regardless of sub-directories:

src/**/*

To only include js and ts files in all directories:

src/**/*.{js,ts}

To include all files within the src directory only:

src/*

It seems that your current configuration is matching directories that end with specific extensions such as js, ts, etc.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

ReactJs Error: Unable to access property value because it is undefined (trying to read '0')

I am currently attempting to retrieve and display the key-value pairs in payload from my JSON data. If the key exists in the array countTargetOptions, I want to show it in a component. However, I am encountering an error message stating Uncaught TypeError ...

Having trouble with assigning an error message in Formik validation using TypeScript

Currently, I am in the process of constructing a user input form in React & TypeScript using Formik. The form requires the user to input a name, email, and password. The input handling is functioning properly, but now I aim to implement validation functio ...

Encountering TS2344 error when referring to the Component Ref in Vue.js during typing operations

I received a component reference on my FormCheckbox component from a patternlib. When I tried to incorporate the component into my own TestComp component, I encountered this TypeScript error that left me puzzled: TS2344: Type '{ name: string; mixins: ...

What is the best way to filter by enum value in Typescript?

If I define an enum as follows: export enum Status { InProgress = 0, Completed = 1, Cancelled = 2 } and have a class that references the enum: import { Status } from "./Status"; export class TaskDto { public name: string = null; public c ...

The property 'options' is not found in the type of union type

In my development project, I have defined a couple of interface types and a union type: export interface FieldOptions { value: string | number; viewValue: string; } export interface BaseField { id: string; derived?: boolean; required?: boolean; ...

What steps should I follow to bring in an animated SVG file into Next.js 13 without losing transparency and animation effects?

How to Include an Animated SVG File in Next.js 13 import Image from "next/image"; export default function Home() { return ( <main className="flex h-screen flex-col items-center"> <div className="container mt-1 ...

proceed to another page after choosing specific dates for submission

I have created the following code to build a booking calendar. I would like it so that when I click on submit, it will redirect to a different page by navigating. The function should check if the start date is greater than the current date and the end date ...

Using Angular Ionic for a click event that is triggered by a specific class

I am utilizing Highcharts and would like to click on the legend upon loading. With the use of Angular Ionic, how can I trigger a click on the .highcharts-legend-item class within the ngOnInit() {} method? I am aiming to click on this class as soon as the ...

Exploring the process of iterating through fetched arrays and presenting the outcomes using Next.js

I'm encountering an issue while attempting to iterate through an array and display the results individually on my website. The error message that keeps appearing is: TypeError: this.props.fetched.map is not a function I'm unsure of what is caus ...

What is the best approach for testing the TypeScript code below?

Testing the following code has been requested, although I am not familiar with it. import AWS from 'aws-sdk'; import db from './db'; async function uploadUserInfo(userID: number) { const user = db.findByPk(userID); if(!user) throw ...

What is the process for retrieving the chosen country code using material-ui-phone-number?

When incorporating user input for phone numbers, I have opted to utilize a package titled material-ui-phone-number. However, the challenge arises when attempting to retrieve the country code to verify if the user has included a 0 after the code. This infor ...

Unable to retrieve data within a customer component

Currently, I am engrossed in a personal project where I am crafting an odds comparison platform using Next.js for the frontend and Django REST Framework in conjunction with Django Channels for the backend. Despite immersing myself in the Next.js documentat ...

Could someone please help me understand the reason behind the map function error I'm encountering?

Currently, I am delving into the world of API fetching. After following various responses on this platform, I encountered a perplexing error with the script below: const fetcher = async () => { const url = 'https://banana1.free.beeceptor.com/c ...

Expanding the capability of a function by inheriting properties of either type any or unknown

Can you explain why the values of P1 and P2 are different in these type definitions? type P1 = (() => 22) extends {[k:string]:any} ? 1:2 //`P1 == 1` type P2 = (() => 22) extends {[k:string]:unknown} ? 1:2 //`P2 == 2` ...

The clerk and next.js authentication middleware code falls short in securing my /dashboard route effectively

My dashboard route isn't authenticating properly before entering. I have both my .env and middleware files located outside the app folder. import { authMiddleware } from "@clerk/nextjs"; export default authMiddleware({ // Routes that c ...

Encountering a problem while attempting to host an Angular application on localhost:4200

After executing the ng serve command, I encountered an issue in the browser: An error occurred while trying to resolve "localhost:4200" ("") for "10.238.0.0": rpc error: code = Unknown desc = no such record I apologize if this question seems basic, as I ...

The useParams() method results in a null value

When attempting to utilize the useParams() hook in nextjs, I am encountering an issue where it returns null despite following the documentation. Here is my current directory structure: pages ├── [gameCode] │ └── index.tsx Within index.tsx ...

Next.js routes triggering the retrieval of default data for React Context API

Modify_app.tsx: 'use client'; import type { AppProps } from 'next/app' import NavBar from '@/Components/NavBar/NavBar'; import { NextUIProvider } from '@nextui-org/react' import BlogState from '@/Context/blogSta ...

Relaying numerous references in TypeScript

Having some trouble forwarding an object of refs in TypeScript and struggling with how to properly type them. Here are the refs and the way I'm passing them into my component. const storyRef = useRef<HTMLElement>(null); const parcoursRef = useR ...

retrieve asynchronous data from the server using ngrx

How can I retrieve asynchronous data from the server? I am looking to save this data in a global store for future updates. I'm having trouble grasping the concept of asynchronous calls, such as in Redux. While I was able to understand it with simpl ...