Skip to content

Commit

Permalink
enable type checking for tests and mocks (#325)
Browse files Browse the repository at this point in the history
* use separate tsconfig files for development and release

* fix type errors in tests

* add typecheck script

* address PR feedback
  • Loading branch information
dsokal authored Apr 13, 2021
1 parent 38bc806 commit fbaba77
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
with:
node-version: ${{ matrix.node }}
- run: yarn install --frozen-lockfile --check-files
- run: yarn lerna run build
- run: yarn typecheck
- run: yarn test
if: ${{ !matrix.coverage }}
- run: yarn test --coverage
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"scripts": {
"build": "lerna run build",
"build:tarballs:linux": "yarn build && yarn workspace eas-cli oclif-dev pack --targets linux-x64",
"typecheck": "lerna run typecheck",
"start": "lerna run watch --parallel",
"watch": "yarn start",
"eas": "packages/eas-cli/bin/run",
"lint": "eslint . --ext .ts",
"release": "lerna version",
Expand Down
5 changes: 3 additions & 2 deletions packages/eas-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@
"scripts": {
"postpack": "rm -f oclif.manifest.json",
"prepack": "rm -rf build && yarn build && yarn oclif-dev manifest && yarn oclif-dev readme",
"build": "tsc",
"watch": "tsc --watch",
"build": "tsc --project tsconfig.build.json",
"watch": "yarn build --watch",
"typecheck": "tsc",
"test": "jest",
"version": "yarn oclif-dev readme && git add README.md",
"generate-graphql-code": "graphql-codegen --config graphql-codegen.yml"
Expand Down
4 changes: 2 additions & 2 deletions packages/eas-cli/src/__tests__/api-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('apiClient', () => {
],
});

let error: Error;
let error: Error | null = null;
try {
await apiClient.post('test');
} catch (e) {
Expand All @@ -41,7 +41,7 @@ describe('apiClient', () => {
it('does not convert non-APIv2 error to ApiV2Error', async () => {
nock(getExpoApiBaseUrl()).post('/--/api/v2/test').reply(500, 'Something went wrong');

let error: Error;
let error: Error | null = null;
try {
await apiClient.post('test');
} catch (e) {
Expand Down
25 changes: 16 additions & 9 deletions packages/eas-cli/src/build/ios/__tests__/version-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ExpoConfig } from '@expo/config';
import { IOSConfig } from '@expo/config-plugins';
import fs from 'fs-extra';
import { vol } from 'memfs';
import os from 'os';
Expand Down Expand Up @@ -38,9 +39,11 @@ describe(bumpVersionAsync, () => {
});

const appJSON = await fs.readJSON('/repo/app.json');
const infoPlist = await readPlistAsync('/repo/ios/myproject/Info.plist');
const infoPlist = (await readPlistAsync(
'/repo/ios/myproject/Info.plist'
)) as IOSConfig.InfoPlist;
expect(fakeExp.version).toBe('1.0.0');
expect(fakeExp.ios.buildNumber).toBe('2');
expect(fakeExp.ios?.buildNumber).toBe('2');
expect(appJSON.expo.version).toBe('1.0.0');
expect(appJSON.expo.ios.buildNumber).toBe('2');
expect(infoPlist['CFBundleShortVersionString']).toBe('1.0.0');
Expand All @@ -57,9 +60,11 @@ describe(bumpVersionAsync, () => {
});

const appJSON = await fs.readJSON('/repo/app.json');
const infoPlist = await readPlistAsync('/repo/ios/myproject/Info.plist');
const infoPlist = (await readPlistAsync(
'/repo/ios/myproject/Info.plist'
)) as IOSConfig.InfoPlist;
expect(fakeExp.version).toBe('1.0.1');
expect(fakeExp.ios.buildNumber).toBe('1');
expect(fakeExp.ios?.buildNumber).toBe('1');
expect(appJSON.expo.version).toBe('1.0.1');
expect(appJSON.expo.ios.buildNumber).toBe('1');
expect(infoPlist['CFBundleShortVersionString']).toBe('1.0.1');
Expand All @@ -76,9 +81,11 @@ describe(bumpVersionAsync, () => {
});

const appJSON = await fs.readJSON('/repo/app.json');
const infoPlist = await readPlistAsync('/repo/ios/myproject/Info.plist');
const infoPlist = (await readPlistAsync(
'/repo/ios/myproject/Info.plist'
)) as IOSConfig.InfoPlist;
expect(fakeExp.version).toBe('1.0.0');
expect(fakeExp.ios.buildNumber).toBe('1');
expect(fakeExp.ios?.buildNumber).toBe('1');
expect(appJSON.expo.version).toBe('1.0.0');
expect(appJSON.expo.ios.buildNumber).toBe('1');
expect(infoPlist['CFBundleShortVersionString']).toBe('1.0.0');
Expand All @@ -99,7 +106,7 @@ describe(bumpVersionInAppJsonAsync, () => {

const appJSON = await fs.readJSON('/repo/app.json');
expect(fakeExp.version).toBe('1.0.0');
expect(fakeExp.ios.buildNumber).toBe('2');
expect(fakeExp.ios?.buildNumber).toBe('2');
expect(appJSON.expo.version).toBe('1.0.0');
expect(appJSON.expo.ios.buildNumber).toBe('2');
});
Expand All @@ -115,7 +122,7 @@ describe(bumpVersionInAppJsonAsync, () => {

const appJSON = await fs.readJSON('/repo/app.json');
expect(fakeExp.version).toBe('1.0.1');
expect(fakeExp.ios.buildNumber).toBe('1');
expect(fakeExp.ios?.buildNumber).toBe('1');
expect(appJSON.expo.version).toBe('1.0.1');
expect(appJSON.expo.ios.buildNumber).toBe('1');
});
Expand All @@ -131,7 +138,7 @@ describe(bumpVersionInAppJsonAsync, () => {

const appJSON = await fs.readJSON('/repo/app.json');
expect(fakeExp.version).toBe('1.0.0');
expect(fakeExp.ios.buildNumber).toBe('1');
expect(fakeExp.ios?.buildNumber).toBe('1');
expect(appJSON.expo.version).toBe('1.0.0');
expect(appJSON.expo.ios.buildNumber).toBe('1');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ export const jester: Actor = {
id: 'jester-id',
username: 'jester',
accounts: [{ id: 'jester-account-id', name: 'jester' }],
isExpoAdmin: false,
};

export const jester2: Actor = {
__typename: 'User',
id: 'jester2-id',
username: 'jester2',
accounts: [{ id: 'jester2-account-id', name: 'jester2' }],
isExpoAdmin: false,
};

export const testUsername = jester.username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ export function createManagerMock(mockOverride: Record<string, any> = {}): Crede
}),
},
mockOverride
);
) as any;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import nullthrows from 'nullthrows';

import { confirmAsync } from '../../../../../prompts';
import { getAppstoreMock, testAuthCtx } from '../../../../__tests__/fixtures-appstore';
import { createCtxMock } from '../../../../__tests__/fixtures-context';
Expand Down Expand Up @@ -32,9 +34,10 @@ describe('SetupProvisioningProfile', () => {
authCtx: testAuthCtx,
listProvisioningProfilesAsync: jest.fn(() => [
{
provisioningProfileId:
provisioningProfileId: nullthrows(
testIosAppCredentialsWithBuildCredentialsQueryResult.iosAppBuildCredentialsArray[0]
.provisioningProfile.developerPortalIdentifier,
.provisioningProfile
).developerPortalIdentifier,
},
]),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mockdate from 'mockdate';
import nullthrows from 'nullthrows';

import Log from '../../../../log';
import { IosAppCredentialsQuery } from '../../api/graphql/queries/IosAppCredentialsQuery';
Expand All @@ -18,7 +19,7 @@ describe('print credentials', () => {
appleAppIdentifierId: 'test-id',
}
);
displayIosAppCredentials(testIosAppCredentialsData);
displayIosAppCredentials(nullthrows(testIosAppCredentialsData));
const loggedSoFar = (Log.log as jest.Mock).mock.calls.reduce(
(acc, mockValue) => acc + mockValue
);
Expand Down
9 changes: 5 additions & 4 deletions packages/eas-cli/src/devices/__tests__/manager-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ describe(AccountResolver, () => {
{ id: 'account_id_777', name: 'dominik' },
{ id: 'account_id_888', name: 'foo' },
],
isExpoAdmin: false,
};

describe('when inside project dir', () => {
const projectDir = '/app';
const exp = {} as any;

it('returns the account defined in app.json/app.config.js if user confirms', async () => {
asMock(prompts).mockImplementationOnce(() => ({
value: true,
}));

const resolver = new AccountResolver(projectDir, user);
const resolver = new AccountResolver(exp, user);
const account = await resolver.resolveAccountAsync();
expect(account).toEqual(user.accounts[1]);
});
Expand All @@ -53,7 +54,7 @@ describe(AccountResolver, () => {
account: user.accounts[0],
}));

const resolver = new AccountResolver(projectDir, user);
const resolver = new AccountResolver(exp, user);
const account = await resolver.resolveAccountAsync();
expect(account).toEqual(user.accounts[0]);
});
Expand All @@ -71,7 +72,7 @@ describe(AccountResolver, () => {
const originalConsoleWarn = console.warn;
console.warn = jest.fn();

const resolver = new AccountResolver(projectDir, userWithAccessToProjectAccount);
const resolver = new AccountResolver(exp, userWithAccessToProjectAccount);
const account = await resolver.resolveAccountAsync();
expect(account).toEqual(user.accounts[0]);
expect(console.warn).toBeCalledWith(expect.stringMatching(/doesn't have access to the/));
Expand Down
7 changes: 7 additions & 0 deletions packages/eas-cli/src/project/__tests__/projectUtils-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe(getProjectAccountName, () => {
id: 'userId',
username: 'notbrent',
accounts: [],
isExpoAdmin: false,
});
expect(projectAccountName).toBe(expWithOwner.owner);
});
Expand All @@ -74,6 +75,7 @@ describe(getProjectAccountName, () => {
id: 'userId',
firstName: 'notauser',
accounts: [],
isExpoAdmin: false,
});
expect(projectAccountName).toBe(expWithOwner.owner);
});
Expand All @@ -84,6 +86,7 @@ describe(getProjectAccountName, () => {
id: 'userId',
username: 'dominik',
accounts: [],
isExpoAdmin: false,
});
expect(projectAccountName).toBe('dominik');
});
Expand All @@ -95,6 +98,7 @@ describe(getProjectAccountName, () => {
id: 'userId',
firstName: 'notauser',
accounts: [],
isExpoAdmin: false,
});
expect(resolveProjectAccountName).toThrow('manifest property is required');
});
Expand All @@ -117,6 +121,7 @@ describe(getProjectAccountNameAsync, () => {
{ id: 'account_id_1', name: 'notnotbrent' },
{ id: 'account_id_2', name: 'dominik' },
],
isExpoAdmin: false,
}));

