Skip to content
Discussion options

You must be logged in to vote

main.js

const data= await ky.post('https://www.example.com').json()

main.test.js

// import ky
import ky from 'ky'

// mock ky instance
jest.mock('ky', () => ({
  post: jest.fn(),
  json: jest.fn(),
}))

// Test block
describe('Ky Tests', () => {
  it('renders component', async () => {
    const mockResponse = {}

   // Spy ky here
    jest.spyOn(ky, 'post').mockReturnThis()
    jest.spyOn(ky, 'json').mockReturnValue(mockResponse)

    render(
        <Component/>
    )

    await screen.findByRole('')
    expect(1).toBe(1)
  })
})

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by sindresorhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #487 on January 31, 2023 07:30.