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 #12938

Closed
valeriangalliat opened this issue Jun 14, 2022 · 6 comments
Closed

[Bug]: User mock shadows Node module #12938

valeriangalliat opened this issue Jun 14, 2022 · 6 comments

Comments

@valeriangalliat
Copy link

Version

28.1.1

Steps to reproduce

cat << EOF > package.json
{
  "dependencies": {
    "jest": "^28.1.1",
    "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

Additional context

No response

Environment

System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Pro
  Binaries:
    Node: 18.3.0 - ~/.asdf/installs/nodejs/18.3.0/bin/node
    Yarn: 1.22.19 - ~/.asdf/installs/nodejs/18.3.0/.npm/bin/yarn
    npm: 8.11.0 - ~/.asdf/installs/nodejs/18.3.0/bin/npm
  npmPackages:
    jest: ^28.1.1 => 28.1.1
@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 Stale and removed Stale labels Jul 14, 2022
@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 Aug 13, 2022
@github-actions
Copy link

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

@github-actions
Copy link

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

@valeriangalliat
Copy link
Author

Still happening on 29.0.3

@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 Oct 13, 2022
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