Skip to content

Commit

Permalink
chore: try vitest for /bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
work-kevin-flynn committed Nov 25, 2024
1 parent 878ffdc commit e1ec825
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"scripts": {
"dev": "father dev",
"build": "father build",
"test": "NODE_OPTIONS='$NODE_OPTIONS --experimental-vm-modules' jest",
"test": "vitest",
"test:dev": "pnpm test -- --watch",
"test:coverage": "pnpm test -- --coverage",
"test:debug": "pnpm test -- --detectOpenHandles"
Expand Down
9 changes: 5 additions & 4 deletions packages/bridge/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { type TChainId } from '@aelf-web-login/wallet-adapter-base';
import { getCaContractBase, getIsManagerReadOnly } from '../utils';
import { getContractBasic } from '@portkey/contracts';
import { type Mock } from 'vitest';

afterEach(() => {
jest.unmock('@portkey/contracts');
vi.unmock('@portkey/contracts');
});
jest.mock('@portkey/contracts', () => ({
getContractBasic: jest.fn(),
vi.mock('@portkey/contracts', () => ({
getContractBasic: vi.fn(),
}));

describe('getCaContractBase()', () => {
Expand All @@ -25,7 +26,7 @@ describe('getCaContractBase()', () => {
chainId,
address: '',
};
(getContractBasic as jest.Mock).mockImplementation(() => mockContractBase);
(getContractBasic as Mock).mockImplementation(() => mockContractBase);
const contractBase = await getCaContractBase(chainId);
expect(contractBase).toMatchObject(mockContractBase);
});
Expand Down
42 changes: 42 additions & 0 deletions packages/bridge/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { defineConfig, configDefaults } from 'vitest/config';

const exclude = [...configDefaults.exclude, '**/dist/*.*', '**/.*', '**/*.setup.*'];

export default defineConfig({
test: {
maxConcurrency: 20,
pool: 'vmThreads',
poolOptions: {
threads: {
singleThread: true,
},
},
isolate: false, // only safe with the poolOptions above
css: false,
deps: {
optimizer: {
web: {
enabled: true,
include: ['node-fetch'],
},
},
},
globals: true,
environment: 'happy-dom',
setupFiles: ['./vitest.setup.ts'],
include: ['./src/**/*(*.)?{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
name: 'bridge',
exclude,
reporters: ['junit'],
outputFile: {
junit: './jest-report.xml',
},
coverage: {
enabled: true,
provider: 'v8',
exclude: [...exclude, '**/__tests__/*.*'],
reportsDirectory: './coverage',
reporter: [['json', { file: 'coverage-summary.json' }]],
},
},
});
40 changes: 40 additions & 0 deletions packages/bridge/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import '@testing-library/jest-dom';

// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
if (typeof window !== 'undefined') {
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
}

import { type TChainId } from '@aelf-web-login/wallet-adapter-base';
vi.mock('@aelf-web-login/wallet-adapter-portkey-aa', () => ({
PortkeyAAWallet: jest.fn(),
}));
vi.mock('@portkey/did-ui-react', () => ({
getChainInfo: (c: TChainId) => (!c ? null : {}),
did: {
didWallet: {
managementAccount: {
privateKey: '',
},
},
},
}));
vi.mock('lottie-web');
vi.mock('@portkey/utils');
vi.mock('node-fetch');
import { TextEncoder, TextDecoder } from 'util';
global.TextEncoder = TextEncoder;
// @ts-expect-error TextDecoder
global.TextDecoder = TextDecoder;
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
],
"@aelf-web-login/wallet-adapter-night-elf": ["./packages/wallets/night-elf/src"]
},
"types": ["node", "jest", "@testing-library/jest-dom"]
"types": ["node", "jest", "@testing-library/jest-dom", "vitest/globals"]
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "./tsconfig.base.json",
"include": ["./packages/"]
"include": ["./packages/", "packages/bridge/vitest.setup.ts/vitest.setup.ts"]
}

0 comments on commit e1ec825

Please sign in to comment.