-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
Getting from MockedFunction to Mock #9
Comments
This change allows `MockedFunction` to be used where `Mock` can be used. I don't think this fundamentally changes `MockedFunction`, just makes it more robust. This would resolve issues where an extended expect method takes a `Mock` type, but the mocking library uses `MockedFunction`. Related issues: extend-chrome/jest-chrome#9 jest-community/jest-extended#292
Great question! It looks like there's an open PR in The root of the problem is that This could be solved two ways:
Both probably need to happen. I've made a draft PR in |
@yakirn Maybe you could cast to expect(chrome.tabs.update).toHaveBeenCalledAfter(chrome.cookies.set as jest.MockInstance); |
@jacksteamdev casting to jest.Mock did the trick!
Anything left to do with the PR do DefinitelyTyped? If you can fill me in I can try and continue over the weekend. |
@yakirn I finished the DefinitelyTyped PR. I wanted to add some tests. We'll see how it goes 🤞 We should upvote the PR in |
Thank you so much @jacksteamdev ! I upvoted and I’ll ask my coworkers to do the same :) |
@yakirn It looks like these PRs probably won't go anywhere fast. 😞 But, we can use module augmentation to fix it locally. Create a declaration file, (ie, /// <reference types="jest" />
declare namespace jest {
interface Matchers {
/**
* Use `.toHaveBeenCalledBefore` when checking if a `MockInstance` was called before another `MockInstance`.
*
* Note: Required Jest version >=23
*
* @param {MockInstance} mock
*/
toHaveBeenCalledBefore(mock: jest.MockInstance): R
/**
* Use `.toHaveBeenCalledAfter` when checking if a `MockInstance` was called after another `MockInstance`.
*
* Note: Required Jest version >=23
*
* @param {MockInstance} mock
*/
toHaveBeenCalledAfter(mock: jest.MockInstance): R
}
interface Expect {
/**
* Use `.toHaveBeenCalledBefore` when checking if a `MockInstance` was called before another `MockInstance`.
*
* Note: Required Jest version >=23
*
* @param {MockInstance} mock
*/
toHaveBeenCalledBefore(mock: jest.MockInstance): R
/**
* Use `.toHaveBeenCalledAfter` when checking if a `MockInstance` was called after another `MockInstance`.
*
* Note: Required Jest version >=23
*
* @param {MockInstance} mock
*/
toHaveBeenCalledAfter(mock: jest.MockInstance): R
}
} I've tried it out like this, and it works for me! import { chrome } from "jest-chrome";
import "jest-extended";
test('toBeCalledAfter allows MockedFunction', () => {
expect(chrome.alarms.clear).toHaveBeenCalledAfter(chrome.alarms.create)
}) |
Hey,
First of all, thank you for an awesome extension!
Second, please see this unanswered question.
It's more of a quality of life thing than an actual issue (I hate to use
any
in TS).Figured since the code works just fine there must be a less risky way to tell TS it's fine.
The assertion is from jest-extended
TL;DR
I'm trying to make TS to accept the following line but without casting to
any
:expect(chrome.tabs.update).toHaveBeenCalledAfter(chrome.cookies.set as any);
Thanks!
The text was updated successfully, but these errors were encountered: