Skip to content

Commit 7ea8a5a

Browse files
authored
Merge pull request #83 from magiclabs/mushfichowdhury/sc49279/convertAllTestsToJest
Converted all Ava and Sinon testing to Jest
2 parents a19f1d9 + 2170a60 commit 7ea8a5a

27 files changed

+1976
-1632
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jest.config.ts

jest.config.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { Config } from '@jest/types';
2+
3+
const config: Config.InitialOptions = {
4+
maxWorkers: 2,
5+
preset: 'ts-jest',
6+
coverageReporters: ['text-summary', 'html'],
7+
collectCoverageFrom: ['./src/**/*.{ts,tsx,}'],
8+
collectCoverage: true,
9+
testTimeout: 30000, // 30s
10+
coverageThreshold: {
11+
global: {
12+
lines: 99,
13+
statements: 99,
14+
functions: 99,
15+
branches: 99,
16+
}
17+
},
18+
globals: {
19+
'ts-jest': {
20+
tsconfig: './test/tsconfig.json',
21+
isolatedModules: true,
22+
},
23+
},
24+
};
25+
26+
export default config;

package.json

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"test": "npm run clean:test-artifacts && ./scripts/test.sh",
1717
"lint": "eslint --fix src/**/*.ts",
1818
"clean": "npm-run-all -s clean:*",
19-
"clean:test-artifacts": "rimraf coverage && rimraf .nyc_output",
19+
"clean:test-artifacts": "rimraf coverage",
2020
"clean:build": "rimraf dist",
2121
"clean_node_modules": "rimraf node_modules"
2222
},
@@ -26,12 +26,11 @@
2626
"@istanbuljs/nyc-config-typescript": "^1.0.1",
2727
"@types/eth-sig-util": "^2.1.0",
2828
"@types/express": "^4.17.2",
29+
"@types/jest": "^27.4.1",
2930
"@types/node": "^13.1.2",
3031
"@types/node-fetch": "^2.5.4",
31-
"@types/sinon": "^7.5.2",
3232
"@typescript-eslint/eslint-plugin": "^2.15.0",
3333
"auto": "^9.60.1",
34-
"ava": "^3.4.0",
3534
"boxen-cli": "^1.0.0",
3635
"eslint": "^6.7.2",
3736
"eslint-import-resolver-typescript": "^2.0.0",
@@ -41,15 +40,15 @@
4140
"eslint-plugin-react": "^7.15.1",
4241
"eslint-plugin-react-hooks": "^1.7.0",
4342
"husky": "^4.2.3",
43+
"jest": "^27.5.1",
4444
"lint-staged": "^10.0.8",
4545
"npm-run-all": "~4.1.5",
46-
"nyc": "^15.0.0",
4746
"prettier": "^1.19.1",
4847
"rimraf": "~3.0.0",
49-
"sinon": "^9.0.0",
5048
"ts-node": "~8.5.2",
5149
"tslint": "~5.20.1",
52-
"typescript": "~3.8.3"
50+
"typescript": "~3.8.3",
51+
"ts-jest": "^27.1.3"
5352
},
5453
"dependencies": {
5554
"eth-sig-util": "2.1.2",
@@ -70,31 +69,5 @@
7069
"npm",
7170
"released"
7271
]
73-
},
74-
"ava": {
75-
"require": [
76-
"ts-node/register"
77-
],
78-
"files": [
79-
"test/**/*.spec.ts"
80-
],
81-
"extensions": [
82-
"ts"
83-
],
84-
"verbose": true
85-
},
86-
"nyc": {
87-
"extends": "@istanbuljs/nyc-config-typescript",
88-
"all": false,
89-
"check-coverage": true,
90-
"per-file": true,
91-
"lines": 99,
92-
"statements": 99,
93-
"functions": 99,
94-
"branches": 99,
95-
"reporter": [
96-
"html",
97-
"lcov"
98-
]
9972
}
10073
}

scripts/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ export NODE_OPTIONS=--max_old_space_size=4096
1010
export NODE_ENV=test
1111

