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

[Bug]: User mock shadows Node module #13252

Closed
valeriangalliat opened this issue Sep 12, 2022 · 5 comments
Closed

[Bug]: User mock shadows Node module #13252

valeriangalliat opened this issue Sep 12, 2022 · 5 comments

Comments

@valeriangalliat
Copy link

valeriangalliat commented Sep 12, 2022

When defining a mock e.g. lib/__mocks__/foo.js in order to mock lib/foo.js, Jest also auto mocks calls to require('foo') (and not just require('./lib/foo.js') which is unintended.

Version

29.0.3

Steps to reproduce

cat << EOF > package.json
{
  "dependencies": {
    "jest": "^29.0.3",
    "redis": "^4.1.0"
  }
}
EOF

mkdir -p lib/__mocks__ test

cat << EOF > lib/redis.js
const redis = require('redis')

module.exports = {
  client: redis
}
EOF

cat << EOF > lib/__mocks__/redis.js
module.exports = {
  the: 'mock'
}
EOF

cat << EOF > test/test.js
const redis = require('../lib/redis')

// jest.mock('../lib/redis')

console.log(redis)
EOF

npm install
npx jest

Expected behavior

Should log the Redis client from the redis module:

  console.log
    {
      client: {
        createClient: [Function: createClient],
        createCluster: [Function: createCluster],
        commandOptions: [Getter],
        defineScript: [Getter],
        GeoReplyWith: [Getter],
        AbortError: [Getter],
        WatchError: [Getter],
        ConnectionTimeoutError: [Getter],
        ClientClosedError: [Getter],
        DisconnectsClientError: [Getter],
        SocketClosedUnexpectedlyError: [Getter],
        RootNodesUnavailableError: [Getter],
        ReconnectStrategyError: [Getter],
        ErrorReply: [Getter],
        SchemaFieldTypes: [Getter],
        SchemaTextFieldPhonetics: [Getter],
        VectorAlgorithms: [Getter],
        AggregateSteps: [Getter],
        AggregateGroupByReducers: [Getter],
        TimeSeriesDuplicatePolicies: [Getter],
        TimeSeriesEncoding: [Getter],
        TimeSeriesAggregationType: [Getter]
      }
    }

Actual behavior

  console.log
    { client: { the: 'mock' } }

The require('redis') call imported the user mock for lib/redis.js which is unintended according to https://jestjs.io/docs/manual-mocks

The mock in in lib/__mocks__/redis.js should mock only lib/redis.js and not the redis package itself.

Additional context

No response

Environment

System:
    OS: macOS 12.5
    CPU: (10) arm64 Apple M1 Pro
  Binaries:
    Node: 18.6.0 - ~/.asdf/installs/nodejs/18.6.0/bin/node
    Yarn: 1.22.19 - ~/.asdf/installs/nodejs/18.6.0/.npm/bin/yarn
    npm: 8.13.2 - ~/.asdf/installs/nodejs/18.6.0/bin/npm
  npmPackages:
    jest: ^29.0.3 => 29.0.3

Follow up of #12938

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Oct 12, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 11, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@valeriangalliat
Copy link
Author

Looks like this is a special case of #2070

#2070 focuses on 2 different files in different directories in the user's code having the same name, but the fact the basenme of mocks is globally unique in jest also causes the same issue when a file in the user code have the same name as a third party dependency

@github-actions
Copy link

github-actions bot commented Jan 3, 2023

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 Jan 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant