Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove @ikscodes/browser-env #842

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,13 @@ export default [
},
},
},
{
files: ['**/*.spec.*'],

rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/naming-convention': 'off',
'@typescript-eslint/use-unknown-in-catch-callback-variable': 'off',
},
},
];
14 changes: 9 additions & 5 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const config: Config.InitialOptions = {
collectCoverageFrom: ['./src/**/*.{ts,tsx,}'],
collectCoverage: true,
testTimeout: 30000, // 30s
testEnvironment: 'jsdom',
coverageThreshold: {
global: {
lines: 99,
Expand All @@ -16,11 +17,14 @@ const config: Config.InitialOptions = {
},
},
setupFilesAfterEnv: ['./test/setup.ts'],
globals: {
'ts-jest': {
tsconfig: './test/tsconfig.json',
isolatedModules: true,
},
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
tsconfig: './test/tsconfig.json',
isolatedModules: true,
},
],
},
};

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
"@eslint/compat": "^1.2.2",
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.14.0",
"@ikscodes/browser-env": "~0.3.1",
"@istanbuljs/nyc-config-typescript": "~0.1.3",
"@types/fs-extra": "^9.0.13",
"@types/inquirer": "^8.1.1",
"@types/is-ci": "^3.0.0",
"@types/jest": "^27.0.0",
"@types/jest": "^29.5.11",
"@types/jsdom": "~12.2.4",
"@types/lodash": "^4.14.172",
"@types/react": "^18.0.26",
Expand Down Expand Up @@ -53,7 +52,8 @@
"husky": "^7.0.1",
"inquirer": "^8.1.2",
"is-ci": "^3.0.0",
"jest": "^27.0.6",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"lerna": "8.0.2",
"lint-staged": "^10.0.7",
"lodash": "^4.17.21",
Expand All @@ -69,7 +69,7 @@
"regenerator-runtime": "0.13.9",
"replace-in-file": "^6.1.0",
"rimraf": "~3.0.2",
"ts-jest": "^27.0.4",
"ts-jest": "^29.1.4",
"ts-node": "^10.2.0",
"tsc-watch": "^4.2.9",
"tslib": "^2.3.1",
Expand Down
3 changes: 0 additions & 3 deletions packages/@magic-ext/aptos/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// NOTE: This module is automatically included at the top of each test file.
import browserEnv from '@ikscodes/browser-env';
import { mockConsole } from '../../../../scripts/utils/mock-console';

browserEnv();

beforeEach(() => {
mockConsole();
});
8 changes: 6 additions & 2 deletions packages/@magic-ext/aptos/test/spec/aptos-extension.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import browserEnv from '@ikscodes/browser-env';
import { BCS, TxnBuilderTypes } from 'aptos';
import { createMagicSDKWithExtension } from '../../../../@magic-sdk/provider/test/factories';
import { AptosExtension } from '../../src';
import { AptosPayloadMethod } from '../../src/type';
import { TextEncoder, TextDecoder } from 'util';

const APTOS_NODE_URL = 'https://fullnode.testnet.aptoslabs.com';

Expand Down Expand Up @@ -31,8 +31,12 @@ const MESSAGE_PAYLOAD = {
nonce: 'random-nonce',
};

beforeAll(() => {
Object.assign(global, { TextEncoder, TextDecoder });
})

beforeEach(() => {
browserEnv.restore();
jest.resetAllMocks();
});

