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

Cannot mock paginate function #853

Closed
ApilPokhrel opened this issue Sep 4, 2023 · 1 comment
Closed

Cannot mock paginate function #853

ApilPokhrel opened this issue Sep 4, 2023 · 1 comment

Comments

@ApilPokhrel
Copy link

import * as nestjsTypeormPaginate from 'nestjs-typeorm-paginate';

jest.spyOn(nestjsTypeormPaginate, 'paginate').mockImplementationOnce(() => Promise.resolve({items: [], meta: {}}))

Actual Behaviour
TypeError: Cannot redefine property: paginate
at Function.defineProperty ()

Expected Behaviour
{items: [], meta: {}}

ApilPokhrel pushed a commit to ApilPokhrel/nestjs-typeorm-paginate that referenced this issue Sep 5, 2023
@bashleigh
Copy link
Collaborator

Ok sorry, I've just found the issue related to your PR. I think this is an issue related to the default export? Have you tried mocking the packge method without the default export and have you tried to mock the import instead? I think that would be a better method.

const mockPaginateMethod = jest.fn()
jest.mock('nestjs-typeorm-paginate', () => ({
  ...jest.requireActual('nestjs-typeorm-paginate'),
  paginate: mockPaginateMethod,
}))


// somewhere else

mockPaginate.mockImplementationOnce(() => Promise.resolve({items: [], meta: {}}))

Actually yea, try that. That'll work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants