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

Mocking next files with jest.mock broken in packages #32964

Closed
spence-novata opened this issue Jan 3, 2022 · 3 comments
Closed

Mocking next files with jest.mock broken in packages #32964

spence-novata opened this issue Jan 3, 2022 · 3 comments
Labels
bug Issue was opened via the bug report template. Testing Related to testing with Next.js.

Comments

@spence-novata
Copy link

What version of Next.js are you using?

12.0.7

What version of Node.js are you using?

14.17.6

What browser are you using?

N/A

What operating system are you using?

macOS

How are you deploying your application?

jest

Describe the Bug

I'm trying to upgrade to Next 12 and cannot get my tests to pass as something strange is going on with jest.mock

If I use jest.mock to mock next/router and require it from my test or indeed my project the mock works as expected. However, if I require the router into another module the original router is required, not the mock.

I also tried to mock next/dist/client/script and found similar behavior so I don't think this is limited to the router. Adding my own file to the next/dist/client directory and it can be mocked fine. Changing the contents of one of the existing files to match my own test file in there did not though. I was unable to find anything in the package that made these files special and so that is a far as I was able to investigate.

Expected Behavior

The router should be mocked everywhere.

To Reproduce

node_modules/my-module/index.js

const router = require('next/router')

module.exports = {
  name: 'my-module',
  router,
}

app.js

const router = require('next/router')

module.exports = {
  name: 'app stuf',
  router,
}

tests/routerMock.test.js

const router = require('next/router')
const app = require('../app')
const myModule = require('my-module')

jest.mock('next/router', () => ({ isMocked: 'IS MOCKED' }))

describe('Router', () => {
  // PASSES
  it('is mocked in test', () => { 
    expect(router).toEqual({ isMocked: 'IS MOCKED' }) 
  })
  // PASSES
  it('is mocked in app', () => {
    expect(app.router).toEqual({ isMocked: 'IS MOCKED' })
  })
  // FAILS
  it('is mocked in other modules', () => {
    expect(myModule.router).toEqual({ isMocked: 'IS MOCKED' })
  })
})
@spence-novata spence-novata added the bug Issue was opened via the bug report template. label Jan 3, 2022
@balazsorban44 balazsorban44 added the Testing Related to testing with Next.js. label Jan 20, 2022
@timneutkens
Copy link
Member

Are you using next/jest or another configuration?

@spence-novata
Copy link
Author

This appears to have been fixed in #33731 and 12.0.10

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. Testing Related to testing with Next.js.
Projects
None yet
Development

No branches or pull requests

3 participants