test('Construct GetAccount request with `aptos_getAccount`', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import browserEnv from '@ikscodes/browser-env';
import { BCS, TxnBuilderTypes } from 'aptos';
import { createMagicSDKWithExtension } from '../../../../@magic-sdk/provider/test/factories';
import { AptosExtension, MagicAptosWallet } from '../../src';
Expand Down Expand Up @@ -33,7 +32,7 @@ const MOCK_ACCOUTN_INFO = {
};

beforeEach(() => {
browserEnv.restore();
jest.resetAllMocks();
});

test('Call connect()', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/@magic-ext/aptos/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "../../../../tsconfig.settings.test.json",
"extends": "../../../../tsconfig.settings.test.json"
}
2 changes: 0 additions & 2 deletions packages/@magic-ext/gdkms/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// NOTE: This module is automatically included at the top of each test file.
import browserEnv from '@ikscodes/browser-env';
import { Crypto } from '@peculiar/webcrypto';
import { mockConsole } from '../../../../scripts/utils/mock-console';

browserEnv();
(window as any).crypto = new Crypto();

beforeEach(() => {
Expand Down
3 changes: 1 addition & 2 deletions packages/@magic-ext/gdkms/test/spec/gdkms.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import browserEnv from '@ikscodes/browser-env';
import { MagicPayloadMethod } from '@magic-sdk/types';
import { createMagicSDKWithExtension } from '../../../../@magic-sdk/provider/test/factories';
import { GDKMSExtension } from '../../src';

beforeEach(() => {
browserEnv.restore();
jest.resetAllMocks();
});

test('Construct Encrypt Request with `magic_auth_encrypt_v1`', async () => {
Expand Down
3 changes: 0 additions & 3 deletions packages/@magic-ext/harmony/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// NOTE: This module is automatically included at the top of each test file.
import browserEnv from '@ikscodes/browser-env';
import { mockConsole } from '../../../../scripts/utils/mock-console';

browserEnv();

beforeEach(() => {
mockConsole();
});
3 changes: 1 addition & 2 deletions packages/@magic-ext/harmony/test/spec/harmony.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import browserEnv from '@ikscodes/browser-env';
import { HarmonyPayloadMethod } from '../../src/types';
import { createMagicSDKWithExtension } from '../../../../@magic-sdk/provider/test/factories';
import { HarmonyExtension } from '../../src';

beforeEach(() => {
browserEnv.restore();
jest.resetAllMocks();
});

test('Sends params as payload', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/@magic-ext/kadena/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class KadenaExtension extends Extension.Internal<'kadena'> {
}

public signTransaction(hash: string): Promise<SignatureWithPublicKey> {
return this.request(this.utils.createJsonRpcRequestPayload(KadenaPayloadMethod.KadenaSignTransaction, [{ hash }]));
return this.request<SignatureWithPublicKey>(this.utils.createJsonRpcRequestPayload(KadenaPayloadMethod.KadenaSignTransaction, [{ hash }]));
}

public async signTransactionWithSpireKey(
Expand Down
3 changes: 0 additions & 3 deletions packages/@magic-ext/kadena/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// NOTE: This module is automatically included at the top of each test file.
import browserEnv from '@ikscodes/browser-env';
import { mockConsole } from '../../../../scripts/utils/mock-console';

browserEnv();

beforeEach(() => {
mockConsole();
});
7 changes: 1 addition & 6 deletions packages/@magic-ext/kadena/test/spec/kadena.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import browserEnv from '@ikscodes/browser-env';
import { KadenaPayloadMethod } from '../../src/types';
import { createMagicSDKWithExtension } from '../../../../@magic-sdk/provider/test/factories';
import { KadenaExtension } from '../../src';

beforeEach(() => {
browserEnv.restore();
jest.resetAllMocks();
});

test('signTransaction - Sends params as payload', () => {
Expand All @@ -13,7 +12,6 @@ test('signTransaction - Sends params as payload', () => {
rpcUrl: '',
chainId: '1',
networkId: 'testnet04',
network: 'testnet',
createAccountsOnChain: true,
}),
]);
Expand All @@ -34,7 +32,6 @@ test('signTransactionWithSpireKey - Sends params as payload', () => {
rpcUrl: '',
chainId: '1',
networkId: 'testnet04',
network: 'testnet',
createAccountsOnChain: true,
}),
]);
Expand All @@ -59,7 +56,6 @@ test('Generate JSON RPC request payload with method `kda_loginWithSpireKey`', ()
rpcUrl: '',
chainId: '1',
networkId: 'testnet04',
network: 'testnet',
createAccountsOnChain: true,
}),
]);
Expand All @@ -77,7 +73,6 @@ test('Generate JSON RPC request payload with method `kda_getUserInfo`', () => {
rpcUrl: '',
chainId: '1',
networkId: 'testnet04',
network: 'testnet',
createAccountsOnChain: true,
}),
]);
Expand Down
3 changes: 0 additions & 3 deletions packages/@magic-ext/sui/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// NOTE: This module is automatically included at the top of each test file.
import browserEnv from '@ikscodes/browser-env';
import { mockConsole } from '../../../../scripts/utils/mock-console';

browserEnv();

beforeEach(() => {
mockConsole();
});
3 changes: 1 addition & 2 deletions packages/@magic-ext/sui/test/spec/sui.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import browserEnv from '@ikscodes/browser-env';
import { SuiExtension } from '../../src/index';
import { SuiPayloadMethod } from '../../src/types';
import { createMagicSDKWithExtension } from '../../../../@magic-sdk/provider/test/factories';

beforeEach(() => {
browserEnv.restore();
jest.resetAllMocks();
});

