jest: get runtime config object or file

Viewed 367

I was wondering if it's possible to get the jest runtime config object or filepath.

My use case would be to use various runtime config properties on my custom matchers

// ./jest.config.js
const path = require("path");

module.exports = {
  prop1: "foo",
  prop2: "bar"
};
// my-custom-matcher.js
expect.extend({
  matcherName(received: any, pathToFile: string) {
    const relativeDir = path.join(runtimeconfigpath, pathToFile); // i need the path to the runtimeconfigpath being used here
    const baz = runtimeconfig.foo // or access the config properties like this
  }
});

I tried various stuff and dug around the docs but i dont see this anywhere. The nearest possible alternative I can think of would be to use config globals but that would complicate stuff if 'preset' and config extensions come into play.

Any help would be appreciated.

1 Answers

Have you tried to simply import jest.config.js? Or is there a specific reason not to import it? At least it is loaded by jest itself, so you should be fine by just importing it.

As js code is only compiled once and the reused of other files also need it

Otherwise you could look into 5 Advanced npm package.json configuration tips and see if it helps.

Or crate a recursive function that searches for the file. Maybe there is even some way to instantly get the path to package.json and then you can use this to continue from there (I did not find it after a short search)

Related