Skip to content

Commit

Permalink
Implement module.parent.filename
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 6, 2017
1 parent cd3becf commit e0b5bdc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
19 changes: 16 additions & 3 deletions packages/jest-runtime/src/__tests__/runtime_require_module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

'use strict';

const path = require('path');
import path from 'path';
import slash from 'slash';

let createRuntime;

Expand All @@ -34,7 +35,7 @@ describe('Runtime requireModule', () => {
);
expect(exports.parent).toEqual({
exports: {},
filename: 'mock.js',
filename: '',
id: 'mockParent',
require: expect.any(Function),
});
Expand All @@ -46,7 +47,19 @@ describe('Runtime requireModule', () => {
runtime.__mockRootPath,
'inner_parent_module',
);
expect(exports).toEqual('This should happen');
expect(exports.outputString).toEqual('This should happen');
}));

it('resolve module.parent.filename correctly', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'inner_parent_module',
);

expect(slash(exports.parentFileName.replace(__dirname, ''))).toEqual(
'/test_root/node_modules/module-needing-parent/index.js',
);
}));

it('provides `module.filename` to modules', () =>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ const getModuleNameMapper = (config: ProjectConfig) => {

const mockParentModule = {
exports: {},
filename: 'mock.js',
id: 'mockParent',
};

Expand Down Expand Up @@ -497,6 +496,7 @@ class Runtime {
localModule.paths = this._resolver.getModulePaths(dirname);
localModule.require = this._createRequireImplementation(filename, options);
localModule.parent = Object.assign({}, localModule.parent, {
filename: lastExecutingModulePath,
require: this._createRequireImplementation(
lastExecutingModulePath,
options,
Expand Down

0 comments on commit e0b5bdc

Please sign in to comment.