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

require.cache always be {} in one module? #5741

Closed
DelBlank opened this issue Mar 7, 2018 · 2 comments
Closed

require.cache always be {} in one module? #5741

DelBlank opened this issue Mar 7, 2018 · 2 comments

Comments

@DelBlank
Copy link

DelBlank commented Mar 7, 2018

A bug?

here is load-uncached.js

module.exports = module => {
  let moduleExport
  try {
    const modulePath = require.resolve(module)
    for (let key in require.cache) {
      delete require.cache[key]
    }
    moduleExport = require(modulePath)
  } catch (err) {
    ....
  }

  return moduleExport
}

here is load-uncached.test.js:

jest.mock('args', () => ({}))

require('./router')  // add  router module to require.cache
const loadUncached = require('load-uncached')

describe('test load-uncached', () => {
  afterAll(() => {
    jest.resetModules()
  })

  it('should throw error when mock config file does not exist', () => {
    expect(() => loadUncached('xxx')).toThrow(/xxx/)
  })

  it('should require uncached module', () => {
    const moduleExport = loadUncached('../src/args')
    expect(moduleExport).toEqual({})
  })
})

What is the current behavior?

it seems that require.cache always be {} in load-uncached.js.

What is the expected behavior?

require.cache in load-uncached.js should be {router: ...} when require('router') in load-uncached.test.js.

my jest configuration in package.json:

"jest": {
    "verbose": true,
    "notify": true,
    "testEnvironment": "node",
    "collectCoverage": true,
    "roots": [
      "<rootDir>/__tests__"
    ],
    "modulePaths": [
      "<rootDir>/src"
    ],
    "moduleDirectories": [
      "<rootDir>/__mocks__",
      "node_modules"
    ],
    "coverageReporters": [
      "html",
      "text",
      "text-summary"
    ]
  }

jest: v22.4.2
npm: v5.5.1
node: v9.3.0
os: v10.12.3

@SimenB
Copy link
Member

SimenB commented Mar 7, 2018

Jest does not implement require.cache, it's always just {} - it has its own cache. Use jest.resetModules() to clear it

@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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants