Skip to content
New issue

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

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

Already on GitHub? # to your account

Continue migration from mocha to jest #17226

Closed
wants to merge 9 commits into from
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ module.exports = {
'app/scripts/lib/**/*.test.js',
'app/scripts/migrations/*.test.js',
'app/scripts/platforms/*.test.js',
'app/scripts/account-import-strategies/account-import-strategies.test.js',
'app/scripts/controllers/backup.test.js',
'app/scripts/controllers/cached-balances.test.js',
'app/scripts/controllers/incoming-transactions.test.js',
'development/**/*.test.js',
'shared/**/*.test.js',
'ui/**/*.test.js',
Expand Down Expand Up @@ -270,6 +274,10 @@ module.exports = {
'app/scripts/lib/**/*.test.js',
'app/scripts/migrations/*.test.js',
'app/scripts/platforms/*.test.js',
'app/scripts/account-import-strategies/account-import-strategies.test.js',
'app/scripts/controllers/backup.test.js',
'app/scripts/controllers/cached-balances.test.js',
'app/scripts/controllers/incoming-transactions.test.js',
'development/**/*.test.js',
'shared/**/*.test.js',
'shared/**/*.test.ts',
Expand Down
4 changes: 4 additions & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module.exports = {
'./app/scripts/controllers/permissions/**/*.test.js',
'./app/scripts/controllers/mmi-controller.test.js',
'./app/scripts/constants/error-utils.test.js',
'./app/scripts/account-import-strategies/account-import-strategies.test.js',
'./app/scripts/controllers/backup.test.js',
'./app/scripts/controllers/cached-balances.test.js',
'./app/scripts/controllers/incoming-transactions.test.js',
],
recursive: true,
require: ['test/env.js', 'test/setup.js'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,70 +1,69 @@
import { strict as assert } from 'assert';
/**
* @jest-environment node
*/
import { stripHexPrefix } from '../../../shared/modules/hexstring-utils';
import accountImporter from '.';

describe('Account Import Strategies', function () {
describe('Account Import Strategies', () => {
const privkey =
'0x4cfd3e90fc78b0f86bf7524722150bb8da9c60cd532564d7ff43f5716514f553';
const json =
'{"version":3,"id":"dbb54385-0a99-437f-83c0-647de9f244c3","address":"a7f92ce3fba24196cf6f4bd2e1eb3db282ba998c","Crypto":{"ciphertext":"bde13d9ade5c82df80281ca363320ce254a8a3a06535bbf6ffdeaf0726b1312c","cipherparams":{"iv":"fbf93718a57f26051b292f072f2e5b41"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"7ffe00488319dec48e4c49a120ca49c6afbde9272854c64d9541c83fc6acdffe","n":8192,"r":8,"p":1},"mac":"2adfd9c4bc1cdac4c85bddfb31d9e21a684e0e050247a70c5698facf6b7d4681"}}';

describe('private key import', function () {
it('imports a private key and strips 0x prefix', async function () {
describe('private key import', () => {
it('imports a private key and strips 0x prefix', async () => {
const importPrivKey = await accountImporter.importAccount('Private Key', [
privkey,
]);
assert.equal(importPrivKey, stripHexPrefix(privkey));

expect(importPrivKey).toStrictEqual(stripHexPrefix(privkey));
});

it('throws an error for empty string private key', async function () {
await assert.rejects(
async () => {
await accountImporter.importAccount('Private Key', ['']);
},
Error,
'no empty strings',
);
it('throws an error for empty string private key', async () => {
await expect(
async () => await accountImporter.importAccount('Private Key', ['']),
).rejects.toThrow('Cannot import an empty key.');
});

it('throws an error for undefined string private key', async function () {
await assert.rejects(async () => {
await accountImporter.importAccount('Private Key', [undefined]);
});
it('throws an error for undefined string private key', async () => {
await expect(
async () =>
await accountImporter.importAccount('Private Key', [undefined]),
).rejects.toThrow('Cannot import an empty key.');

await assert.rejects(async () => {
await accountImporter.importAccount('Private Key', []);
});
await expect(
async () => await accountImporter.importAccount('Private Key', []),
).rejects.toThrow('Cannot import an empty key.');
});

it('throws an error for invalid private key', async function () {
await assert.rejects(async () => {
await accountImporter.importAccount('Private Key', ['popcorn']);
});
it('throws an error for invalid private key', async () => {
await expect(
async () =>
await accountImporter.importAccount('Private Key', ['popcorn']),
).rejects.toThrow(`t('importAccountErrorNotHexadecimal')`);
});
});

describe('JSON keystore import', function () {
it('fails when password is incorrect for keystore', async function () {
describe('JSON keystore import', () => {
it('fails when password is incorrect for keystore', async () => {
const wrongPassword = 'password2';

try {
await accountImporter.importAccount('JSON File', [json, wrongPassword]);
} catch (error) {
assert.equal(
error.message,
'Key derivation failed - possibly wrong passphrase',
);
}
await expect(
async () =>
await accountImporter.importAccount('JSON File', [
json,
wrongPassword,
]),
).rejects.toThrow('Key derivation failed - possibly wrong passphrase');
});

it('imports json string and password to return a private key', async function () {
it('imports json string and password to return a private key', async () => {
const fileContentsPassword = 'password1';
const importJson = await accountImporter.importAccount('JSON File', [
json,
fileContentsPassword,
]);
assert.equal(
importJson,

expect(importJson).toStrictEqual(
'0x5733876abe94146069ce8bcbabbde2677f2e35fa33e875e92041ed2ac87e5bc7',
);
});
Expand Down
79 changes: 33 additions & 46 deletions app/scripts/controllers/backup.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { strict as assert } from 'assert';
import sinon from 'sinon';
import BackupController from './backup';

Expand Down Expand Up @@ -151,7 +150,7 @@ const jsonData = JSON.stringify({
},
});

describe('BackupController', function () {
describe('BackupController', () => {
const getBackupController = () => {
return new BackupController({
preferencesController: getMockPreferencesController(),
Expand All @@ -161,91 +160,79 @@ describe('BackupController', function () {
});
};

describe('constructor', function () {
it('should setup correctly', async function () {
describe('constructor', () => {
it('should setup correctly', async () => {
const backupController = getBackupController();
const selectedAddress =
backupController.preferencesController.getSelectedAddress();
assert.equal(selectedAddress, '0x01');
expect(selectedAddress).toStrictEqual('0x01');
});

it('should restore backup', async function () {
it('should restore backup', async () => {
const backupController = getBackupController();
await backupController.restoreUserData(jsonData);
// check networks backup
assert.equal(
backupController.networkController.state.networkConfigurations[
expect(
backupController.networkController.store.networkConfigurations[
'network-configuration-id-1'
].chainId,
'0x539',
);
assert.equal(
backupController.networkController.state.networkConfigurations[
).toStrictEqual('0x539');
expect(
backupController.networkController.store.networkConfigurations[
'network-configuration-id-2'
].chainId,
'0x38',
);
assert.equal(
backupController.networkController.state.networkConfigurations[
).toStrictEqual('0x38');
expect(
backupController.networkController.store.networkConfigurations[
'network-configuration-id-3'
].chainId,
'0x61',
);
assert.equal(
backupController.networkController.state.networkConfigurations[
).toStrictEqual('0x61');
expect(
backupController.networkController.store.networkConfigurations[
'network-configuration-id-4'
].chainId,
'0x89',
);
).toStrictEqual('0x89');
// make sure identities are not lost after restore
assert.equal(
expect(
backupController.preferencesController.store.identities[
'0x295e26495CEF6F69dFA69911d9D8e4F3bBadB89B'
].lastSelected,
1655380342907,
);
assert.equal(
).toStrictEqual(1655380342907);
expect(
backupController.preferencesController.store.identities[
'0x295e26495CEF6F69dFA69911d9D8e4F3bBadB89B'
].name,
'Account 3',
);
assert.equal(
).toStrictEqual('Account 3');
expect(
backupController.preferencesController.store.lostIdentities[
'0xfd59bbe569376e3d3e4430297c3c69ea93f77435'
].lastSelected,
1655379648197,
);
assert.equal(
).toStrictEqual(1655379648197);
expect(
backupController.preferencesController.store.lostIdentities[
'0xfd59bbe569376e3d3e4430297c3c69ea93f77435'
].name,
'Ledger 1',
);
).toStrictEqual('Ledger 1');
// make sure selected address is not lost after restore
assert.equal(
expect(
backupController.preferencesController.store.selectedAddress,
'0x01',
);
).toStrictEqual('0x01');
// check address book backup
assert.equal(
expect(
backupController.addressBookController.store.addressBook['0x61'][
'0x42EB768f2244C8811C63729A21A3569731535f06'
].chainId,
'0x61',
);
assert.equal(
).toStrictEqual('0x61');
expect(
backupController.addressBookController.store.addressBook['0x61'][
'0x42EB768f2244C8811C63729A21A3569731535f06'
].address,
'0x42EB768f2244C8811C63729A21A3569731535f06',
);
assert.equal(
).toStrictEqual('0x42EB768f2244C8811C63729A21A3569731535f06');
expect(
backupController.addressBookController.store.addressBook['0x61'][
'0x42EB768f2244C8811C63729A21A3569731535f06'
].isEns,
false,
);
).toStrictEqual(false);
});
});
});
51 changes: 23 additions & 28 deletions app/scripts/controllers/cached-balances.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { strict as assert } from 'assert';
import sinon from 'sinon';
import { CHAIN_IDS } from '../../../shared/constants/network';
import CachedBalancesController from './cached-balances';

describe('CachedBalancesController', function () {
describe('updateCachedBalances', function () {
it('should update the cached balances', async function () {
describe('CachedBalancesController', () => {
describe('updateCachedBalances', () => {
it('should update the cached balances', async () => {
const controller = new CachedBalancesController({
getCurrentChainId: () => CHAIN_IDS.GOERLI,
accountTracker: {
Expand All @@ -18,26 +16,24 @@ describe('CachedBalancesController', function () {
},
});

controller._generateBalancesToCache = sinon
.stub()
.callsFake(() => Promise.resolve('mockNewCachedBalances'));
jest
.spyOn(controller, '_generateBalancesToCache')
.mockResolvedValue('mockNewCachedBalances');

await controller.updateCachedBalances({ accounts: 'mockAccounts' });

assert.equal(controller._generateBalancesToCache.callCount, 1);
assert.deepEqual(controller._generateBalancesToCache.args[0], [
expect(controller._generateBalancesToCache).toHaveBeenCalledWith(
'mockAccounts',
CHAIN_IDS.GOERLI,
]);
assert.equal(
controller.store.getState().cachedBalances,
);
expect(controller.store.getState().cachedBalances).toStrictEqual(
'mockNewCachedBalances',
);
});
});

describe('_generateBalancesToCache', function () {
it('should generate updated account balances where the current network was updated', function () {
describe('_generateBalancesToCache', () => {
it('should generate updated account balances where the current network was updated', () => {
const controller = new CachedBalancesController({
accountTracker: {
store: {
Expand Down Expand Up @@ -69,7 +65,7 @@ describe('CachedBalancesController', function () {
CHAIN_IDS.GOERLI,
);

assert.deepEqual(result, {
expect(result).toStrictEqual({
[CHAIN_IDS.GOERLI]: {
a: '0x4',
b: '0x2',
Expand All @@ -83,7 +79,7 @@ describe('CachedBalancesController', function () {
});
});

it('should generate updated account balances where the a new network was selected', function () {
it('should generate updated account balances where the a new network was selected', () => {
const controller = new CachedBalancesController({
accountTracker: {
store: {
Expand All @@ -110,7 +106,7 @@ describe('CachedBalancesController', function () {
16,
);

assert.deepEqual(result, {
expect(result).toStrictEqual({
[CHAIN_IDS.GOERLI]: {
a: '0x1',
b: '0x2',
Expand All @@ -124,9 +120,9 @@ describe('CachedBalancesController', function () {
});
});

describe('_registerUpdates', function () {
it('should subscribe to the account tracker with the updateCachedBalances method', async function () {
const subscribeSpy = sinon.spy();
describe('_registerUpdates', () => {
it('should subscribe to the account tracker with the updateCachedBalances method', async () => {
const subscribeSpy = jest.fn();
const controller = new CachedBalancesController({
getCurrentChainId: () => CHAIN_IDS.GOERLI,
accountTracker: {
Expand All @@ -135,17 +131,16 @@ describe('CachedBalancesController', function () {
},
},
});
subscribeSpy.resetHistory();

const updateCachedBalancesSpy = sinon.spy();
subscribeSpy.mockReset();

const updateCachedBalancesSpy = jest.fn();
controller.updateCachedBalances = updateCachedBalancesSpy;
controller._registerUpdates({ accounts: 'mockAccounts' });

assert.equal(subscribeSpy.callCount, 1);

subscribeSpy.args[0][0]();

assert.equal(updateCachedBalancesSpy.callCount, 1);
expect(subscribeSpy).toHaveBeenCalled();
subscribeSpy.mock.calls[0][0]();
expect(updateCachedBalancesSpy).toHaveBeenCalled();
});
});
});
Loading