Skip to content

Commit

Permalink
chore: clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
work-kevin-flynn committed Nov 25, 2024
1 parent 9e0536b commit 5e9212c
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 37 deletions.
4 changes: 2 additions & 2 deletions packages/bridge/coverage/coverage-summary.json
Original file line number Diff line number Diff line change
@@ -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}}
}
12 changes: 9 additions & 3 deletions packages/bridge/jest-report.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="jest tests" tests="1" failures="0" errors="0" time="0.914">
<testsuite name="getCaContractBase()" errors="0" failures="0" skipped="0" timestamp="2024-11-22T09:23:05" time="0.605" tests="1">
<testcase classname="getCaContractBase() should throw error if no chainId" name="getCaContractBase() should throw error if no chainId" time="0.003">
<testsuites name="jest tests" tests="4" failures="0" errors="0" time="0.919">
<testsuite name="getCaContractBase()" errors="0" failures="0" skipped="0" timestamp="2024-11-25T04:09:22" time="0.576" tests="4">
<testcase classname="getCaContractBase() should throw error about chain is not running" name="getCaContractBase() should throw error about chain is not running" time="0.002">
</testcase>
<testcase classname="getCaContractBase() should get back contract base" name="getCaContractBase() should get back contract base" time="0.001">
</testcase>
<testcase classname="getIsManagerReadOnly() should throw error about chain is not running" name="getIsManagerReadOnly() should throw error about chain is not running" time="0">
</testcase>
<testcase classname="getIsManagerReadOnly() should return false" name="getIsManagerReadOnly() should return false" time="0.004">
</testcase>
</testsuite>
</testsuites>
6 changes: 2 additions & 4 deletions packages/bridge/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/.*/)',
],
Expand All @@ -192,16 +191,15 @@ 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,
[k]: path.resolve(__dirname, `../../${compilerOptions.paths[k][0]}`),
}),
{},
),
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/empty.js',
'\\.(css|less|scss|sass)$': '<rootDir>/empty.js',
},
};

Expand Down
11 changes: 9 additions & 2 deletions packages/bridge/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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;
12 changes: 6 additions & 6 deletions packages/bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
48 changes: 48 additions & 0 deletions packages/bridge/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
14 changes: 0 additions & 14 deletions packages/bridge/src/utils.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/react/empty.js

This file was deleted.

7 changes: 2 additions & 5 deletions packages/react/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -207,9 +205,8 @@ const config: JestConfigWithTsJest = {
moduleDirectories: ['node_modules', 'src'],
moduleNameMapper: {
...pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>../../' }),
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/empty.js',
'\\.(css|less|scss|sass)$': '<rootDir>/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',
};
Expand Down

0 comments on commit 5e9212c

Please sign in to comment.