const projectAccountName = await getProjectAccountNameAsync(expWithOwner);
Expand All @@ -132,6 +137,7 @@ describe(getProjectAccountNameAsync, () => {
{ id: 'account_id_1', name: 'notnotbrent' },
{ id: 'account_id_2', name: 'dominik' },
],
isExpoAdmin: false,
}));

const projectAccountName = await getProjectAccountNameAsync(expWithoutOwner);
Expand All @@ -155,6 +161,7 @@ describe(getProjectAccountNameAsync, () => {
{ id: 'account_id_1', name: 'notnotbrent' },
{ id: 'account_id_2', name: 'dominik' },
],
isExpoAdmin: false,
}));
await expect(getProjectAccountNameAsync(expWithoutOwner)).rejects.toThrow(
'manifest property is required'
Expand Down
2 changes: 2 additions & 0 deletions packages/eas-cli/src/user/__tests__/User-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ const userStub: Actor = {
id: 'userId',
username: 'username',
accounts: [],
isExpoAdmin: false,
};

const robotStub: Actor = {
__typename: 'Robot',
id: 'userId',
firstName: 'GLaDOS',
accounts: [],
isExpoAdmin: false,
};

describe('getUserAsync', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/eas-cli/src/user/__tests__/actions-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ const userStub: Actor = {
id: 'userId',
username: 'username',
accounts: [],
isExpoAdmin: false,
};

const robotStub: Actor = {
__typename: 'Robot',
id: 'userId',
firstName: 'GLaDOS',
accounts: [],
isExpoAdmin: false,
};

describe('ensureActorHasUsername', () => {
Expand Down
18 changes: 9 additions & 9 deletions packages/eas-cli/src/user/__tests__/otp-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe(retryUsernamePasswordAuthWithOTPAsync, () => {
id: 'p0',
is_primary: true,
method: UserSecondFactorDeviceMethod.AUTHENTICATOR,
sms_phone_number: undefined,
sms_phone_number: null,
},
],
smsAutomaticallySent: false,
Expand Down Expand Up @@ -96,13 +96,13 @@ describe(retryUsernamePasswordAuthWithOTPAsync, () => {
id: 'p0',
is_primary: true,
method: UserSecondFactorDeviceMethod.AUTHENTICATOR,
sms_phone_number: undefined,
sms_phone_number: null,
},
{
id: 'p2',
is_primary: false,
method: UserSecondFactorDeviceMethod.AUTHENTICATOR,
sms_phone_number: undefined,
sms_phone_number: null,
},
],
smsAutomaticallySent: false,
Expand All @@ -126,7 +126,7 @@ describe(retryUsernamePasswordAuthWithOTPAsync, () => {
id: 'p0',
is_primary: true,
method: UserSecondFactorDeviceMethod.AUTHENTICATOR,
sms_phone_number: undefined,
sms_phone_number: null,
},
],
smsAutomaticallySent: false,
Expand Down Expand Up @@ -156,13 +156,13 @@ describe(retryUsernamePasswordAuthWithOTPAsync, () => {
id: 'p0',
is_primary: true,
method: UserSecondFactorDeviceMethod.AUTHENTICATOR,
sms_phone_number: undefined,
sms_phone_number: null,
},
{
id: 'p2',
is_primary: false,
method: UserSecondFactorDeviceMethod.AUTHENTICATOR,
sms_phone_number: undefined,
sms_phone_number: null,
},
],
smsAutomaticallySent: false,
Expand Down Expand Up @@ -195,7 +195,7 @@ describe(retryUsernamePasswordAuthWithOTPAsync, () => {
id: 'p0',
is_primary: true,
method: UserSecondFactorDeviceMethod.AUTHENTICATOR,
sms_phone_number: undefined,
sms_phone_number: null,
},
{
id: 'p2',
Expand Down Expand Up @@ -240,13 +240,13 @@ describe(retryUsernamePasswordAuthWithOTPAsync, () => {
id: 'p0',
is_primary: true,
method: UserSecondFactorDeviceMethod.AUTHENTICATOR,
sms_phone_number: undefined,
sms_phone_number: null,
},
{
id: 'p2',
is_primary: false,
method: UserSecondFactorDeviceMethod.AUTHENTICATOR,
sms_phone_number: undefined,
sms_phone_number: null,
},
],
smsAutomaticallySent: false,
Expand Down
4 changes: 4 additions & 0 deletions packages/eas-cli/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig",
"exclude": ["**/__mocks__/*", "**/__tests__/*"]
}
Loading

0 comments on commit fbaba77

Please sign in to comment.