test('Sends params as payload', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/@magic-sdk/provider/test/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function createMagicSDKTestMode(environment: { [P in keyof SDKEnvironment
return new Ctor(TEST_API_KEY, { testMode: true });
}

export function createMagicSDKWithExtension(environment: { [P in keyof SDKEnvironment]?: any } = {}, extensions = []) {
export function createMagicSDKWithExtension(environment: { [P in keyof SDKEnvironment]?: any } = {}, extensions:unknown[] = []) {
const Ctor = createMagicSDKCtor(environment);
return new Ctor(TEST_API_KEY, { extensions });
}
12 changes: 10 additions & 2 deletions packages/@magic-sdk/provider/test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ export function restoreSDKEnvironmentConstants() {
jest.unmock('../src/core/sdk-environment');
}

export function mockLocalForage(FAKE_STORE = {}) {
jest.spyOn(storage, 'getItem').mockImplementation((key: string) => FAKE_STORE[key]);
export function mockLocalForage(FAKE_STORE: Record<string, unknown> = {}) {
jest.spyOn(storage, 'getItem').mockImplementation((key: string, callback?: (err: unknown, value: unknown) => void) => {
const value = FAKE_STORE[key];

if (callback) {
callback(null, value);
}

return Promise.resolve(value);
});
jest.spyOn(storage, 'setItem').mockImplementation(async (key: string, value: any) => {
FAKE_STORE[key] = value;
});
Expand Down
2 changes: 0 additions & 2 deletions packages/@magic-sdk/provider/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// NOTE: This module is automatically included at the top of each test file.
import browserEnv from '@ikscodes/browser-env';
import { Crypto } from '@peculiar/webcrypto';
import { mockConsole } from '../../../../scripts/utils/mock-console';

browserEnv();
(window as any).crypto = new Crypto();

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import browserEnv from '@ikscodes/browser-env';
import { JsonRpcError, JsonRpcRequestPayload } from '@magic-sdk/types';
import { JsonRpcError, JsonRpcRequestPayload, RPCErrorCode } from '@magic-sdk/types';
import { JsonRpcResponse } from '../../../../../src/core/json-rpc';

function createSourcePayload(): JsonRpcRequestPayload {
Expand All @@ -14,12 +13,12 @@ function createSourcePayload(): JsonRpcRequestPayload {
function createJsonRcpError(): JsonRpcError {
return {
message: 'hello wolrd',
code: 1,
code: RPCErrorCode.InternalError,
};
}

beforeEach(() => {
browserEnv.restore();
jest.restoreAllMocks();
});

test('Add a formatted error to the response with `JsonRpcError` object as argument', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import browserEnv from '@ikscodes/browser-env';
import { JsonRpcRequestPayload } from '@magic-sdk/types';
import { JsonRpcResponse } from '../../../../../src/core/json-rpc';

Expand All @@ -12,7 +11,7 @@ function createSourcePayload(): JsonRpcRequestPayload {
}

beforeEach(() => {
browserEnv.restore();
jest.resetAllMocks();
});

test('Applies a result to the response.', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import browserEnv from '@ikscodes/browser-env';
import { JsonRpcResponse } from '../../../../../src/core/json-rpc';

beforeEach(() => {
browserEnv.restore();
jest.resetAllMocks();
});

test('Initialize JsonRpcResponse instance if argument is `instanceof` JsonRpcReponse', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import browserEnv from '@ikscodes/browser-env';
import { Extension } from '../../../../src/modules/base-extension';
import { mockSDKEnvironmentConstant, restoreSDKEnvironmentConstants } from '../../../mocks';

Expand All @@ -12,7 +11,7 @@ function errorAssertions(error: any, expectedCode: string, expectedMessage: stri

beforeEach(() => {
jest.resetModules();
browserEnv.restore();
jest.resetAllMocks();
restoreSDKEnvironmentConstants();
});

Expand Down Expand Up @@ -117,6 +116,7 @@ class NoopExtSupportingWeb extends Extension<'noop'> {
name = 'noop' as const;
compat = {
'magic-sdk': '>1.0.0',
'@magic-sdk/react-native': false,
'@magic-sdk/react-native-bare': false,
'@magic-sdk/react-native-expo': false,
};
Expand All @@ -127,6 +127,7 @@ class NoopExtSupportingBareReactNative extends Extension<'noop'> {
name = 'noop' as const;
compat = {
'magic-sdk': false,
'@magic-sdk/react-native': false,
'@magic-sdk/react-native-bare': '>1.0.0',
'@magic-sdk/react-native-expo': false,
};
Expand All @@ -137,6 +138,7 @@ class NoopExtSupportingExpoReactNative extends Extension<'noop'> {
name = 'noop' as const;
compat = {
'magic-sdk': false,
'@magic-sdk/react-native': false,
'@magic-sdk/react-native-bare': false,
'@magic-sdk/react-native-expo': '>1.0.0',
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import browserEnv from '@ikscodes/browser-env';
import { MagicExtensionError } from '../../../../../src/core/sdk-exceptions';
import { Extension } from '../../../../../src/modules/base-extension';

beforeEach(() => {
browserEnv();
jest.resetAllMocks();
});

class TestExtension extends Extension<'test'> {
Expand Down
Loading