From 5e9212c87b3fff4794aaf7ea64821d6ab7d03a27 Mon Sep 17 00:00:00 2001 From: "fred.tran" Date: Mon, 25 Nov 2024 12:05:52 +0800 Subject: [PATCH] chore: clean up tests --- .../bridge/coverage/coverage-summary.json | 4 +- packages/bridge/jest-report.xml | 12 +++-- packages/bridge/jest.config.ts | 6 +-- packages/bridge/jest.setup.ts | 11 ++++- packages/bridge/package.json | 12 ++--- packages/bridge/src/__tests__/utils.test.ts | 48 +++++++++++++++++++ packages/bridge/src/utils.test.ts | 14 ------ packages/react/empty.js | 1 - packages/react/jest.config.ts | 7 +-- 9 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 packages/bridge/src/__tests__/utils.test.ts delete mode 100644 packages/bridge/src/utils.test.ts delete mode 100644 packages/react/empty.js diff --git a/packages/bridge/coverage/coverage-summary.json b/packages/bridge/coverage/coverage-summary.json index fa8a510..488ca08 100644 --- a/packages/bridge/coverage/coverage-summary.json +++ b/packages/bridge/coverage/coverage-summary.json @@ -1,3 +1,3 @@ -{"total": {"lines":{"total":22,"covered":12,"skipped":0,"pct":54.54},"statements":{"total":27,"covered":13,"skipped":0,"pct":48.14},"functions":{"total":2,"covered":1,"skipped":0,"pct":50},"branches":{"total":5,"covered":1,"skipped":0,"pct":20},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"}} -,"/Users/aelf/Documents/Projects/aelf/aelf-web-login/packages/bridge/src/utils.ts": {"lines":{"total":22,"covered":12,"skipped":0,"pct":54.54},"functions":{"total":2,"covered":1,"skipped":0,"pct":50},"statements":{"total":27,"covered":13,"skipped":0,"pct":48.14},"branches":{"total":5,"covered":1,"skipped":0,"pct":20}} +{"total": {"lines":{"total":22,"covered":20,"skipped":0,"pct":90.9},"statements":{"total":27,"covered":22,"skipped":0,"pct":81.48},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"branches":{"total":5,"covered":5,"skipped":0,"pct":100},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"}} +,"/Users/aelf/Documents/Projects/aelf/aelf-web-login/packages/bridge/src/utils.ts": {"lines":{"total":22,"covered":20,"skipped":0,"pct":90.9},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":27,"covered":22,"skipped":0,"pct":81.48},"branches":{"total":5,"covered":5,"skipped":0,"pct":100}} } diff --git a/packages/bridge/jest-report.xml b/packages/bridge/jest-report.xml index d92d32f..685de98 100644 --- a/packages/bridge/jest-report.xml +++ b/packages/bridge/jest-report.xml @@ -1,7 +1,13 @@ - - - + + + + + + + + + \ No newline at end of file diff --git a/packages/bridge/jest.config.ts b/packages/bridge/jest.config.ts index 9e54d95..e04c7da 100644 --- a/packages/bridge/jest.config.ts +++ b/packages/bridge/jest.config.ts @@ -170,7 +170,6 @@ const config = { '^.+\\.(t|j)sx?$': '@swc/jest', }, // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation - // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation transformIgnorePatterns: [ '.pnpm/node_modules/(?!((jest-)?react-native(-.*)?|@react-native(-community)?|victory(-.*)?|uuid)|react-navigation|@shopify/react-native-skia|@react-navigation/.*/)', ], @@ -192,6 +191,8 @@ const config = { modulePaths: ['./', compilerOptions.baseUrl], moduleDirectories: ['node_modules', 'src'], moduleNameMapper: { + '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|css|less|scss|sass)$': + 'jest-transform-stub', ...Object.keys(compilerOptions.paths).reduce( (prev, k) => ({ ...prev, @@ -199,9 +200,6 @@ const config = { }), {}, ), - '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': - '/empty.js', - '\\.(css|less|scss|sass)$': '/empty.js', }, }; diff --git a/packages/bridge/jest.setup.ts b/packages/bridge/jest.setup.ts index edddde0..7317d3c 100644 --- a/packages/bridge/jest.setup.ts +++ b/packages/bridge/jest.setup.ts @@ -1,10 +1,10 @@ -// In your own jest-setup.js (or any other name) +import { type TChainId } from '@aelf-web-login/wallet-adapter-base'; import '@testing-library/jest-dom'; jest.mock('@aelf-web-login/wallet-adapter-portkey-aa', () => ({ PortkeyAAWallet: jest.fn(), })); jest.mock('@portkey/did-ui-react', () => ({ - getChainInfo: () => null, + getChainInfo: (c: TChainId) => (!c ? null : {}), did: { didWallet: { managementAccount: { @@ -13,4 +13,11 @@ jest.mock('@portkey/did-ui-react', () => ({ }, }, })); +jest.mock('lottie-web'); +jest.mock('@portkey/utils'); +import 'jest-canvas-mock'; global.fetch = require('node-fetch'); +import { TextEncoder, TextDecoder } from 'util'; +global.TextEncoder = TextEncoder; +// @ts-expect-error TextDecoder +global.TextDecoder = TextDecoder; diff --git a/packages/bridge/package.json b/packages/bridge/package.json index 39852a0..65c3431 100755 --- a/packages/bridge/package.json +++ b/packages/bridge/package.json @@ -33,24 +33,24 @@ }, "dependencies": { "@aelf-web-login/wallet-adapter-base": "workspace:*", - "@portkey/services": "^2.15.9", "@portkey/contracts": "^2.15.9", + "@portkey/services": "^2.15.9", "@reduxjs/toolkit": "^2.2.3", "react": "^18.0.0", "react-dom": "^18.0.0" }, "devDependencies": { - "@portkey/types": "^2.15.9", "@portkey/did-ui-react": "^2.15.9", + "@portkey/types": "^2.15.9", "@portkey/utils": "^2.15.9", + "@swc/core": "^1.9.3", + "@swc/jest": "^0.2.37", "@types/react": "^18.3.1", "@types/react-dom": "^18.3.0", "antd": "4.24.14", "father": "^4.3.8", - "typescript": "^5.3.3", - "@swc/core": "^1.9.3", - "@swc/jest": "^0.2.37", - "jest": "^29.7.0" + "jest": "^29.7.0", + "typescript": "^5.3.3" }, "peerDependencies": { "@portkey/did-ui-react": "^2.15.9" diff --git a/packages/bridge/src/__tests__/utils.test.ts b/packages/bridge/src/__tests__/utils.test.ts new file mode 100644 index 0000000..ae5874e --- /dev/null +++ b/packages/bridge/src/__tests__/utils.test.ts @@ -0,0 +1,48 @@ +import { type TChainId } from '@aelf-web-login/wallet-adapter-base'; +import { getCaContractBase, getIsManagerReadOnly } from '../utils'; +import { getContractBasic } from '@portkey/contracts'; + +afterEach(() => { + jest.unmock('@portkey/contracts'); +}); +jest.mock('@portkey/contracts', () => ({ + getContractBasic: jest.fn(), +})); + +describe('getCaContractBase()', () => { + it('should throw error about chain is not running', async () => { + const chainId = null; + try { + await getCaContractBase(chainId as unknown as TChainId); + } catch (error) { + expect(error).toBeInstanceOf(Error); + expect(error).toHaveProperty('message', `Chain is not running: ${chainId}`); + } + }); + it('should get back contract base', async () => { + const chainId: TChainId = 'tDVW'; + const mockContractBase = { + chainId, + address: '', + }; + (getContractBasic as jest.Mock).mockImplementation(() => mockContractBase); + const contractBase = await getCaContractBase(chainId); + expect(contractBase).toMatchObject(mockContractBase); + }); +}); + +describe('getIsManagerReadOnly()', () => { + it('should throw error about chain is not running', async () => { + const chainId = null; + try { + await getIsManagerReadOnly(chainId as unknown as TChainId); + } catch (error) { + expect(error).toBeInstanceOf(Error); + expect(error).toHaveProperty('message', `Chain is not running: ${chainId}`); + } + }); + it('should return false', async () => { + const r = await getIsManagerReadOnly('tDVV'); + expect(r).toBeFalsy(); + }); +}); diff --git a/packages/bridge/src/utils.test.ts b/packages/bridge/src/utils.test.ts deleted file mode 100644 index 57d4ebc..0000000 --- a/packages/bridge/src/utils.test.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { type TChainId } from '@aelf-web-login/wallet-adapter-base'; -import { getCaContractBase } from './utils'; - -describe('getCaContractBase()', () => { - it('should throw error if no chainId', async () => { - const chainId: TChainId = 'tDVV'; - try { - await getCaContractBase(chainId); - } catch (error) { - expect(error).toBeInstanceOf(Error); - expect(error).toHaveProperty('message', `Chain is not running: ${chainId}`); - } - }); -}); diff --git a/packages/react/empty.js b/packages/react/empty.js deleted file mode 100644 index 9dc5fc1..0000000 --- a/packages/react/empty.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = ''; diff --git a/packages/react/jest.config.ts b/packages/react/jest.config.ts index faa3d90..caf397c 100644 --- a/packages/react/jest.config.ts +++ b/packages/react/jest.config.ts @@ -17,8 +17,6 @@ const presetConfig = createDefaultEsmPreset({ isolatedModules: true, // perf }); -console.log(Object.values(presetConfig.transform)[0][1].tsconfig); - const config: JestConfigWithTsJest = { ...presetConfig, // All imported modules in your tests should be mocked automatically @@ -207,9 +205,8 @@ const config: JestConfigWithTsJest = { moduleDirectories: ['node_modules', 'src'], moduleNameMapper: { ...pathsToModuleNameMapper(compilerOptions.paths, { prefix: '../../' }), - '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': - '/empty.js', - '\\.(css|less|scss|sass)$': '/empty.js', + '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|css|less|scss|sass)$': + 'jest-transform-stub', }, preset: 'ts-jest', };