1212
export TS_NODE_PROJECT="test/tsconfig.json"
13-
npx nyc --reporter=lcov --reporter=text-summary ava $input
13+
jest $input
Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/* eslint-disable no-underscore-dangle */
2-
3-
import test, { ExecutionContext } from 'ava';
42
import {
53
MagicAdminSDKError,
64
createTokenExpiredError,
@@ -12,86 +10,75 @@ import {
1210
createTokenCannotBeUsedYetError,
1311
} from '../../../../src/core/sdk-exceptions';
1412

15-
function errorAssertions<T extends ExecutionContext<any>>(
16-
t: T,
13+
function errorAssertions(
1714
error: MagicAdminSDKError,
1815
expectedCode: string,
1916
expectedMessage: string,
2017
expectedData: any[] = [],
2118
) {
22-
t.true(error instanceof MagicAdminSDKError);
23-
t.is(error.code, expectedCode);
24-
t.is(error.message, `Magic Admin SDK Error: [${expectedCode}] ${expectedMessage}`);
25-
t.deepEqual(error.data, expectedData);
19+
expect(error instanceof MagicAdminSDKError).toBe(true);
20+
expect(error.code).toBe(expectedCode);
21+
expect(error.message).toBe(`Magic Admin SDK Error: [${expectedCode}] ${expectedMessage}`);
22+
expect(error.data).toEqual(expectedData);
2623
}
2724

28-
test('#01: Creates `ERROR_DIDT_EXPIRED` error', async t => {
25+
test('#01: Creates `ERROR_DIDT_EXPIRED` error', async () => {
2926
const error = createTokenExpiredError();
30-
errorAssertions(t, error, 'ERROR_DIDT_EXPIRED', 'DID Token has expired. Request failed authentication.');
27+
errorAssertions(error, 'ERROR_DIDT_EXPIRED', 'DID Token has expired. Request failed authentication.');
3128
});
3229

33-
test('#02: Creates `ERROR_DIDT_CANNOT_BE_USED_YET` error', async t => {
30+
test('#02: Creates `ERROR_DIDT_CANNOT_BE_USED_YET` error', async () => {
3431
const error = createTokenCannotBeUsedYetError();
3532
errorAssertions(
36-
t,
3733
error,
3834
'ERROR_DIDT_CANNOT_BE_USED_YET',
3935
'Given DID Token cannot be used at this time. Please check the `nbf` field and regenerate a new token with a suitable value.',
4036
);
4137
});
4238

43-
test('#03: Creates `ERROR_INCORRECT_SIGNER_ADDR` error', async t => {
39+
test('#03: Creates `ERROR_INCORRECT_SIGNER_ADDR` error', async () => {
4440
const error = createIncorrectSignerAddressError();
4541
errorAssertions(
46-
t,
4742
error,
4843
'ERROR_INCORRECT_SIGNER_ADDR',
4944
'Incorrect signer address for DID Token. Request failed authentication.',
5045
);
5146
});
5247

53-
test('#04: Creates `ERROR_FAILED_RECOVERING_PROOF` error', async t => {
48+
test('#04: Creates `ERROR_FAILED_RECOVERING_PROOF` error', async () => {
5449
const error = createFailedRecoveringProofError();
55-
errorAssertions(t, error, 'ERROR_FAILED_RECOVERING_PROOF', 'Failed to recover proof. Request failed authentication.');
50+
errorAssertions(error, 'ERROR_FAILED_RECOVERING_PROOF', 'Failed to recover proof. Request failed authentication.');
5651
});
5752

58-
test('#05: Creates `ERROR_SECRET_API_KEY_MISSING` error', async t => {
53+
test('#05: Creates `ERROR_SECRET_API_KEY_MISSING` error', async () => {
5954
const error = createApiKeyMissingError();
6055
errorAssertions(
61-
t,
6256
error,
6357
'ERROR_SECRET_API_KEY_MISSING',
6458
'Please provide a secret Fortmatic API key that you acquired from the developer dashboard.',
6559
);
6660
});
6761

68-
test('#06: Creates `SERVICE_ERROR` error with empty `data` property', async t => {
62+
test('#06: Creates `SERVICE_ERROR` error with empty `data` property', async () => {
6963
const error = createServiceError();
7064
errorAssertions(
71-
t,
7265
error,
7366
'SERVICE_ERROR',
7467
'A service error occurred while communicating with the Magic API. Check the `data` key of this error object to see nested errors with additional context.',
7568
);
7669
});
7770

78-
test('#07: Creates `SERVICE_ERROR` error with non-empty `data` property', async t => {
71+
test('#07: Creates `SERVICE_ERROR` error with non-empty `data` property', async () => {
7972
const error = createServiceError('hello', 'world');
8073
errorAssertions(
81-
t,
8274
error,
8375
'SERVICE_ERROR',
8476
'A service error occurred while communicating with the Magic API. Check the `data` key of this error object to see nested errors with additional context.',
8577
['hello', 'world'],
8678
);
8779
});
8880

89-
test('#08: Creates `EXPECTED_BEARER_STRING` error', async t => {
81+
test('#08: Creates `EXPECTED_BEARER_STRING` error', async () => {
9082
const error = createExpectedBearerStringError();
91-
errorAssertions(
92-
t,
93-
error,
94-
'EXPECTED_BEARER_STRING',
95-
'Expected argument to be a string in the `Bearer {token}` format.',
96-
);
83+
errorAssertions(error, 'EXPECTED_BEARER_STRING', 'Expected argument to be a string in the `Bearer {token}` format.');
9784
});
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import test from 'ava';
21
import { MagicAdminSDKError } from '../../../../../src/core/sdk-exceptions';
32

4-
test('#01: Instantiates `MagicAdminSDKError` with empty `data` property', t => {
3+
test('#01: Instantiates `MagicAdminSDKError` with empty `data` property', () => {
54
const error = new MagicAdminSDKError('TEST_CODE' as any, 'test message');
6-
t.true(error instanceof MagicAdminSDKError);
7-
t.is(error.message, 'Magic Admin SDK Error: [TEST_CODE] test message');
8-
t.is(error.code, 'TEST_CODE');
9-
t.deepEqual(error.data, []);
5+
expect(error instanceof MagicAdminSDKError).toBe(true);
6+
expect(error.message).toBe('Magic Admin SDK Error: [TEST_CODE] test message');
7+
expect(error.code).toBe('TEST_CODE');
8+
expect(error.data).toEqual([]);
109
});
1110

12-
test('#01: Instantiates `MagicAdminSDKError` with non-empty `data` property', t => {
11+
test('#01: Instantiates `MagicAdminSDKError` with non-empty `data` property', () => {
1312
const error = new MagicAdminSDKError('TEST_CODE' as any, 'test message', ['hello world']);
14-
t.true(error instanceof MagicAdminSDKError);
15-
t.is(error.message, 'Magic Admin SDK Error: [TEST_CODE] test message');
16-
t.is(error.code, 'TEST_CODE');
17-
t.deepEqual(error.data, ['hello world']);
13+
expect(error instanceof MagicAdminSDKError).toBe(true);
14+
expect(error.message).toBe('Magic Admin SDK Error: [TEST_CODE] test message');
15+
expect(error.code).toBe('TEST_CODE');
16+
expect(error.data).toEqual(['hello world']);
1817
});
Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
11
/* eslint-disable no-new */
2-
3-
import test from 'ava';
4-
import { MagicAdminSDK } from '../../../../src/core/sdk';
2+
import { Magic } from '../../../../src/index';
53
import { API_FULL_URL, API_KEY } from '../../../lib/constants';
64
import { TokenModule } from '../../../../src/modules/token';
75
import { UsersModule } from '../../../../src/modules/users';
86
import { UtilsModule } from '../../../../src/modules/utils';
97

10-
test.serial('#01: Initialize `MagicAdminSDK`', t => {
11-
const magic = new MagicAdminSDK(API_KEY);
8+
test('#01: Initialize `MagicAdminSDK`', () => {
9+
const magic = new Magic(API_KEY);
1210

13-
t.is(magic.secretApiKey, API_KEY);
14-
t.is(magic.apiBaseUrl, API_FULL_URL);
15-
t.true(magic.token instanceof TokenModule);
16-
t.true(magic.users instanceof UsersModule);
11+
expect(magic.secretApiKey).toBe(API_KEY);
12+
expect(magic.apiBaseUrl).toBe(API_FULL_URL);
13+
expect(magic.token instanceof TokenModule).toBe(true);
14+
expect(magic.users instanceof UsersModule).toBe(true);
1715
});
1816

19-
test.serial('#02: Initialize `MagicAdminSDK` with custom endpoint', t => {
20-
const magic = new MagicAdminSDK(API_KEY, { endpoint: 'https://example.com' });
17+
test('#02: Initialize `MagicAdminSDK` with custom endpoint', () => {
18+
const magic = new Magic(API_KEY, { endpoint: 'https://example.com' });
2119

22-
t.is(magic.secretApiKey, API_KEY);
23-
t.is(magic.apiBaseUrl, 'https://example.com');
24-
t.true(magic.token instanceof TokenModule);
25-
t.true(magic.users instanceof UsersModule);
26-
t.true(magic.utils instanceof UtilsModule);
20+
expect(magic.secretApiKey).toBe(API_KEY);
21+
expect(magic.apiBaseUrl).toBe('https://example.com');
22+
expect(magic.token instanceof TokenModule).toBe(true);
23+
expect(magic.users instanceof UsersModule).toBe(true);
24+
expect(magic.utils instanceof UtilsModule).toBe(true);
2725
});
2826

29-
test.serial('#03: Strips trailing slash(es) from custom endpoint argument', t => {
30-
const magicA = new MagicAdminSDK(API_KEY, { endpoint: 'https://example.com/' });
31-
const magicB = new MagicAdminSDK(API_KEY, { endpoint: 'https://example.com//' });
32-
const magicC = new MagicAdminSDK(API_KEY, { endpoint: 'https://example.com///' });
27+
test('#03: Strips trailing slash(es) from custom endpoint argument', () => {
28+
const magicA = new Magic(API_KEY, { endpoint: 'https://example.com/' });
29+
const magicB = new Magic(API_KEY, { endpoint: 'https://example.com//' });
30+
const magicC = new Magic(API_KEY, { endpoint: 'https://example.com///' });
3331

34-
t.is(magicA.apiBaseUrl, 'https://example.com');
35-
t.is(magicB.apiBaseUrl, 'https://example.com');
36-
t.is(magicC.apiBaseUrl, 'https://example.com');
32+
expect(magicA.apiBaseUrl).toBe('https://example.com');
33+
expect(magicB.apiBaseUrl).toBe('https://example.com');
34+
expect(magicC.apiBaseUrl).toBe('https://example.com');
3735
});
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import test from 'ava';
21
import { MagicAdminSDK } from '../../../../src/core/sdk';
32
import { BaseModule } from '../../../../src/modules/base-module';
43
import { createMagicAdminSDK } from '../../../lib/factories';
54

6-
test.serial('#01: Initializes `BaseModule`', t => {
5+
test('#01: Initializes `BaseModule`', () => {
76
const sdk = createMagicAdminSDK();
87

98
const baseModule: any = new (BaseModule as any)(sdk);
109

11-
t.true(baseModule instanceof BaseModule);
12-
t.true(baseModule.sdk instanceof MagicAdminSDK);
10+
expect(baseModule instanceof BaseModule).toBe(true);
11+
expect(baseModule.sdk instanceof MagicAdminSDK).toBe(true);
1312
});
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
import test from 'ava';
21
import { createMagicAdminSDK } from '../../../lib/factories';
32
import { VALID_DIDT, VALID_DIDT_DECODED, INVALID_DIDT_MALFORMED_CLAIM } from '../../../lib/constants';
4-
import { MagicAdminSDKError, createMalformedTokenError } from '../../../../src/core/sdk-exceptions';
3+
import { createMalformedTokenError } from '../../../../src/core/sdk-exceptions';
54

6-
test('#01: Successfully decodes DIDT', async t => {
5+
test('#01: Successfully decodes DIDT', async () => {
76
const sdk = createMagicAdminSDK();
87
const result = sdk.token.decode(VALID_DIDT);
9-
t.deepEqual(result, VALID_DIDT_DECODED);
8+
expect(result).toEqual(VALID_DIDT_DECODED);
109
});
1110

12-
test('#02: Throws error if token is malformed', async t => {
11+
test('#02: Throws error if token is malformed', async () => {
1312
const sdk = createMagicAdminSDK();
1413
const expectedError = createMalformedTokenError();
15-
const error: MagicAdminSDKError = t.throws(() => sdk.token.decode(INVALID_DIDT_MALFORMED_CLAIM));
16-
t.is(error.code, expectedError.code);
17-
t.is(error.message, expectedError.message);
14+
expect(() => sdk.token.decode(INVALID_DIDT_MALFORMED_CLAIM)).toThrow(expectedError);
1815
});
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import test from 'ava';
21
import { createMagicAdminSDK } from '../../../lib/factories';
32
import { VALID_DIDT, VALID_DIDT_PARSED_CLAIMS } from '../../../lib/constants';
43

5-
test('#01: Successfully gets issuer from DIDT', t => {
4+
test('#01: Successfully gets issuer from DIDT', () => {
65
const sdk = createMagicAdminSDK();
76
const result = sdk.token.getIssuer(VALID_DIDT);
87
const expected = VALID_DIDT_PARSED_CLAIMS.iss;
9-
t.is(result, expected);
8+
expect(result).toBe(expected);
109
});
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import test from 'ava';
21
import { createMagicAdminSDK } from '../../../lib/factories';
32
import { VALID_DIDT, VALID_DIDT_PARSED_CLAIMS } from '../../../lib/constants';
43

5-
test('#01: Successfully gets public address from DIDT', t => {
4+
test('#01: Successfully gets public address from DIDT', () => {
65
const sdk = createMagicAdminSDK();
76
const result = sdk.token.getPublicAddress(VALID_DIDT);
87
const expected = VALID_DIDT_PARSED_CLAIMS.iss.split(':')[2];
9-
t.is(result, expected);
8+
expect(result).toBe(expected);
109
});

0 commit comments

Comments
 (0)