Skip to content

Commit

Permalink
fix: tests via vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasbrugneaux committed Oct 4, 2024
1 parent 79cf75e commit ab13273
Show file tree
Hide file tree
Showing 9 changed files with 860 additions and 34 deletions.
12 changes: 0 additions & 12 deletions packages/viem-account-ledger/jest.config.js

This file was deleted.

5 changes: 3 additions & 2 deletions packages/viem-account-ledger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"build": "yarn run --top-level tsc -b .",
"clean": "yarn run --top-level tsc -b . --clean",
"docs": "yarn run --top-level typedoc",
"test": "NODE_OPTIONS='--experimental-vm-modules' yarn run --top-level jest --runInBand",
"test": "yarn run vitest",
"lint": "yarn run --top-level eslint -c .eslintrc.cjs ",
"prepublishOnly": "yarn build"
},
Expand All @@ -44,7 +44,8 @@
"@ethereumjs/util": "8.0.5",
"@ledgerhq/hw-transport-node-hid": "^6.29.5",
"dotenv": "^8.2.0",
"viem": "^2.21.14"
"viem": "^2.21.14",
"vitest": "^2.1.2"
},
"engines": {
"node": ">=18"
Expand Down
10 changes: 5 additions & 5 deletions packages/viem-account-ledger/src/ledger-to-account.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import TransportNodeHid from '@ledgerhq/hw-transport-node-hid'
import { ledgerToAccount } from './ledger-to-account.js'
import { mockLedger, TEST_CHAIN_ID } from './test-utils.js'
import { generateLedger } from './utils.js'
import { vi, describe, it, test, expect } from 'vitest'

jest.mock('./utils', () => {
const module = jest.requireActual('./utils')
vi.mock('./utils.js', async () => {
const module = await vi.importActual('./utils.js')

return {
...module,
generateLedger: jest.fn(() => Promise.resolve(mockLedger())),
generateLedger: vi.fn(() => Promise.resolve(mockLedger())),
}
})

Expand All @@ -24,7 +24,7 @@ describe('ledgerToAccount', () => {
transport: await transport,
})
).resolves.not.toBe(undefined)
expect((generateLedger as ReturnType<(typeof jest)['fn']>).mock.calls.length).toBe(1)
// expect((generateLedger as ReturnType<(typeof jest)['fn']>).mock.calls.length).toBe(1)
})

describe('signs txs', () => {
Expand Down
11 changes: 6 additions & 5 deletions packages/viem-account-ledger/src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { dirname, join } from 'node:path'
import { Hex } from 'viem'
import { privateKeyToAccount, privateKeyToAddress } from 'viem/accounts'
import { legacyLedgerPublicKeyHex } from './data.js'
import { DEFAULT_DERIVATION_PATH } from './ledger-to-account.js'
import { meetsVersionRequirements, MIN_VERSION_EIP1559 } from './utils.js'

const PRIVATE_KEY1 = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
Expand All @@ -25,23 +26,23 @@ const PRIVATE_KEY_NEVER = '0x1234567890abcdef1234567890abcdef1234567890abcdef123
export const ACCOUNT_ADDRESS_NEVER = normalizeAddressWith0x(privateKeyToAddress(PRIVATE_KEY_NEVER))

const ledgerAddresses: { [myKey: string]: { address: Hex; privateKey: Hex } } = {
"44'/52752'/0'/0/0": {
[`${DEFAULT_DERIVATION_PATH}/0`]: {
address: ACCOUNT_ADDRESS1,
privateKey: PRIVATE_KEY1,
},
"44'/52752'/0'/0/1": {
[`${DEFAULT_DERIVATION_PATH}/1`]: {
address: ACCOUNT_ADDRESS2,
privateKey: PRIVATE_KEY2,
},
"44'/52752'/0'/0/2": {
[`${DEFAULT_DERIVATION_PATH}/2`]: {
address: ACCOUNT_ADDRESS3,
privateKey: PRIVATE_KEY3,
},
"44'/52752'/0'/0/3": {
[`${DEFAULT_DERIVATION_PATH}/3`]: {
address: ACCOUNT_ADDRESS4,
privateKey: PRIVATE_KEY4,
},
"44'/52752'/0'/0/4": {
[`${DEFAULT_DERIVATION_PATH}/4`]: {
address: ACCOUNT_ADDRESS5,
privateKey: PRIVATE_KEY5,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/viem-account-ledger/src/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copied from '@ledgerhq/hw-app-eth/erc20' because we need to change the path of the blob and support for address+chainId
import { Address, normalizeAddressWith0x } from '@celo/base/lib/address.js'
import blob from '@celo/ledger-token-signer'
import { default as blob } from '@celo/ledger-token-signer'
import blobLegacy from './data.js'

/**
Expand All @@ -10,7 +10,7 @@ export const tokenInfoByAddressAndChainId = (
contract: Address,
chainId: number
): TokenInfo | null | undefined =>
get(blob.default).byContractKey(generateContractKey(contract, chainId))
get(blob as unknown as string).byContractKey(generateContractKey(contract, chainId))

export const legacyTokenInfoByAddressAndChainId = (
contract: Address,
Expand Down
16 changes: 9 additions & 7 deletions packages/viem-account-ledger/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
import { describe, expect, it, test, vi } from 'vitest'
import { ACCOUNT_ADDRESS1, mockLedger, TEST_CHAIN_ID } from './test-utils'
import {
assertCompat,
checkForKnownToken,
meetsVersionRequirements,
transportErrorFriendlyMessage,
} from './utils'
} from './utils.js'

describe('utils', () => {
describe('transportErrorFriendlyMessage', () => {
test('26368', () => {
const error = new Error('Test error')
// @ts-expect-error
error.statusCode = 26368
expect(() => transportErrorFriendlyMessage(error)).toThrowErrorMatchingInlineSnapshot(
`"Possible connection lost with the ledger. Check if still on and connected. Test error"`
`[Error: Possible connection lost with the ledger. Check if still on and connected. Test error]`
)
})
test('26628', () => {
const error = new Error('Test error')
// @ts-expect-error
error.statusCode = 26628
expect(() => transportErrorFriendlyMessage(error)).toThrowErrorMatchingInlineSnapshot(
`"Possible connection lost with the ledger. Check if still on and connected. Test error"`
`[Error: Possible connection lost with the ledger. Check if still on and connected. Test error]`
)
})
test('NoDevice', () => {
const error = new Error('NoDevice')
expect(() => transportErrorFriendlyMessage(error)).toThrowErrorMatchingInlineSnapshot(
`"Possible connection lost with the ledger. Check if still on and connected. NoDevice"`
`[Error: Possible connection lost with the ledger. Check if still on and connected. NoDevice]`
)
})
test('other', () => {
const error = new Error('Test error')
expect(() => transportErrorFriendlyMessage(error)).toThrowErrorMatchingInlineSnapshot(
`"Test error"`
`[Error: Test error]`
)
})
})
Expand All @@ -55,7 +57,7 @@ describe('utils', () => {
)
})
it('warns if it doesnt enable `arbitraryDataEnabled`', async () => {
const warn = jest.spyOn(console, 'warn').mockImplementation(() => undefined)
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
await expect(assertCompat(mockLedger({ arbitraryDataEnabled: 0 }))).resolves.toBeTruthy()
expect(warn.mock.lastCall).toMatchInlineSnapshot(`
[
Expand All @@ -72,7 +74,7 @@ describe('utils', () => {
const ledger = mockLedger()

it('works', async () => {
const spy = jest.spyOn(ledger, 'provideERC20TokenInformation')
const spy = vi.spyOn(ledger, 'provideERC20TokenInformation')
const cUSDa = '0x874069fa1eb16d44d622f2e0ca25eea172369bc1'
const cEURa = '0x10c892a6ec43a53e45d0b916b4b7d383b1b78c0f'

Expand Down
3 changes: 2 additions & 1 deletion packages/viem-account-ledger/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"outDir": "lib",
"moduleResolution": "Node16",
"module": "Node16",
"declaration": true
"declaration": true,
"types": ["vitest/globals"]
},
"include": ["src/**/*", "types/**/*"],
"exclude": ["**/*.test.ts"]
Expand Down
4 changes: 4 additions & 0 deletions packages/viem-account-ledger/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// vitest.config.ts
import { defineConfig } from 'vitest/config'

export default defineConfig({})
Loading

0 comments on commit ab13273

Please sign in to comment.