-
-
Notifications
You must be signed in to change notification settings - Fork 833
Conversation
I want to use things like `jest.advanceTimersByTimeAsync`
"@types/katex": "^0.16.0", | ||
"@types/lodash": "^4.14.168", | ||
"@types/modernizr": "^3.5.3", | ||
"@types/node": "^16", | ||
"@types/node-fetch": "^2.6.2", | ||
"@types/pako": "^2.0.0", | ||
"@types/prettier": "^2.7.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like this was previously being pulled in transitively, but is no more.
@t3chguy sorry I needed some more stuff. Could you take another look? |
This is proving somewhat tricky. The problem is that Jest changed the semantics of |
`mockImplementation` is (now) reset by `jest.resetAllMocks` and `jest.restoreAllMocks`, which we don't really want here. Fixes `theme-test`
e1c1eb9
to
bcb41e6
Compare
This doesn't work well in test suites with multiple tests, because the `mockReturnValue` is reset for subsequent tests. In any case, having it mocked out automagically is *magical*. Let's make it opt-in.
|
||
let onFinished: () => void; | ||
|
||
beforeEach(() => { | ||
onFinished = jest.fn(); | ||
jest.spyOn(WidgetUtils, "canUserModifyWidgets").mockReturnValue(true); | ||
|
||
mockClient = { | ||
getUserId: jest.fn().mockReturnValue(userId), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
restoreAllMocks
now clears the mock return value, so move this in here to set it up before each test.
useMockedCalls(); | ||
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move these in here from the top of the describe
block, so they are set up afresh after the restoreAllMocks
in afterEach
// set up mediaDevices mock | ||
Object.defineProperty(navigator, "mediaDevices", { | ||
value: { | ||
enumerateDevices: jest.fn().mockResolvedValue([]), | ||
getUserMedia: jest.fn(), | ||
}, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this in here from setupManualMocks
, again to ensure that they are set up afresh after restoreAllMocks
.
.mockReset() | ||
.mockReturnValueOnce([]) | ||
.mockReturnValueOnce([pollStart]) | ||
.mockReturnValue(undefined as unknown as MatrixEvent[]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is necessary due to a change in the way mockReset
works.
https://jestjs.io/docs/29.3/mock-function-api#mockfnmockreset says:
Resetting a mock created with
jest.spyOn()
will result in a function with no return value.
This no longer appears to be the case as of 29.6 and we now get the original implementation instead.
So, to preserve current behaviour, have it return undefined
once we run out of mockReturnValueOnce
s.
@@ -203,7 +207,7 @@ describe("<PollHistory />", () => { | |||
.mockReturnValueOnce("test-pagination-token-3"); | |||
|
|||
const { getByText } = getComponent(); | |||
await flushPromises(); | |||
await act(flushPromises); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shoved some things in act
while I was debugging this test.
beforeEach(() => { | ||
openLinkModalSpy.mockReturnValue(undefined); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we were previously relying on resetAllMocks
to make this return undefined
. It doesn't any longer, so we need to do it explicitly.
@@ -28,7 +28,7 @@ describe("<SidebarUserSettingsTab />", () => { | |||
beforeEach(() => { | |||
jest.spyOn(PosthogTrackers, "trackInteraction").mockClear(); | |||
jest.spyOn(SettingsStore, "getValue").mockRestore(); | |||
jest.spyOn(SettingsStore, "setValue").mockReset(); | |||
jest.spyOn(SettingsStore, "setValue").mockResolvedValue(undefined); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another case of mockReset
not working the same way on spyOn
default: jest.fn().mockImplementation(({ url }) => <div>{url}</div>), | ||
default: ({ url }: { url: string }) => <div>{url}</div>, | ||
})); | ||
|
||
jest.mock("../../../../src/components/structures/HomePage", () => ({ | ||
__esModule: true, | ||
default: jest.fn().mockImplementation(() => <div>home page</div>), | ||
default: () => <div>home page</div>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avoid these being reset by restoreAllMocks
by using regular functions instead of mocks.
// TODO: Extract this to a function and have tests that need it opt into it. | ||
const mockMatchMedia = (query: string) => ({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avoid this being reset by restoreAllMocks
by using a regular function instead of a mock.
// spy on various toasts' hide and show functions | ||
// easier than mocking | ||
jest.spyOn(SetupEncryptionToast, "showToast").mockReturnValue(undefined); | ||
jest.spyOn(SetupEncryptionToast, "hideToast").mockReturnValue(undefined); | ||
jest.spyOn(BulkUnverifiedSessionsToast, "showToast").mockReturnValue(undefined); | ||
jest.spyOn(BulkUnverifiedSessionsToast, "hideToast").mockReturnValue(undefined); | ||
jest.spyOn(UnverifiedSessionToast, "showToast").mockResolvedValue(undefined); | ||
jest.spyOn(UnverifiedSessionToast, "hideToast").mockReturnValue(undefined); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we were previously relying on resetAllMocks to make these return undefined. It doesn't any longer, so we need to do it explicitly.
bcb41e6
to
fd9aa0e
Compare
I want to use things like
jest.advanceTimersByTimeAsync
based on #11413Fixes element-hq/element-web#24588
This change is marked as an internal change (Task), so will not be included in the changelog.