Skip to content

Commit

Permalink
chore: extract common internal config
Browse files Browse the repository at this point in the history
  • Loading branch information
work-kevin-flynn committed Nov 26, 2024
1 parent 4756be0 commit 8788f2e
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 106 deletions.
1 change: 1 addition & 0 deletions packages/bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"@aelf-web-login/wallet-adapter-base": "workspace:*",
"@aelf-web-login/tools": "workspace:*",
"@portkey/contracts": "^2.15.9",
"@portkey/services": "^2.15.9",
"@reduxjs/toolkit": "^2.2.3",
Expand Down
47 changes: 3 additions & 44 deletions packages/bridge/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,4 @@
import { defineConfig, configDefaults } from 'vitest/config';
import tsconfigPaths from 'vite-tsconfig-paths';
import baseViteConfig from '../tools/vite.config';
import { mergeConfig } from 'vitest/config';

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

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

beforeAll(() => {
global.ResizeObserver = class ResizeObserver {
observe() {
// do nothing
}
unobserve() {
// do nothing
}
disconnect() {
// do nothing
}
};
});

afterEach(() => {
cleanup();
});

// 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: vi.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;
import '../tools/__mocks__/setupGlobal';
import '../tools/__mocks__/setupLocal';
5 changes: 5 additions & 0 deletions packages/tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Why another package?

During migration to vitest, there's a need to both provide common config and mocks without polluting workspace root, and establish clear boundary to import/export such configs.

This package is _meant_ to be internally consumed, thus `private: true` in package.json and not to be published.
46 changes: 46 additions & 0 deletions packages/tools/__mocks__/setupGlobal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import '@testing-library/jest-dom';
import { cleanup } from '@testing-library/react';
import { afterEach, beforeAll, vi } from 'vitest';

beforeAll(() => {
global.ResizeObserver = class ResizeObserver {
observe() {
// do nothing
}
unobserve() {
// do nothing
}
disconnect() {
// do nothing
}
};
});

afterEach(() => {
cleanup();
});

// 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 { TextEncoder, TextDecoder } from 'util';
global.TextEncoder = TextEncoder;
// @ts-expect-error TextDecoder
global.TextDecoder = TextDecoder;

vi.mock('lottie-web');
vi.mock('node-fetch');
17 changes: 17 additions & 0 deletions packages/tools/__mocks__/setupLocal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { vi } from 'vitest';
import { type TChainId } from '@aelf-web-login/wallet-adapter-base';

vi.mock('@portkey/utils');
vi.mock('@aelf-web-login/wallet-adapter-portkey-aa', () => ({
PortkeyAAWallet: vi.fn(),
}));
vi.mock('@portkey/did-ui-react', () => ({
getChainInfo: (c: TChainId) => (!c ? null : {}),
did: {
didWallet: {
managementAccount: {
privateKey: '',
},
},
},
}));
13 changes: 13 additions & 0 deletions packages/tools/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { resolve } from 'path';
// relative path is still required here before aliases are enabled
import baseTSConfig from '../../tsconfig.base.json';

export const getTSConfigPaths = () => {
const aliases: Record<string, string> = {};
for (const [key, value] of Object.entries<string[]>(baseTSConfig.compilerOptions.paths)) {
aliases[key] = resolve(__dirname, value[0]);
}
return aliases;
};

export default {};
14 changes: 14 additions & 0 deletions packages/tools/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@aelf-web-login/tools",
"private": true,
"version": "1.0.0",
"description": "",
"main": "index.ts",
"files": [
"*"
],
"scripts": {},
"keywords": [],
"author": "",
"license": "UNLICENSED"
}
4 changes: 4 additions & 0 deletions packages/tools/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.base.json",
"include": ["*.ts", "**/*.ts"]
}
48 changes: 48 additions & 0 deletions packages/tools/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { defineConfig, configDefaults } from 'vitest/config';
import tsconfigPaths from 'vite-tsconfig-paths';

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

export default defineConfig({
plugins: [tsconfigPaths()],
test: {
maxConcurrency: 20,
pool: 'vmThreads',
poolOptions: {
threads: {
singleThread: true,
},
},
isolate: false, // only safe with the poolOptions above
css: false,
deps: {
optimizer: {
web: {
enabled: true,
},
},
},
globals: true,
watch: false,
environment: 'happy-dom',
// expect a relative vitest.setup.[js|ts] file where this is initiated
// due to nature of mocks being hoisted during evaluation, consumers need
// to provide individual vitest setup files
setupFiles: ['./vitest.setup'],
include: ['./src/**/*(*.)?{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
name: 'bridge',
exclude,
reporters: ['junit', 'default'],
outputFile: {
junit: './jest-report.xml',
},
coverage: {
all: false,
enabled: true,
provider: 'v8',
exclude: [...exclude, '**/__tests__/*.*'],
reportsDirectory: './coverage',
reporter: [['json', { file: 'coverage-summary.json' }], ['text']],
},
},
});
7 changes: 5 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"baseUrl": "./",
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"moduleResolution": "node",
"downlevelIteration": true,
"jsx": "react-jsx",
"esModuleInterop": true,
"experimentalDecorators": true,
Expand All @@ -25,7 +26,9 @@
"@aelf-web-login/wallet-adapter-portkey-discover": [
"./packages/wallets/portkey-discover/src"
],
"@aelf-web-login/wallet-adapter-night-elf": ["./packages/wallets/night-elf/src"]
"@aelf-web-login/wallet-adapter-night-elf": ["./packages/wallets/night-elf/src"],
"@aelf-web-login/tools/*": ["./packages/tools/*"],
"~/*": ["./*"]
},
"types": ["node", "jest", "@testing-library/jest-dom", "vitest/globals"]
}
Expand Down

0 comments on commit 8788f2e

Please sign in to comment.