This question is really good: Jest Mock module per test
It just don't answer my question on how to only mock it out in one single test and keep the rest using the original ponent.
That would make sense if I wish to do some mocking for an snapshot but not for the other tests
This is my current attempt
import React from 'react'
import { render } from '@testing-library/react'
jest.mock('ponents/Photo', () =>
jest.fn(() => jest.requireActual('ponents/Photo'))
)
import Photo from 'ponents/Photo'
import PhotoGrid from '.'
beforeEach(() => {
Photo.mockReset()
})
test('default renders 9 photos if provided 9', () => {
const photos = [...Array(9).keys()]
const { getAllByTestId, debug } = render(<PhotoGrid photos={photos} />)
debug()
expect(getAllByTestId(PHOTO_COMP_TEST_ID)).toHaveLength(9)
})
test('renders with masonry grid style', () => {
Photo.mockImplementation(() => <div />)
const photos = [...Array(9).keys()]
const { container, debug } = render(<PhotoGrid photos={photos} />)
debug()
expect(container).toMatchInlineSnapshot(`
<div>
...
</div>
`)
})
This is the ponent to test
import React from 'react'
import Masonry from 'react-masonry-css'
import './index.css'
import Photo from 'ponents/Photo'
function PhotoGrid({ photos, numberOfPhotos = 9 }) {
const imgs = photos ? photos.slice(0, numberOfPhotos) : []
const breakpointColumnsObj = {
default: 4,
1300: 3,
900: 2,
700: 1,
}
return (
<Masonry
breakpointCols={breakpointColumnsObj}
className="my-masonry-grid"
columnClassName="my-masonry-grid_column"
>
{imgs &&
imgs.map(({ id, secret, server, farm }, index) => (
<div key={index} className="masonry-item">
<Photo id={id} secret={secret} server={server} farm={farm} />
</div>
))}
</Masonry>
)
}
export default PhotoGrid
This question is really good: Jest Mock module per test
It just don't answer my question on how to only mock it out in one single test and keep the rest using the original ponent.
That would make sense if I wish to do some mocking for an snapshot but not for the other tests
This is my current attempt
import React from 'react'
import { render } from '@testing-library/react'
jest.mock('ponents/Photo', () =>
jest.fn(() => jest.requireActual('ponents/Photo'))
)
import Photo from 'ponents/Photo'
import PhotoGrid from '.'
beforeEach(() => {
Photo.mockReset()
})
test('default renders 9 photos if provided 9', () => {
const photos = [...Array(9).keys()]
const { getAllByTestId, debug } = render(<PhotoGrid photos={photos} />)
debug()
expect(getAllByTestId(PHOTO_COMP_TEST_ID)).toHaveLength(9)
})
test('renders with masonry grid style', () => {
Photo.mockImplementation(() => <div />)
const photos = [...Array(9).keys()]
const { container, debug } = render(<PhotoGrid photos={photos} />)
debug()
expect(container).toMatchInlineSnapshot(`
<div>
...
</div>
`)
})
This is the ponent to test
import React from 'react'
import Masonry from 'react-masonry-css'
import './index.css'
import Photo from 'ponents/Photo'
function PhotoGrid({ photos, numberOfPhotos = 9 }) {
const imgs = photos ? photos.slice(0, numberOfPhotos) : []
const breakpointColumnsObj = {
default: 4,
1300: 3,
900: 2,
700: 1,
}
return (
<Masonry
breakpointCols={breakpointColumnsObj}
className="my-masonry-grid"
columnClassName="my-masonry-grid_column"
>
{imgs &&
imgs.map(({ id, secret, server, farm }, index) => (
<div key={index} className="masonry-item">
<Photo id={id} secret={secret} server={server} farm={farm} />
</div>
))}
</Masonry>
)
}
export default PhotoGrid
Share
Improve this question
edited Aug 14, 2020 at 19:32
skyboyer
23.7k7 gold badges62 silver badges71 bronze badges
asked Aug 14, 2020 at 9:34
NorfeldtNorfeldt
9,68824 gold badges109 silver badges172 bronze badges
1
- is that working ? – La pach' Commented Nov 30, 2020 at 15:03
2 Answers
Reset to default 7I don't have the permission to add ments so I will just leave this here.
I have tried the solution offered by Han Daniels and I was getting the error:
TypeError: specificMockImpl.apply is not a function
.
I have fixed it by adding .default
after jest.requireActual('ponents/Photo')
because Photo is the default export.
So it is: jest.requireActual('ponents/Photo').default
jest.mock(...) the ponent you want to mock in one of tests.
Use jest.requireActual(...) to implement default behavior.
Use mockImplementation to implement custom behavior.
import React from 'react'
import { render } from '@testing-library/react'
jest.mock('ponents/Photo')
import Photo from 'ponents/Photo'
import PhotoGrid from '.'
// This maintains the original implementation for tests.
// This step is important because if not done, it will
// result in empty render errors.
beforeEach(() => {
Photo.mockImplementation(jest.requireActual('ponents/Photo'));
})
// This test will use original implementation of 'ponents/Photo'
test('default renders 9 photos if provided 9', () => {
const photos = [...Array(9).keys()]
const { getAllByTestId, debug } = render(<PhotoGrid photos={photos} />)
debug()
expect(getAllByTestId(PHOTO_COMP_TEST_ID)).toHaveLength(9)
})
// This test will use the mocked implementation of 'ponents/Photo'
test('renders with masonry grid style', () => {
Photo.mockImplementation(() => <div />)
const photos = [...Array(9).keys()]
const { container, debug } = render(<PhotoGrid photos={photos} />)
debug()
expect(container).toMatchInlineSnapshot(`
<div>
...
</div>
`)
})