Skip to content

Commit

Permalink
chore: add typescript-eslint rules for test files
Browse files Browse the repository at this point in the history
  • Loading branch information
otabek-magic committed Nov 28, 2024
1 parent dd691b6 commit 85ba0fe
Show file tree
Hide file tree
Showing 92 changed files with 140 additions and 246 deletions.
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',
},
},
];
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,4 +1,3 @@
import browserEnv from '@ikscodes/browser-env';
import { JsonRpcError, JsonRpcRequestPayload } from '@magic-sdk/types';
import { JsonRpcResponse } from '../../../../../src/core/json-rpc';

Expand All @@ -19,7 +18,7 @@ function createJsonRcpError(): JsonRpcError {
}

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
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import browserEnv from '@ikscodes/browser-env';
import { MagicExtensionWarning } from '../../../../../src/core/sdk-exceptions';
import { Extension } from '../../../../../src/modules/base-extension';

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

class TestExtension extends Extension<'test'> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import browserEnv from '@ikscodes/browser-env';
import { MagicExtensionWarning } from '../../../../../src/core/sdk-exceptions';
import { Extension } from '../../../../../src/modules/base-extension';

Expand All @@ -10,7 +9,7 @@ test('`MagicSDKWarning.log` logs message to `console.warn`', async () => {
const ext = new TestExtension();
const warning = new MagicExtensionWarning(ext, 'TEST_CODE' as any, 'test message');
const consoleWarningStub = jest.fn();
browserEnv.stub('console.warn', consoleWarningStub);
jest.spyOn(console, 'warn').mockImplementation(consoleWarningStub);
warning.log();

expect(consoleWarningStub.mock.calls[0][0]).toBe('Magic Extension Warning (test): [TEST_CODE] test message');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import browserEnv from '@ikscodes/browser-env';
import { MagicRPCError } from '../../../../../src/core/sdk-exceptions';

const exampleData =
'0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000011555345525f554e52454749535445524544000000000000000000000000000000';

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

test('Initialize `MagicRPCError` with object argument', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import browserEnv from '@ikscodes/browser-env';
import { MagicSDKError } from '../../../../../src/core/sdk-exceptions';

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

test('Instantiate `MagicSDKError`', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import browserEnv from '@ikscodes/browser-env';
import { MagicSDKWarning } from '../../../../../src/core/sdk-exceptions';

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

test('Instantiate `MagicSDKWarning`', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import browserEnv from '@ikscodes/browser-env';
import { MagicSDKWarning } from '../../../../../src/core/sdk-exceptions';

test('`MagicSDKWarning.log` logs message to `console.warn`', async () => {
const warning = new MagicSDKWarning('TEST_CODE' as any, 'test message');
const consoleWarningStub = jest.fn();
browserEnv.stub('console.warn', consoleWarningStub);
jest.spyOn(console, 'warn').mockImplementation(consoleWarningStub);
warning.log();

expect(consoleWarningStub.mock.calls[0][0]).toBe('Magic SDK Warning: [TEST_CODE] test message');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import browserEnv from '@ikscodes/browser-env';
import { mockSDKEnvironmentConstant, restoreSDKEnvironmentConstants } from '../../../mocks';

function warningAssertions(warning: any, expectedCode: string, expectedMessage: string) {
Expand All @@ -11,7 +10,7 @@ function warningAssertions(warning: any, expectedCode: string, expectedMessage:

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import browserEnv from '@ikscodes/browser-env';
import { MAGIC_RELAYER_FULL_URL, TEST_API_KEY } from '../../../constants';
import { createMagicSDKCtor } from '../../../factories';
import { AuthModule } from '../../../../src/modules/auth';
Expand All @@ -7,7 +6,7 @@ import { RPCProviderModule } from '../../../../src/modules/rpc-provider';
import { Extension } from '../../../../src/modules/base-extension';

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

Expand Down Expand Up @@ -65,7 +64,7 @@ test('Initialize `MagicSDK` with custom endpoint', () => {
});

test('Initialize `MagicSDK` when `window.location` is missing', () => {
browserEnv.stub('location', undefined);
jest.spyOn(window, 'location', 'get').mockReturnValue(undefined);

const Ctor = createMagicSDKCtor();
const magic = new Ctor(TEST_API_KEY);
Expand Down Expand Up @@ -324,7 +323,7 @@ test('Initialize `MagicSDK` with incompatible Expo React Native extension (versi
test('Warns upon construction of `MagicSDK` instance if `endpoint` parameter is provided with `react-native` target.', () => {
const Ctor = createMagicSDKCtor({ platform: 'react-native' });
const consoleWarnStub = jest.fn();
browserEnv.stub('console.warn', consoleWarnStub);
jest.spyOn(console, 'warn').mockImplementation(consoleWarnStub);
const { createReactNativeEndpointConfigurationWarning } = require('../../../../src/core/sdk-exceptions');
const expectedWarning = createReactNativeEndpointConfigurationWarning();
new Ctor(TEST_API_KEY, { endpoint: 'https://example.com' } as any);
Expand All @@ -334,7 +333,7 @@ test('Warns upon construction of `MagicSDK` instance if `endpoint` parameter is
test('Does not warn upon construction of `MagicSDK` instance if `endpoint` parameter is omitted with `react-native` target.', () => {
const Ctor = createMagicSDKCtor({ platform: 'react-native' });
const consoleWarnStub = jest.fn();
browserEnv.stub('console.warn', consoleWarnStub);
jest.spyOn(console, 'warn').mockImplementation(consoleWarnStub);
new Ctor(TEST_API_KEY);
expect(consoleWarnStub).not.toBeCalled();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import browserEnv from '@ikscodes/browser-env';
import { TEST_API_KEY } from '../../../constants';
import { ViewController } from '../../../../src/core/view-controller';
import { createMagicSDKCtor } from '../../../factories';
import { SDKBase } from '../../../../src/core/sdk';

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

test('`MagicSDK.overlay` is lazy loaded', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import browserEnv from '@ikscodes/browser-env';
import { ViewController } from '../../../../src/core/view-controller';
import { createMagicSDK } from '../../../factories';

beforeEach(() => {
browserEnv.restore();
jest.resetAllMocks();
(ViewController as any).prototype.waitForReady = jest.fn().mockImplementation(() => Promise.resolve());
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import browserEnv from '@ikscodes/browser-env';
import { ViewController } from '../../../../src/core/view-controller';

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

test('Instantiates `ViewController`', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import browserEnv from '@ikscodes/browser-env';
import { createViewController } from '../../../factories';
import { MSG_TYPES } from '../../../constants';
import { MagicSDKWarning } from '../../../../src/core/sdk-exceptions';

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

test('Receive MAGIC_HIDE_OVERLAY, call `hideOverlay`', (done) => {
test('Receive MAGIC_HIDE_OVERLAY, call `hideOverlay`', done => {
const overlay = createViewController('');
const hideOverlayStub = jest.fn();
overlay.hideOverlay = hideOverlayStub;
Expand All @@ -20,7 +19,7 @@ test('Receive MAGIC_HIDE_OVERLAY, call `hideOverlay`', (done) => {
}, 0);
});

test('Receive MAGIC_SHOW_OVERLAY, call `showOverlay`', (done) => {
test('Receive MAGIC_SHOW_OVERLAY, call `showOverlay`', done => {
const overlay = createViewController('');
const showOverlayStub = jest.fn();
overlay.showOverlay = showOverlayStub;
Expand All @@ -33,7 +32,7 @@ test('Receive MAGIC_SHOW_OVERLAY, call `showOverlay`', (done) => {
}, 0);
});

test('Receive MAGIC_SEND_PRODUCT_ANNOUNCEMENT, log product announcement', (done) => {
test('Receive MAGIC_SEND_PRODUCT_ANNOUNCEMENT, log product announcement', done => {
const overlay = createViewController('');
const productAnnouncement = 'New feature available!';
const logSpy = jest.spyOn(MagicSDKWarning.prototype, 'log');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import browserEnv from '@ikscodes/browser-env';
import { MagicIncomingWindowMessage } from '@magic-sdk/types';
import { createViewController } from '../../../factories';

beforeEach(() => {
browserEnv();
});

/**
* We start with 3 listeners whenever a `ViewController` is created.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import browserEnv from '@ikscodes/browser-env';
import { MagicIncomingWindowMessage, MagicOutgoingWindowMessage, JsonRpcRequestPayload } from '@magic-sdk/types';
import { createViewController, TestViewController } from '../../../factories';
import { JsonRpcResponse } from '../../../../src/core/json-rpc';
Expand Down Expand Up @@ -73,13 +72,13 @@ let FAKE_STORE: any = {};
let viewController: TestViewController;

beforeEach(() => {
jest.resetAllMocks();
jest.restoreAllMocks();
createJwtStub = jest.spyOn(webCryptoUtils, 'createJwt');
getDecryptedDeviceShareStub = jest.spyOn(deviceShareWebCryptoUtils, 'getDecryptedDeviceShare');
clearDeviceSharesStub = jest.spyOn(deviceShareWebCryptoUtils, 'clearDeviceShares');
jest.spyOn(global.console, 'info').mockImplementation(() => {});
browserEnv();
browserEnv.stub('addEventListener', jest.fn());
jest.spyOn(global, 'addEventListener').mockImplementation(jest.fn());
jest.spyOn(storage, 'getItem').mockImplementation((key: string) => FAKE_STORE[key]);
jest.spyOn(storage, 'setItem').mockImplementation(async (key: string, value: any) => {
FAKE_STORE[key] = value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import browserEnv from '@ikscodes/browser-env';
import { createViewController } from '../../../factories';
import { MSG_TYPES } from '../../../constants';

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

test('Receive MAGIC_OVERLAY_READY, resolve `waitForReady` promise', (done) => {
test('Receive MAGIC_OVERLAY_READY, resolve `waitForReady` promise', done => {
const overlay = createViewController('');
const waitForReady = (overlay as any).waitForReady();

Expand All @@ -17,7 +16,7 @@ test('Receive MAGIC_OVERLAY_READY, resolve `waitForReady` promise', (done) => {
window.postMessage({ msgType: MSG_TYPES().MAGIC_OVERLAY_READY }, '*');
});

test('Resolve `waitForReady` promise after timeout', (done) => {
test('Resolve `waitForReady` promise after timeout', done => {
jest.useFakeTimers();
const overlay = createViewController('');
const waitForReady = (overlay as any).waitForReady();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import browserEnv from '@ikscodes/browser-env';
import { MagicPayloadMethod } from '@magic-sdk/types';

import { isPromiEvent } from '../../../../src/util';
import { createMagicSDK, createMagicSDKTestMode } from '../../../factories';

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

Expand All @@ -25,13 +24,13 @@ test('If no parameters are given & platform target is "web", URL search string a
const magic = createMagicSDK({ platform: 'web' });
magic.auth.request = jest.fn();

browserEnv.stub('window.history.replaceState', () => {});
jest.spyOn(window.history, 'replaceState').mockImplementation(() => {});

browserEnv.stub('window.location', {
jest.spyOn(window, 'location', 'get').mockReturnValue({
search: '?magic_credential=asdf',
origin: 'http://example.com',
pathname: '/hello/world',
});
} as any);

await magic.auth.loginWithCredential();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import browserEnv from '@ikscodes/browser-env';
import { MagicPayloadMethod } from '@magic-sdk/types';

import { isPromiEvent } from '../../../../src/util';
import { createMagicSDK, createMagicSDKTestMode } from '../../../factories';

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import browserEnv from '@ikscodes/browser-env';
import { createMagicSDK } from '../../../factories';

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

Expand Down
Loading

0 comments on commit 85ba0fe

Please sign in to comment.