Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

reRequiring index files returns startMocking #36

Open
treygriffith opened this issue Nov 26, 2018 · 1 comment · May be fixed by #37
Open

reRequiring index files returns startMocking #36

treygriffith opened this issue Nov 26, 2018 · 1 comment · May be fixed by #37

Comments

@treygriffith
Copy link

When trying to reRequire a file named index that is in the local path, reRequire will instead return what mock-require exports, specifically the startMocking function.

Example code:

index.js:

const fs = require('fs')
// ...
module.exports = { fs }

test.js:

const mock = require('mock-require')

mock('fs', {})

const index = mock.reRequire('./index')

console.log(index)

Expected output:

{ fs: {} }

Actual output:

function startMocking(path, mockExport) {
  const calledFrom = getCallerFile();

  if (typeof mockExport === 'string') {
    mockExport = getFullPathNormalized(mockExport, calledFrom);
  }

  pendingMockExports[getFullPathNormalized(path, calledFrom)] = mockExport;
}

Based on looking at the code (and specifically at getFullPath and isInNodePath, it looks like any file that can be resolved from the mock-require package will cause it to return that file rather than the actual local file.

@treygriffith
Copy link
Author

My branch https://github.com/treygriffith/mock-require/tree/fix/local-collision illustrates the problem, which is only apparent when mock-require itself is in the node path, so doesn't show up in test cases for the module themselves.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant