Skip to content

Commit 9f13517

Browse files
authored
test: update tests with types, include tests in linting (#23136)
#22530 broke eslint for `*.test.ts` and `*.test.tsx` files. This PR restores eslint functionality in those files. In order for eslint to work with typescript files the typescript files need to have a corresponding `tsconfig.json`. Instead of having a separate config for our test files I've added the test files to our existing `tsconfig.json`. Many of the tests didn't pass the type checker, so I had to update types in the tests to get everything working. This PR does not restore eslint for `*.stories.ts(x)` files. I think that should be in another PR, as there are 40 stories files that don't pass the type checker.
1 parent 37227c4 commit 9f13517

File tree

39 files changed

+222
-104
lines changed

39 files changed

+222
-104
lines changed

app/scripts/controllers/decrypt-message.test.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { DecryptMessageManager } from '@metamask/message-manager';
2-
import { AbstractMessage } from '@metamask/message-manager/dist/AbstractMessageManager';
1+
import {
2+
DecryptMessageManager,
3+
DecryptMessageParams,
4+
} from '@metamask/message-manager';
35
import { MetaMetricsEventCategory } from '../../../shared/constants/metametrics';
46
import DecryptMessageController, {
57
DecryptMessageControllerMessenger,
@@ -8,15 +10,17 @@ import DecryptMessageController, {
810
} from './decrypt-message';
911

1012
const messageIdMock = '12345';
13+
const messageDataMock =
14+
'0x7b2276657273696f6e223a227832353531392d7873616c736132302d706f6c7931333035222c226e6f6e6365223a226b45586143524c3045646142766f77756e35675979357175784a4a6967304548222c22657068656d5075626c69634b6579223a224863334636506d314734385a567955424763365866537839682b77784b6958587238456a51434253466e553d222c2263697068657274657874223a22546a41556b68554a5968656e7a2f655a6e57454a2b31456c7861354f77765939613830507a62746c7a7a48746934634175525941227d';
1115
const messageMock = {
1216
metamaskId: messageIdMock,
1317
time: 123,
1418
status: 'unapproved',
1519
type: 'testType',
1620
rawSig: undefined,
17-
} as any as AbstractMessage;
18-
const messageDataMock =
19-
'0x7b2276657273696f6e223a227832353531392d7873616c736132302d706f6c7931333035222c226e6f6e6365223a226b45586143524c3045646142766f77756e35675979357175784a4a6967304548222c22657068656d5075626c69634b6579223a224863334636506d314734385a567955424763365866537839682b77784b6958587238456a51434253466e553d222c2263697068657274657874223a22546a41556b68554a5968656e7a2f655a6e57454a2b31456c7861354f77765939613830507a62746c7a7a48746934634175525941227d';
21+
data: messageDataMock,
22+
from: '0x0',
23+
} as DecryptMessageParams & { metamaskId: string };
2024

2125
const mockExtState = {};
2226

@@ -56,7 +60,14 @@ const createDecryptMessageManagerMock = <T>() =>
5660
} as any as jest.Mocked<T>);
5761

5862
describe('DecryptMessageController', () => {
59-
let decryptMessageController: DecryptMessageController;
63+
class MockDecryptMessageController extends DecryptMessageController {
64+
// update is protected, so we expose it for typechecking here
65+
public update(callback: Parameters<DecryptMessageController['update']>[0]) {
66+
return super.update(callback);
67+
}
68+
}
69+
70+
let decryptMessageController: MockDecryptMessageController;
6071

6172
const decryptMessageManagerConstructorMock =
6273
DecryptMessageManager as jest.MockedClass<typeof DecryptMessageManager>;
@@ -88,7 +99,7 @@ describe('DecryptMessageController', () => {
8899
decryptMessageManagerMock,
89100
);
90101

91-
decryptMessageController = new DecryptMessageController({
102+
decryptMessageController = new MockDecryptMessageController({
92103
getState: getStateMock as any,
93104
keyringController: keyringControllerMock as any,
94105
messenger: messengerMock as any,
@@ -118,7 +129,10 @@ describe('DecryptMessageController', () => {
118129
expect(decryptMessageManagerMock.update).toBeCalledTimes(1);
119130
});
120131
it('should add unapproved messages', async () => {
121-
await decryptMessageController.newRequestDecryptMessage(messageMock);
132+
await decryptMessageController.newRequestDecryptMessage(
133+
messageMock,
134+
undefined as any,
135+
);
122136

123137
expect(decryptMessageManagerMock.addUnapprovedMessageAsync).toBeCalledTimes(
124138
1,
@@ -206,7 +220,7 @@ describe('DecryptMessageController', () => {
206220
const messageToDecrypt = {
207221
...messageMock,
208222
data: messageDataMock,
209-
};
223+
} as any;
210224
decryptMessageManagerMock.getMessage.mockReturnValue(messageToDecrypt);
211225
mockMessengerAction(
212226
'KeyringController:decryptMessage',
@@ -238,7 +252,9 @@ describe('DecryptMessageController', () => {
238252
});
239253

240254
it('should be able to cancel decrypt message', async () => {
241-
decryptMessageManagerMock.rejectMessage.mockResolvedValue(messageMock);
255+
decryptMessageManagerMock.rejectMessage.mockResolvedValue(
256+
messageMock as never,
257+
);
242258
getStateMock.mockReturnValue(mockExtState);
243259

244260
const result = await decryptMessageController.cancelDecryptMessage(
@@ -255,7 +271,7 @@ describe('DecryptMessageController', () => {
255271
it('should be able to reject all unapproved messages', async () => {
256272
decryptMessageManagerMock.getUnapprovedMessages.mockReturnValue({
257273
[messageIdMock]: messageMock,
258-
});
274+
} as any);
259275

260276
await decryptMessageController.rejectUnapproved('reason to cancel');
261277

app/scripts/lib/SnapsNameProvider.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { NameType } from '@metamask/name-controller';
2+
// @ts-expect-error see: https://github.com/MetaMask/snaps/pull/2174
23
import { HandlerType } from '@metamask/snaps-utils';
34
import {
45
GetAllSnaps,

app/scripts/lib/notification-manager.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2+
// @ts-nocheck types are very broken
13
import browser from 'webextension-polyfill';
24
import NotificationManager from './notification-manager';
35

@@ -44,7 +46,7 @@ describe('Notification Manager', () => {
4446
let notificationManager: NotificationManager,
4547
setCurrentPopupIdSpy: (a: number) => void,
4648
focusWindowSpy: () => void,
47-
currentPopupId: number;
49+
currentPopupId: number | undefined;
4850

4951
beforeEach(() => {
5052
notificationManager = new NotificationManager();
@@ -65,7 +67,8 @@ describe('Notification Manager', () => {
6567
browser.windows.getAll.mockReturnValue([]);
6668
browser.windows.create.mockReturnValue(newPopupWindow);
6769
currentPopupId = undefined;
68-
await notificationManager.showPopup(setCurrentPopupIdSpy, currentPopupId);
70+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
71+
await notificationManager.showPopup(setCurrentPopupIdSpy, currentPopupId!);
6972
expect(setCurrentPopupIdSpy).toHaveBeenCalledTimes(1);
7073
expect(setCurrentPopupIdSpy).toHaveBeenCalledWith(newPopupWindow.id);
7174
});
@@ -82,7 +85,8 @@ describe('Notification Manager', () => {
8285
width: 120, // make sure this is smalled than NOTIFICATION_WIDTH
8386
});
8487
currentPopupId = undefined;
85-
await notificationManager.showPopup(setCurrentPopupIdSpy, currentPopupId);
88+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
89+
await notificationManager.showPopup(setCurrentPopupIdSpy, currentPopupId!);
8690
expect(createSpy).toHaveBeenCalledTimes(1);
8791
expect(createSpy).toHaveBeenCalledWith({
8892
height: 620,

app/scripts/lib/snap-keyring/metrics.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getSnapAndHardwareInfoForMetrics } from './metrics';
33
describe('getSnapAndHardwareInfoForMetrics', () => {
44
let getAccountType: jest.Mock;
55
let getDeviceModel: jest.Mock;
6-
let messenger;
6+
let messenger: any;
77

88
beforeEach(() => {
99
getAccountType = jest.fn();

app/scripts/lib/transaction/metrics.test.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Provider } from '@metamask/network-controller';
22
import {
3+
TransactionMeta,
34
TransactionStatus,
45
TransactionType,
56
} from '@metamask/transaction-controller';
@@ -75,15 +76,15 @@ describe('Transaction metrics', () => {
7576
let fromAccount,
7677
mockChainId,
7778
mockNetworkId,
78-
mockTransactionMeta,
79-
mockTransactionMetaWithBlockaid,
80-
expectedProperties,
81-
expectedSensitiveProperties,
82-
mockActionId;
79+
mockTransactionMeta: TransactionMeta,
80+
mockTransactionMetaWithBlockaid: any,
81+
expectedProperties: any,
82+
expectedSensitiveProperties: any,
83+
mockActionId: any;
8384

8485
beforeEach(() => {
8586
fromAccount = getTestAccounts()[0];
86-
mockChainId = '0x5';
87+
mockChainId = '0x5' as const;
8788
mockNetworkId = '5';
8889
mockActionId = '2';
8990
mockTransactionMeta = {
@@ -188,7 +189,10 @@ describe('Transaction metrics', () => {
188189
});
189190

190191
it('should create event fragment when simulation failed', async () => {
191-
mockTransactionMeta.simulationFails = true;
192+
mockTransactionMeta.simulationFails = {
193+
reason: 'test',
194+
debug: {},
195+
};
192196

193197
await handleTransactionAdded(mockTransactionMetricsRequest, {
194198
transactionMeta: mockTransactionMeta as any,

app/scripts/lib/transaction/util.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ describe('Transaction Utils', () => {
220220

221221
expect(completed).toBe(false);
222222

223-
resultResolve(TRANSACTION_META_MOCK.hash);
223+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
224+
resultResolve!(TRANSACTION_META_MOCK.hash);
224225

225226
await flushPromises();
226227

@@ -307,7 +308,8 @@ describe('Transaction Utils', () => {
307308

308309
expect(completed).toBe(false);
309310

310-
transactionHashResolve(TRANSACTION_META_MOCK.hash);
311+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
312+
transactionHashResolve!(TRANSACTION_META_MOCK.hash);
311313

312314
await flushPromises();
313315

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@
455455
"@types/react-redux": "^7.1.25",
456456
"@types/react-router-dom": "^5.3.3",
457457
"@types/readable-stream": "^4.0.4",
458+
"@types/redux-mock-store": "1.0.6",
458459
"@types/remote-redux-devtools": "^0.5.5",
459460
"@types/sass": "^1.43.1",
460461
"@types/selenium-webdriver": "^4.1.19",

tsconfig.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323
"useUnknownInCatchVariables": true
2424
},
2525
"exclude": [
26-
"**/*.test.ts", // its pretty important to
27-
"**/*.test.tsx", // use accurate types in
28-
"**/*.stories.tsx", // tests and we don't
29-
"**/*.stories.ts" // even come close.
26+
// don't typecheck stories, as they don't yet pass the type checker.
27+
"**/*.stories.tsx",
28+
"**/*.stories.ts"
3029
],
3130
// this should match our node version in .nvmrc
3231
"extends": "@tsconfig/node20/tsconfig.json",

ui/components/app/configure-snap-popup/configure-snap-popup.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import ConfigureSnapPopup, {
1111
const mockOnClose = jest.fn();
1212
const mockStore = configureMockStore([])(mockState);
1313
describe('ConfigureSnapPopup', () => {
14-
global.platform = { openTab: jest.fn() };
14+
global.platform = { openTab: jest.fn(), closeCurrentWindow: jest.fn() };
1515

1616
it('should show configure popup title and description', async () => {
1717
const { getByText } = renderWithProvider(

ui/components/app/snaps/keyring-snap-removal-warning/keyring-snap-removal-warning.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { waitFor, fireEvent } from '@testing-library/react';
33
import configureMockStore from 'redux-mock-store';
4+
// @ts-expect-error see: https://github.com/MetaMask/snaps/pull/2174
45
import { Snap } from '@metamask/snaps-utils';
56
import mockStore from '../../../../../test/data/mock-state.json';
67
import { renderWithProvider } from '../../../../../test/jest';
@@ -40,7 +41,7 @@ const defaultArgs = {
4041
};
4142

4243
describe('Keyring Snap Remove Warning', () => {
43-
let store;
44+
let store: any;
4445
beforeAll(() => {
4546
store = configureMockStore()(mockStore);
4647
});
@@ -102,7 +103,7 @@ describe('Keyring Snap Remove Warning', () => {
102103
});
103104

104105
it('opens block explorer for account', async () => {
105-
global.platform = { openTab: jest.fn() };
106+
global.platform = { openTab: jest.fn(), closeCurrentWindow: jest.fn() };
106107
const { getByText, getAllByTestId } = renderWithProvider(
107108
<KeyringSnapRemovalWarning {...defaultArgs} />,
108109
store,

0 commit comments

Comments
 (0)