Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add require.main and require.main.filename properties #2150

Closed
niieani opened this issue Nov 22, 2016 · 6 comments · Fixed by #5618
Closed

Add require.main and require.main.filename properties #2150

niieani opened this issue Nov 22, 2016 · 6 comments · Fixed by #5618

Comments

@niieani
Copy link
Contributor

niieani commented Nov 22, 2016

Feature request

What is the current behavior?

Currently, the require function does not contain a main property. When running normally in Node, outside of Jest, that property is set to the initial module, the one that was started.

E.g. when running node ./test.js in all descendent modules the value of require.main.filename will be the full path of the initial test.js file.

NodeJS reference

What is the expected behavior?

It would be great if require.main could be set to the module of the originating test file. This way descendent modules could find out the file path to the test that's loading them.

My use case is mocking a dynamic loader that resolves files relative to the path of the file that was initially executed. For that, I need to know the path of the test file from within the external module.

Here's an full, working example that would work if this were implemented:

relative-loader.js:

import * as path from 'path';
const relativeToDir = path.dirname(require.main.filename);

export function relativeImport(moduleId) {
  const fullPathToModule = path.join(relativeToDir, moduleId);
  return require(fullPathToModule);
}

test.js:

import { relativeImport } from `./relative-loader`;

it('should load a module relative to directory of this test', () => {
  let loadedModule = relativeImport('some-file');
  expect(loadedModule).toBe(true);
})

some-file.js:

module.exports = true;
@friend0
Copy link

friend0 commented May 24, 2017

I would really like to see this functionality.

@cpojer
Copy link
Member

cpojer commented Aug 24, 2017

Please send a PR to jest-runtime in the packages folder, it has the require implementation.

@zamotany
Copy link
Contributor

I'm interested in implementing this feature

@ajomadlabs
Copy link

Could I go for this

@thymikee
Copy link
Collaborator

@zamotany was first, so let's give him some time to work on.
@ajomadlabs if you're interested in contributing, observe the "Good First Issue" label. I'm currently working on extending that set :)

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants