Skip to content

Instantly share code, notes, and snippets.

@hyphenized
Created March 14, 2023 21:10
Show Gist options
  • Save hyphenized/4ce1e44767c9f320d9470e008242b0cd to your computer and use it in GitHub Desktop.
Save hyphenized/4ce1e44767c9f320d9470e008242b0cd to your computer and use it in GitHub Desktop.
test-fix.patch
diff --git a/background/services/provider-bridge/tests/index.unit.test.ts b/background/services/provider-bridge/tests/index.unit.test.ts
index 842fc4b6..0cbb1104 100644
--- a/background/services/provider-bridge/tests/index.unit.test.ts
+++ b/background/services/provider-bridge/tests/index.unit.test.ts
@@ -5,10 +5,10 @@ import {
import sinon from "sinon"
import browser from "webextension-polyfill"
import * as featureFlags from "../../../features"
-import { wait } from "../../../lib/utils"
import { createProviderBridgeService } from "../../../tests/factories"
import { AddEthereumChainParameter } from "../../internal-ethereum-provider"
import ProviderBridgeService from "../index"
+import * as popupUtils from "../show-popup"
import { validateAddEthereumChainParameter } from "../utils"
const WINDOW = {
@@ -131,6 +131,7 @@ describe("ProviderBridgeService", () => {
const { enablingPermission } = BASE_DATA
jest.spyOn(featureFlags, "isEnabled").mockImplementation(() => true)
+ const showPopupSpy = jest.spyOn(popupUtils, "default")
const request = providerBridgeService.routeContentScriptRPCRequest(
{
@@ -145,7 +146,7 @@ describe("ProviderBridgeService", () => {
const IEP = providerBridgeService["internalEthereumProviderService"]
const spy = jest.spyOn(IEP, "routeSafeRPCRequest")
- await wait(0) // wait next tick to setup popup
+ await expect.pass(() => expect(showPopupSpy).toHaveBeenCalled())
const validatedPayload = validateAddEthereumChainParameter(
params[0] as AddEthereumChainParameter
@@ -160,12 +161,12 @@ describe("ProviderBridgeService", () => {
expect(spy).not.toHaveBeenCalled()
providerBridgeService.handleAddNetworkRequest("0", true)
- await wait(0) // wait next tick
-
- expect(spy).toHaveBeenCalledWith(
- "wallet_addEthereumChain",
- [validatedPayload, "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"],
- BASE_DATA.origin
+ await expect.pass(() =>
+ expect(spy).toHaveBeenCalledWith(
+ "wallet_addEthereumChain",
+ [validatedPayload, "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"],
+ BASE_DATA.origin
+ )
)
await expect(request).resolves.toEqual(null) // resolves without errors
diff --git a/setupJest.env.ts b/setupJest.env.ts
index b1e81770..9d825bf3 100644
--- a/setupJest.env.ts
+++ b/setupJest.env.ts
@@ -1,3 +1,5 @@
+import { wait } from "@tallyho/tally-background/lib/utils"
+
/* Reset IndexedDB between tests */
beforeEach(() => {
global.indexedDB = new IDBFactory()
@@ -8,6 +10,27 @@ afterEach(() => {
global.indexedDB = new IDBFactory()
})
+expect.pass = async function waitUntilPass(callback, timeout = 1000) {
+ let retry = true
+
+ setTimeout(() => {
+ retry = false
+ }, timeout)
+
+ let lastError
+
+ while (retry) {
+ try {
+ // eslint-disable-next-line no-await-in-loop
+ return await wait(0).then(callback)
+ } catch (error) {
+ lastError = error
+ }
+ }
+
+ throw lastError
+}
+
it.flaky = function checkFlaky(label: string, testCase: () => unknown): void {
// eslint-disable-next-line no-only-tests/no-only-tests
it.only(label, () => {
@@ -19,12 +42,22 @@ it.flaky = function checkFlaky(label: string, testCase: () => unknown): void {
})
}
-// eslint-disable-next-line @typescript-eslint/no-namespace, @typescript-eslint/no-unused-vars
-declare namespace jest {
- interface It {
- /**
- * Used for debugging flaky tests
- */
- flaky: (label: string, testCase: () => unknown) => void
+declare global {
+ // eslint-disable-next-line @typescript-eslint/no-namespace
+ namespace jest {
+ interface It {
+ /**
+ * Used for debugging flaky tests
+ */
+ flaky: (label: string, testCase: () => unknown) => void
+ }
+
+ interface Expect {
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ pass: (
+ assertion: () => Promise<void> | void,
+ timeout?: number
+ ) => Promise<void>
+ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment