Skip to content

Commit

Permalink
Fix lint rules for tests (#245)
Browse files Browse the repository at this point in the history
* Fix lint rules for tests

The lint configuration for tests was wrong (the file glob had the wrong
file extension), so none of the test lint rules or globals were being
set correctly. This has been fixed, and all rule violations have been
fixed.

There were a number of workarounds used in the tests for the globals
`expect` and `it` not being recognized by the linter. These workarounds
have now been removed as well, since globals are now correctly set.

* Preserve restricted matchers unrelated to snapshots
  • Loading branch information
Gudahtt committed Apr 21, 2022
1 parent 219511e commit cde38f7
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 316 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.js
Expand Up @@ -7,11 +7,10 @@ module.exports = {
extends: ['@metamask/eslint-config-typescript'],
},
{
files: ['*.test.js'],
files: ['*.test.ts'],
extends: ['@metamask/eslint-config-jest'],
},
],
plugins: ['jsdoc'],
rules: {
camelcase: [
'error',
Expand Down
7 changes: 0 additions & 7 deletions src/__snapshots__/utils.test.ts.snap

This file was deleted.

18 changes: 9 additions & 9 deletions src/encryption.test.ts
Expand Up @@ -23,13 +23,13 @@ describe('encryption', function () {
ciphertext: 'f8kBcl/NCyf3sybfbwAKk/np2Bzt9lRVkZejr6uh5FgnNlH/ic62DZzy',
};

it("Getting bob's encryptionPublicKey", async function () {
it("getting bob's encryptionPublicKey", async function () {
const result = await getEncryptionPublicKey(bob.ethereumPrivateKey);
expect(result).toBe(bob.encryptionPublicKey);
});

// encryption test
it("Alice encrypts message with bob's encryptionPublicKey", async function () {
it("alice encrypts message with bob's encryptionPublicKey", async function () {
const result = await encrypt({
publicKey: bob.encryptionPublicKey,
data: secretMessage,
Expand All @@ -43,7 +43,7 @@ describe('encryption', function () {
});

// safe encryption test
it("Alice encryptsSafely message with bob's encryptionPublicKey", async function () {
it("alice encryptsSafely message with bob's encryptionPublicKey", async function () {
const version = 'x25519-xsalsa20-poly1305';
const result = await encryptSafely({
publicKey: bob.encryptionPublicKey,
Expand All @@ -58,7 +58,7 @@ describe('encryption', function () {
});

// safe decryption test
it('Bob decryptSafely message that Alice encryptSafely for him', async function () {
it('bob decryptSafely message that Alice encryptSafely for him', async function () {
const version = 'x25519-xsalsa20-poly1305';
const result = await encryptSafely({
publicKey: bob.encryptionPublicKey,
Expand All @@ -74,15 +74,15 @@ describe('encryption', function () {
});

// decryption test
it('Bob decrypts message that Alice sent to him', function () {
it('bob decrypts message that Alice sent to him', function () {
const result = decrypt({
encryptedData,
privateKey: bob.ethereumPrivateKey,
});
expect(result).toBe(secretMessage);
});

it('Decryption failed because version is wrong or missing', function () {
it('decryption failed because version is wrong or missing', function () {
const badVersionData = {
version: 'x256k1-aes256cbc',
nonce: '1dvWO7uOnBnO7iNDJ9kO9pTasLuKNlej',
Expand All @@ -98,7 +98,7 @@ describe('encryption', function () {
).toThrow('Encryption type/version not supported.');
});

it('Decryption failed because nonce is wrong or missing', function () {
it('decryption failed because nonce is wrong or missing', function () {
// encrypted data
const badNonceData = {
version: 'x25519-xsalsa20-poly1305',
Expand All @@ -115,7 +115,7 @@ describe('encryption', function () {
).toThrow('bad nonce size');
});

it('Decryption failed because ephemPublicKey is wrong or missing', function () {
it('decryption failed because ephemPublicKey is wrong or missing', function () {
// encrypted data
const badEphemData = {
version: 'x25519-xsalsa20-poly1305',
Expand All @@ -132,7 +132,7 @@ describe('encryption', function () {
).toThrow('Decryption failed.');
});

it('Decryption failed because cyphertext is wrong or missing', function () {
it('decryption failed because cyphertext is wrong or missing', function () {
// encrypted data
const badEphemData = {
version: 'x25519-xsalsa20-poly1305',
Expand Down
10 changes: 3 additions & 7 deletions src/personal-sign.test.ts
Expand Up @@ -127,19 +127,15 @@ describe('personalSign', function () {
];

for (const { testLabel, message, signature, addressHex, key } of testCases) {
// Reassigned to silence "no-loop-func" ESLint rule
// It was complaining because it saw that `it` and `expect` as "modified variables from the outer scope"
// which can be dangerous to reference in a loop. But they aren't modified in this case, just invoked.
const _expect = expect;
it(testLabel, function () {
it(`${testLabel}`, function () {
const signed = personalSign({ privateKey: key, data: message });
_expect(signed).toBe(signature);
expect(signed).toBe(signature);

const recovered = recoverPersonalSignature({
data: message,
signature,
});
_expect(recovered).toBe(addressHex);
expect(recovered).toBe(addressHex);
});
}

Expand Down

0 comments on commit cde38f7

Please sign in to comment.