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

Fix extension mocking during tests #3773

Merged
merged 6 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
prerelease: ${{ contains(github.ref, '-pre') || contains(github.ref, 'v0.') }}
- name: Upload build asset
if: ${{ !startsWith(github.ref, 'refs/tags/') }}
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: extension-builds-${{ github.event.number || github.event.head_commit.id }}
path: dist/*.zip
Expand Down Expand Up @@ -106,7 +106,7 @@ jobs:
USE_MAINNET_FORK: true
- name: Upload build asset
if: ${{ !startsWith(github.ref, 'refs/tags/') }}
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: extension-builds-fork-${{ github.event.number || github.event.head_commit.id }}
path: dist/*.zip
Expand Down Expand Up @@ -179,7 +179,7 @@ jobs:
- run: yarn install --frozen-lockfile
- name: Install Playwright Browsers
run: npx playwright install --with-deps chromium
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: extension-builds-${{ github.event.number || github.event.head_commit.id }}
- name: Extract extension
Expand All @@ -205,7 +205,7 @@ jobs:
TEST_WALLET_JSON_BODY: ${{ secrets.TEST_WALLET_JSON_BODY }}
TEST_WALLET_JSON_PASSWORD: ${{ secrets.TEST_WALLET_JSON_PASSWORD }}
run: xvfb-run npx playwright test --grep @expensive
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: failure()
with:
name: debug-output
Expand Down Expand Up @@ -234,7 +234,7 @@ jobs:
- run: yarn install --frozen-lockfile
- name: Install Playwright Browsers
run: npx playwright install --with-deps chromium
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: extension-builds-fork-${{ github.event.number || github.event.head_commit.id }}
- name: Extract extension
Expand Down Expand Up @@ -268,7 +268,7 @@ jobs:
TEST_WALLET_JSON_PASSWORD: ${{ secrets.TEST_WALLET_JSON_PASSWORD }}
USE_MAINNET_FORK: true
run: xvfb-run npx playwright test
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: failure()
with:
name: fork-debug-output
Expand Down
42 changes: 0 additions & 42 deletions __mocks__/webextension-polyfill.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ type AbilitiesServiceExternalized = Omit<AbilitiesService, "db"> & {
db: AbilitiesDatabase
}

describe("AbilitiesService", () => {
it.todo("Abilities has been disabled")

describe.skip("AbilitiesService", () => {
const sandbox = sinon.createSandbox()
let abilitiesService: AbilitiesService
let chainService: ChainService
Expand Down
7 changes: 5 additions & 2 deletions background/services/chain/tests/index.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
TransactionRequestWithNonce,
} from "../../../networks"
import {
createAddressOnNetwork,
createAnyEVMTransaction,
createChainService,
createLegacyTransactionRequest,
Expand Down Expand Up @@ -449,6 +450,8 @@ describe("ChainService", () => {

describe("getNetworksToTrack", () => {
it("Should fetch built-in and custom networks to track", async () => {
const account = createAddressOnNetwork()

await chainService.addCustomChain({
chainName: "Foo",
chainId: "12345",
Expand All @@ -462,7 +465,7 @@ describe("ChainService", () => {
})

await chainService.addAccountToTrack({
address: "0x123",
address: account.address,
network: {
name: "Foo",
chainID: "12345",
Expand All @@ -477,7 +480,7 @@ describe("ChainService", () => {
})

await chainService.addAccountToTrack({
address: "0x123",
address: account.address,
network: ETHEREUM,
})
const networksToTrack = await chainService.getNetworksToTrack()
Expand Down
2 changes: 1 addition & 1 deletion background/services/chain/tests/index.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe("Chain Service", () => {
.stub(chainService, "markNetworkActivity")
.callsFake(async () => {})

chainService.markAccountActivity(
await chainService.markAccountActivity(
createAddressOnNetwork({ network: ETHEREUM }),
)

Expand Down
17 changes: 10 additions & 7 deletions background/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TransactionResponse,
} from "@ethersproject/abstract-provider"
import { DexieOptions } from "dexie"
import { BigNumber } from "ethers"
import { BigNumber, Wallet } from "ethers"
import { keccak256 } from "ethers/lib/utils"
import { AccountBalance, AddressOnNetwork } from "../accounts"
import {
Expand All @@ -27,7 +27,6 @@ import {
USD,
} from "../constants"
import { DaylightAbility } from "../lib/daylight"
import { normalizeEVMAddress } from "../lib/utils"
import {
AnyEVMTransaction,
LegacyEVMTransactionRequest,
Expand Down Expand Up @@ -56,13 +55,17 @@ import {
QueuedTxToRetrieve,
} from "../services/chain"
import { EIP712TypedData } from "../types"
import { normalizeEVMAddress } from "../lib/utils"

// We don't want the chain service to use a real provider in tests
jest.mock("../services/chain/serial-fallback-provider")

const createRandom0xHash = () =>
keccak256(Buffer.from(Math.random().toString()))

export const createRandomAddress = () =>
normalizeEVMAddress(Wallet.createRandom().address)

export const createPreferenceService = async (): Promise<PreferenceService> =>
PreferenceService.create()

Expand Down Expand Up @@ -309,7 +312,7 @@ export const createAnyEVMBlock = (
export const createAccountBalance = (
overrides: Partial<AccountBalance> = {},
): AccountBalance => ({
address: createRandom0xHash(),
address: createRandomAddress(),
assetAmount: {
asset: {
metadata: {
Expand All @@ -333,7 +336,7 @@ export const createAccountBalance = (
export const createAddressOnNetwork = (
overrides: Partial<AddressOnNetwork> = {},
): AddressOnNetwork => ({
address: normalizeEVMAddress(createRandom0xHash()),
address: createRandomAddress(),
network: ETHEREUM,
...overrides,
})
Expand Down Expand Up @@ -400,7 +403,7 @@ export const createTransactionResponse = (
blockHash: createRandom0xHash(),
timestamp: Date.now(),
confirmations: 0,
from: createRandom0xHash(),
from: createRandomAddress(),
nonce: 570,
gasLimit: BigNumber.from(15000000),
data: "...",
Expand Down Expand Up @@ -468,7 +471,7 @@ export const createSmartContractAsset = (
symbol,
decimals: 18,
homeNetwork: ETHEREUM,
contractAddress: createRandom0xHash(),
contractAddress: createRandomAddress(),
}

return {
Expand All @@ -492,7 +495,7 @@ export const createNetworkBaseAsset = (
decimals: 18,
coinType: 60,
chainID: "1",
contractAddress: createRandom0xHash(),
contractAddress: createRandomAddress(),
}

return {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"default"
],
"setupFiles": [
"./setup-extension-mock.ts",
"fake-indexeddb/auto",
"jest-webextension-mock",
"./setupJest.ts"
],
"setupFilesAfterEnv": [
Expand Down Expand Up @@ -126,6 +126,7 @@
"install": "^0.13.0",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"jest-webextension-mock": "^3.7.22",
"npm": "^7.5.6",
"npm-run-all": "^4.1.5",
"patch-package": "^6.4.7",
Expand Down
30 changes: 30 additions & 0 deletions setup-extension-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "jest-webextension-mock"

global.chrome.runtime.id = "mocked-extension-runtime-id"

Object.assign(global.chrome, {
...global.chrome,
runtime: {
...global.chrome.runtime,
setUninstallURL: jest.fn(),
},
windows: {
...global.chrome.windows,
getCurrent: jest.fn(),
create: jest.fn(),
onRemoved: {
...(global.chrome.windows?.onRemoved ?? {}),
addListener: jest.fn(),
},
},
alarms: {
...global.chrome.alarms,
create: jest.fn(),
clear: jest.fn(),
onAlarm: {
...(global.chrome.alarms?.onAlarm ?? {}),
addListener: jest.fn(),
removeListener: jest.fn(),
},
},
})
2 changes: 1 addition & 1 deletion setupJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const IS_CI = process.env.CI === "true"
// CI workflows to get an overview of both failed expectations and possible errors.
// This is not the case during development, hence, we set the minimum log level to warning
// as it helps with debugging while writing new tests.
logger.logLevel = IS_CI ? LogLevel.error : LogLevel.warn
logger.logLevel = IS_CI ? LogLevel.off : LogLevel.warn
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not want error logs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I removed it because jest already points at where a test failed


// ref: https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
// ref: https://github.com/jsdom/jsdom/issues/2524
Expand Down
Loading