Skip to content

Commit

Permalink
add messages to empty tothrow calls (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
astarinmymind committed Mar 20, 2021
1 parent 7f21890 commit f6699b1
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 42 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ module.exports = {
'jest/no-test-return-statement': 'off',
'jest/no-try-expect': 'off',
'jest/prefer-strict-equal': 'off',
'jest/require-to-throw-message': 'off',
'jest/valid-expect-in-promise': 'off',
},
settings: {
Expand Down
2 changes: 1 addition & 1 deletion src/BaseController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ describe('BaseController', () => {
const controller = new TestController();
expect(() => {
new ComposableController([controller]);
}).toThrow();
}).toThrow('BaseController must be composed with Foo.');
});
});
8 changes: 5 additions & 3 deletions src/BaseControllerV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('BaseController', () => {

expect(() => {
controller.state = { count: 1 };
}).toThrow();
}).toThrow("Controller state cannot be directly mutated; use 'update' method instead.");
});

it('should allow updating state by modifying draft', () => {
Expand Down Expand Up @@ -115,7 +115,9 @@ describe('BaseController', () => {
draft.count += 1;
return { count: 10 };
});
}).toThrow();
}).toThrow(
'[Immer] An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.',
);
});

it('should inform subscribers of state changes', () => {
Expand Down Expand Up @@ -213,7 +215,7 @@ describe('BaseController', () => {

expect(() => {
controllerMessenger.unsubscribe('CountController:stateChange', listener1);
}).toThrow();
}).toThrow("Subscription not found for event: 'CountController:stateChange'");
});

it('should no longer update subscribers after being destroyed', () => {
Expand Down
20 changes: 12 additions & 8 deletions src/ControllerMessenger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('ControllerMessenger', () => {

expect(() => {
controllerMessenger.registerActionHandler('ping', () => undefined);
}).toThrow();
}).toThrow('A handler for ping has already been registered');
});

it('should throw when calling unregistered action', () => {
Expand All @@ -95,7 +95,7 @@ describe('ControllerMessenger', () => {

expect(() => {
controllerMessenger.call('ping');
}).toThrow();
}).toThrow('A handler for ping has not been registered');
});

