Skip to content

Instantly share code, notes, and snippets.

@Jayphen
Created June 24, 2021 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jayphen/28a5f5d02f218cd05704d929648ddb3e to your computer and use it in GitHub Desktop.
Save Jayphen/28a5f5d02f218cd05704d929648ddb3e to your computer and use it in GitHub Desktop.
spy on a component but also keep its implementation
// for when you REALLY need to test the props SomeComponent is rendered with (not recommended)
// create a mock that re-implements the default behaviour
// but allows us to spy on the props
jest.mock('components/SomeComponent', () => {
const original = jest.requireActual('components/SomeComponent')
return jest.fn(original.default)
})
it('does the thing', () => {
render(<ThingThatRendersSomeComponent />)
expect(SomeComponent).toHaveBeenLastCalledWith(
// props
expect.objectContaining({ some: 'propValue' }),
// context
expect.anything()
)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment