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

Handle there being no parent module #19

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = moduleId => {

const parentPath = parentModule(__filename);

const filePath = resolveFrom(path.dirname(parentPath), moduleId);
const filePath = resolveFrom(path.dirname(parentPath || moduleId), moduleId);
JulienKode marked this conversation as resolved.
Show resolved Hide resolved

const oldModule = require.cache[filePath];
// Delete itself from module parent
Expand Down
21 changes: 21 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,24 @@ test('import when parent removed from cache', t => {
importer(id);
});
});

test('should not fail when there no parent module', t => {
const targetPath = require.resolve('parent-module');
const orignalModule = require.cache[targetPath];
delete require.cache[targetPath];
delete require.cache[require.resolve('.')];
require.cache[targetPath] = {
loaded: true,
id: targetPath,
exports: () => undefined
};

const id = './fixture-importer';
const importer = importFresh(id);

t.notThrows(() => {
importer(id);
});

require.cache[targetPath] = orignalModule;
});