it('should throw when calling an action that has been unregistered', () => {
Expand All @@ -104,7 +104,7 @@ describe('ControllerMessenger', () => {

expect(() => {
controllerMessenger.call('ping');
}).toThrow();
}).toThrow('A handler for ping has not been registered');

let pingCount = 0;
controllerMessenger.registerActionHandler('ping', () => {
Expand All @@ -115,7 +115,7 @@ describe('ControllerMessenger', () => {

expect(() => {
controllerMessenger.call('ping');
}).toThrow();
}).toThrow('A handler for ping has not been registered');
expect(pingCount).toEqual(0);
});

Expand All @@ -125,7 +125,7 @@ describe('ControllerMessenger', () => {

expect(() => {
controllerMessenger.call('ping');
}).toThrow();
}).toThrow('A handler for ping has not been registered');

let pingCount = 0;
controllerMessenger.registerActionHandler('ping', () => {
Expand All @@ -136,7 +136,7 @@ describe('ControllerMessenger', () => {

expect(() => {
controllerMessenger.call('ping');
}).toThrow();
}).toThrow('A handler for ping has not been registered');
expect(pingCount).toEqual(0);
});

Expand Down Expand Up @@ -240,7 +240,9 @@ describe('ControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();

const handler = sinon.stub();
expect(() => controllerMessenger.unsubscribe('message', handler)).toThrow();
expect(() => controllerMessenger.unsubscribe('message', handler)).toThrow(
"Subscription not found for event: 'message'",
);
});

it('should throw when unsubscribing a handler that is not subscribed', () => {
Expand All @@ -251,7 +253,9 @@ describe('ControllerMessenger', () => {
const handler2 = sinon.stub();
controllerMessenger.subscribe('message', handler1);

expect(() => controllerMessenger.unsubscribe('message', handler2)).toThrow();
expect(() => controllerMessenger.unsubscribe('message', handler2)).toThrow(
"Subscription not found for event: 'message'",
);
});

it('should not call subscriber after clearing event subscriptions', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/assets/AccountTrackerController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('AccountTrackerController', () => {

it('should throw when provider property is accessed', () => {
const controller = new AccountTrackerController();
expect(() => console.log(controller.provider)).toThrow();
expect(() => console.log(controller.provider)).toThrow('Property only used for setting');
});

it('should get real balance', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/assets/AssetsContractController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('AssetsContractController', () => {
});

it('should throw when provider property is accessed', () => {
expect(() => console.log(assetsContract.provider)).toThrow();
expect(() => console.log(assetsContract.provider)).toThrow('Property only used for setting');
});

it('should determine if contract supports interface correctly', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/assets/CurrencyRateController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ describe('CurrencyRateController', () => {
it('should throw when currentCurrency property is accessed', () => {
const fetchExchangeRateStub = stub();
const controller = new CurrencyRateController({}, {}, fetchExchangeRateStub);
expect(() => console.log(controller.currentCurrency)).toThrow();
expect(() => console.log(controller.currentCurrency)).toThrow('Property only used for setting');
});

it('should throw when nativeCurrency property is accessed', () => {
const fetchExchangeRateStub = stub();
const controller = new CurrencyRateController({}, {}, fetchExchangeRateStub);
expect(() => console.log(controller.nativeCurrency)).toThrow();
expect(() => console.log(controller.nativeCurrency)).toThrow('Property only used for setting');
});

it('should poll and update rate in the right interval', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/assets/TokenRatesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('TokenRatesController', () => {

it('should throw when tokens property is accessed', () => {
const controller = new TokenRatesController();
expect(() => console.log(controller.tokens)).toThrow();
expect(() => console.log(controller.tokens)).toThrow('Property only used for setting');
});

it('should poll and update rate in the right interval', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/keyring/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ describe('KeyringController', () => {
it('should export seed phrase', () => {
const seed = keyringController.exportSeedPhrase(password);
expect(seed).not.toBe('');
expect(() => keyringController.exportSeedPhrase('')).toThrow();
expect(() => keyringController.exportSeedPhrase('')).toThrow('Invalid password');
});

it('should export account', async () => {
const account = initialState.keyrings[0].accounts[0];
const newPrivateKey = await keyringController.exportAccount(password, account);
expect(newPrivateKey).not.toBe('');
expect(() => keyringController.exportAccount('', account)).toThrow();
expect(() => keyringController.exportAccount('', account)).toThrow('Invalid password');
});

it('should get accounts', async () => {
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('KeyringController', () => {
const somePassword = 'holachao123';
await expect(
keyringController.importAccountWithStrategy('junk' as AccountImportStrategy, [input, somePassword]),
).rejects.toThrow();
).rejects.toThrow("Unexpected import strategy: 'junk'");
});

it('should import account with strategy json wrong password', async () => {
Expand Down Expand Up @@ -188,7 +188,7 @@ describe('KeyringController', () => {
const account = initialState.keyrings[0].accounts[0];
await expect(
keyringController.signTypedMessage({ data: typedMsgParams, from: account }, 'junk' as SignTypedDataVersion),
).rejects.toThrow();
).rejects.toThrow("Keyring Controller signTypedMessage: Error: Unexpected signTypedMessage version: 'junk'");
});

it('should sign typed message V1', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/network/NetworkController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('NetworkController', () => {

it('should throw when providerConfig property is accessed', () => {
const controller = new NetworkController();
expect(() => console.log(controller.providerConfig)).toThrow();
expect(() => console.log(controller.providerConfig)).toThrow('Property only used for setting');
});

it('should create a provider instance for default infura network', () => {
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('NetworkController', () => {

it('should throw when setting an unrecognized provider type', () => {
const controller = new NetworkController();
expect(() => controller.setProviderType('junk' as NetworkType)).toThrow();
expect(() => controller.setProviderType('junk' as NetworkType)).toThrow("Unrecognized network type: 'junk'");
});

it('should verify the network on an error', async () => {
Expand Down
8 changes: 5 additions & 3 deletions src/third-party/EnsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,25 @@ describe('EnsController', () => {
const controller = new EnsController();
expect(() => {
controller.set('a', name1, address1);
}).toThrow();
}).toThrow(
'Invalid ENS entry: { chainId:a, ensName:foobarb.eth, address:0x32Be343B94f860124dC4fEe278FDCBD38C102D88}',
);
expect(controller.state).toEqual({ ensEntries: {} });
});

it('should throw on attempt to set invalid ENS entry: ENS name', () => {
const controller = new EnsController();
expect(() => {
controller.set('1', 'foo.eth', address1);
}).toThrow();
}).toThrow('Invalid ENS name: foo.eth');
expect(controller.state).toEqual({ ensEntries: {} });
});

it('should throw on attempt to set invalid ENS entry: address', () => {
const controller = new EnsController();
expect(() => {
controller.set('1', name1, 'foo');
}).toThrow();
}).toThrow('Invalid ENS entry: { chainId:1, ensName:foobarb.eth, address:foo}');
expect(controller.state).toEqual({ ensEntries: {} });
});

Expand Down
36 changes: 21 additions & 15 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,21 @@ describe('util', () => {

describe('validateTransaction', () => {
it('should throw if no from address', () => {
expect(() => util.validateTransaction({} as any)).toThrow();
expect(() => util.validateTransaction({} as any)).toThrow(
'Invalid "from" address: undefined must be a valid string.',
);
});

it('should throw if non-string from address', () => {
expect(() => util.validateTransaction({ from: 1337 } as any)).toThrow();
expect(() => util.validateTransaction({ from: 1337 } as any)).toThrow(
'Invalid "from" address: 1337 must be a valid string.',
);
});

it('should throw if invalid from address', () => {
expect(() => util.validateTransaction({ from: '1337' } as any)).toThrow();
expect(() => util.validateTransaction({ from: '1337' } as any)).toThrow(
'Invalid "from" address: 1337 must be a valid string.',
);
});

it('should throw if no data', () => {
Expand All @@ -194,12 +200,12 @@ describe('util', () => {
from: '0x3244e191f1b4903970224322180f1fbbc415696b',
to: '0x',
} as any),
).toThrow();
).toThrow('Invalid "to" address: 0x must be a valid string.');
expect(() =>
util.validateTransaction({
from: '0x3244e191f1b4903970224322180f1fbbc415696b',
} as any),
).toThrow();
).toThrow('Invalid "to" address: undefined must be a valid string.');
});

it('should delete data', () => {
Expand All @@ -218,7 +224,7 @@ describe('util', () => {
from: '0x3244e191f1b4903970224322180f1fbbc415696b',
to: '1337',
} as any),
).toThrow();
).toThrow('Invalid "to" address: 1337 must be a valid string.');
});

it('should throw if value is invalid', () => {
Expand All @@ -228,28 +234,28 @@ describe('util', () => {
to: '0x3244e191f1b4903970224322180f1fbbc415696b',
value: '133-7',
} as any),
).toThrow();
).toThrow('Invalid "value": 133-7 is not a positive number.');
expect(() =>
util.validateTransaction({
from: '0x3244e191f1b4903970224322180f1fbbc415696b',
to: '0x3244e191f1b4903970224322180f1fbbc415696b',
value: '133.7',
} as any),
).toThrow();
).toThrow('Invalid "value": 133.7 number must be denominated in wei.');
expect(() =>
util.validateTransaction({
from: '0x3244e191f1b4903970224322180f1fbbc415696b',
to: '0x3244e191f1b4903970224322180f1fbbc415696b',
value: 'hello',
} as any),
).toThrow();
).toThrow('Invalid "value": hello number must be a valid number.');
expect(() =>
util.validateTransaction({
from: '0x3244e191f1b4903970224322180f1fbbc415696b',
to: '0x3244e191f1b4903970224322180f1fbbc415696b',
value: 'one million dollar$',
} as any),
).toThrow();
).toThrow('Invalid "value": one million dollar$ number must be a valid number.');
expect(() =>
util.validateTransaction({
from: '0x3244e191f1b4903970224322180f1fbbc415696b',
Expand Down Expand Up @@ -280,7 +286,7 @@ describe('util', () => {
util.validateSignMessageData({
data: '0x879a05',
} as any),
).toThrow();
).toThrow('Invalid "from" address: undefined must be a valid string.');
});

it('should throw if invalid from address', () => {
Expand All @@ -289,7 +295,7 @@ describe('util', () => {
data: '0x879a05',
from: '3244e191f1b4903970224322180f1fbbc415696b',
} as any),
).toThrow();
).toThrow('Invalid "from" address: 3244e191f1b4903970224322180f1fbbc415696b must be a valid string.');
});

it('should throw if invalid type from address', () => {
Expand All @@ -298,15 +304,15 @@ describe('util', () => {
data: '0x879a05',
from: 123,
} as any),
).toThrow();
).toThrow('Invalid "from" address: 123 must be a valid string.');
});

it('should throw if no data', () => {
expect(() =>
util.validateSignMessageData({
data: '0x879a05',
} as any),
).toThrow();
).toThrow('Invalid "from" address: undefined must be a valid string.');
});

it('should throw if invalid tyoe data', () => {
Expand All @@ -315,7 +321,7 @@ describe('util', () => {
data: 123,
from: '0x3244e191f1b4903970224322180f1fbbc415696b',
} as any),
).toThrow();
).toThrow('Invalid message "data": 123 must be a valid string.');
});
});

Expand Down

0 comments on commit f6699b1

Please sign in to comment.