How can I retrieve the filename of the test being run in Jest hooks?

Referencing the information found at https://jestjs.io/docs/en/configuration#testenvironment-string, within my Jest configuration I have set the

"testEnvironment": "<rootDir>/scripts/testEnvironment.js"
. The testEnvironment.js file houses a custom class with methods like setup(), teardown(), and handleTestEvent().

Is there a way to retrieve the file name from one of the Jest hooks, such as run_describe_start, test_start, or any other hook?

Answer №1

In the same section of the reference documentation, it is demonstrated how the environment class accepts the full path to the test file:

class CustomEnvironment extends NodeEnvironment {
  constructor(config, context) {
    super(config, context);
    this.testPath = context.testPath;
    ...
}

The filename can be isolated like this:

this.testFile = path.basename(this.testPath);

Once it is assigned in the constructor, it can be accessed in other methods.

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

Is there a way to identify and retrieve both the initial and final elements in React?

Having an issue with logging in and need to retrieve the first and last element: Below is my array of objects: 0: pointAmountMax: 99999 pointAmountMin: 1075 rateCode: ['INAINOW'] roomPoolCode: "ZZAO" [[Prototype]]: Object 1: pointAmoun ...

Retrieving latitude and longitude from place id in an Angular Google Maps component

Currently utilizing the google-maps component to extract latitude and longitude from Google Maps prediction data. Additionally, I have integrated a search bar using google-maps component. I have successfully implemented a search bar with ngx-google-places ...

Error message is not shown by React Material UI OutlinedInput

Using React and material UI to show an outlined input. I can successfully display an error by setting the error prop to true, but I encountered a problem when trying to include a message using the helperText prop: <OutlinedInput margin="dense&quo ...

Understanding the specific types of subclasses derived from an abstract generic class in Typescript

There is a Base generic class: abstract class BaseClass<T> { abstract itemArray: Array<T>; static getName(): string { throw new Error(`BaseClass - 'getName' was not overridden!`); } internalLogic() {} } and its inherito ...

Starting Out with Typescript Types - Unidentified term 'angular'

We are currently working on revitalizing an older AngularJS project by migrating it to .Net Core 3.1. As part of this process, we are looking to transition our scripts to TypeScript. While the project is functioning properly without TypeScript, we believe ...

Delete information based on its unique identifier

Currently, I am trying to remove data from a list but encountering a small issue with the following code. Component Code removeSelectedRows(){ console.log(this.selection.selected.map(item => item.userId)) const selectedRowIds = this.selection. ...

Tips for checking the response data from Express with Jest

Currently, I am in the process of writing unit tests for individual middleware functions implemented in Node.js and Express with Jest. For instance, here is a basic example of one such middleware: function sendSomeData(req, res, next) { try { ...

Error: The type 'Promise' cannot be assigned to the type 'Model' in Angular 2 when working with Firebase

I have a method in my bug service that retrieves a single record from Firebase based on the bugId provided: // This method is located in bugservice.ts getBug(bugId: string): Promise<Bug> { return this.bugsDbRef .child(bugId) ...

What is the reason for TypeScript allowing this promise chain without any compilation errors?

Although I am still relatively new to Typescript, I find myself grappling with a particular issue that has been perplexing me. I am unsure why the following code snippet triggers a compilation error: // An example without Promises (does not compile) funct ...

JavaScript - Trouble encountered while trying to use splice to insert one array into another array

I've been working on creating a Cache Hashtable using JavaScript. When I use the code cache.splice(0,0, ...dataPage);, it inserts my data starting from the first position up to the length of dataPage. Assuming that my dataPage size is always 10. Th ...

Adjust the height of a div vertically in Angular 2+

Recently, I started using angular2 and I've been attempting to create a vertically resizable div without success. I have experimented with a directive for this purpose. Below is the code for my directive: import { Directive, HostListener, ElementRef ...

Tips on how to modify the session type in session callback within Next-auth while utilizing Typescript

With my typescript setup, my file named [...next-auth].tsx is structured as follows: import NextAuth, { Awaitable, Session, User } from "next-auth"; // import GithubProvider from "next-auth/providers/github"; import GoogleProvider from ...

Unable to access variables of nested components in Angular 2 templates

Recently delving into Angular2, I've encountered a simple issue. My aim is to create a basic parent component that acts as a container for dynamic boxes, each with its own properties and data. Here's what I've accomplished so far: The con ...

What is the best way to convert an array of data into a dataset format in React Native?

Within my specific use case, I am seeking to reform the array structure prior to loading it into a line chart. In this context, the props received are as follows: const data = [26.727, 26.952, 12.132, 25.933, 12.151, 28.492, 12.134, 26.191] The objective ...

Limit the utilization of toString through a TypeScript interface or type

Here's the interface I'm aiming for: export interface Point { readonly x: number; readonly y: number; readonly toString: never; } I initially expected it to function this way: const p: Point = {x: 4, y: 5}; // This should work fine p.toStr ...

What are some methods for retrieving RTK Query data beyond the confines of a component?

In my React Typescript app using RTK Query, I am working on implementing custom selectors. However, I need to fetch data from another endpoint to achieve this: store.dispatch(userApiSlice.endpoints.check.initiate(undefined)) const data = userApiSlice.endpo ...

Is there a way to manage specific HTML elements in Angular?

I am working on displaying a list of enable/disable buttons for different users. The goal is to show the appropriate button for each user based on their status - enabling if disabled and disabling if enabled. To achieve this, I have utilized the flags "use ...

Tips for invoking a function on a react-bootstrap-table component using TypeScript

One issue I'm encountering is trying to cast the "any" bootstrapTableRef to react-bootstrap-table, setting up the ref correctly, or importing it incorrectly. I am struggling to access the method of the imported table. While I found a solution using th ...

Mastering the Art of Concise Writing: Tips to

Is there a way to write more concisely, maybe even in a single line? this.xxx = smt.filter(item => item.Id === this.smtStatus.ONE); this.yyy = smt.filter(item => item.Id === this.smtStatus.TWO); this.zzz = smt.filter(item => item.Id == ...

Tips for locating precise information within nested object formations using Javascript

Within my code, I have showcased two distinct types of response. Upon closer examination of the following code snippets, it becomes evident that the structure of the response from a service differs slightly between the two types. In the first type, there i ...