Skip to content

Commit

Permalink
[eas-cli] Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
radoslawkrzemien committed Dec 1, 2023
1 parent c057738 commit 0683110
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions packages/eas-cli/src/commandUtils/__tests__/EasCommand-test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Config } from '@oclif/core';
import { CombinedError } from '@urql/core';
import { GraphQLError } from 'graphql/error';
import { v4 as uuidv4 } from 'uuid';
Expand Down Expand Up @@ -42,14 +43,13 @@ beforeEach(() => {
jest.mocked(createAnalyticsAsync).mockResolvedValue(analytics);
});

const createTestEasCommand = (): typeof EasCommand => {
class TestEasCommand extends EasCommand {
async runAsync(): Promise<void> {}
class TestEasCommand extends EasCommand {
async runAsync(): Promise<void> {}
constructor() {
super([], {} as Config);
this.id = 'testEasCommand';
}

TestEasCommand.id = 'testEasCommand'; // normally oclif will assign ids, but b/c this is located outside the commands folder it will not
return TestEasCommand;
};
}

describe(EasCommand.name, () => {
describe('without exceptions', () => {
Expand All @@ -61,30 +61,30 @@ describe(EasCommand.name, () => {
// See https://github.com/oclif/command/blob/master/src/command.ts#L80
// and look for "Config.load"
it('ensures the user data is read', async () => {
const TestEasCommand = createTestEasCommand();
await TestEasCommand.run();
const testEasCommand = new TestEasCommand();
await testEasCommand.run();

const sessionManagerSpy = jest.spyOn(SessionManager.prototype, 'getUserAsync');
expect(sessionManagerSpy).toBeCalledTimes(1);
}, 30_000);

it('initializes analytics', async () => {
const TestEasCommand = createTestEasCommand();
await TestEasCommand.run();
const testEasCommand = new TestEasCommand();
await testEasCommand.run();

expect(createAnalyticsAsync).toHaveBeenCalled();
});

it('flushes analytics', async () => {
const TestEasCommand = createTestEasCommand();
await TestEasCommand.run();
const testEasCommand = new TestEasCommand();
await testEasCommand.run();

expect(analytics.flushAsync).toHaveBeenCalled();
});

it('logs events', async () => {
const TestEasCommand = createTestEasCommand();
await TestEasCommand.run();
const testEasCommand = new TestEasCommand();
await testEasCommand.run();

expect(analytics.logEvent).toHaveBeenCalledWith('action', {
action: `eas ${TestEasCommand.id}`,
Expand All @@ -94,9 +94,9 @@ describe(EasCommand.name, () => {

describe('after exceptions', () => {
it('flushes analytics', async () => {
const TestEasCommand = createTestEasCommand();
const testEasCommand = new TestEasCommand();
try {
await TestEasCommand.run().then(() => {
await testEasCommand.run().then(() => {
throw new Error('foo');
});
} catch {}
Expand All @@ -106,7 +106,7 @@ describe(EasCommand.name, () => {

describe('catch', () => {
it('logs the message', async () => {
const TestEasCommand = createTestEasCommand();
const testEasCommand = new TestEasCommand();
const logErrorSpy = jest.spyOn(Log, 'error');
const logDebugSpy = jest.spyOn(Log, 'debug');
const runAsyncMock = jest.spyOn(TestEasCommand.prototype as any, 'runAsync');
Expand All @@ -115,15 +115,15 @@ describe(EasCommand.name, () => {
throw error;
});
try {
await TestEasCommand.run();
await testEasCommand.run();
} catch {}

expect(logErrorSpy).toBeCalledWith('Unexpected, internal error message');
expect(logDebugSpy).toBeCalledWith(error);
});

it('logs the cleaned message if needed', async () => {
const TestEasCommand = createTestEasCommand();
const testEasCommand = new TestEasCommand();
const logErrorSpy = jest.spyOn(Log, 'error');
const logDebugSpy = jest.spyOn(Log, 'debug');
const runAsyncMock = jest.spyOn(TestEasCommand.prototype as any, 'runAsync');
Expand All @@ -133,15 +133,15 @@ describe(EasCommand.name, () => {
throw error;
});
try {
await TestEasCommand.run();
await testEasCommand.run();
} catch {}

expect(logErrorSpy).toBeCalledWith('Unexpected GraphQL error message');
expect(logDebugSpy).toBeCalledWith(error);
});

it('logs the cleaned message with request ID if present', async () => {
const TestEasCommand = createTestEasCommand();
const testEasCommand = new TestEasCommand();
const logErrorSpy = jest.spyOn(Log, 'error');
const logDebugSpy = jest.spyOn(Log, 'debug');
const runAsyncMock = jest.spyOn(TestEasCommand.prototype as any, 'runAsync');
Expand All @@ -163,7 +163,7 @@ describe(EasCommand.name, () => {
throw error;
});
try {
await TestEasCommand.run();
await testEasCommand.run();
} catch {}

expect(logErrorSpy).toBeCalledWith(
Expand All @@ -173,7 +173,7 @@ describe(EasCommand.name, () => {
});

it('logs the cleaned messages with request IDs if multiple GraphQL errors present', async () => {
const TestEasCommand = createTestEasCommand();
const testEasCommand = new TestEasCommand();
const logErrorSpy = jest.spyOn(Log, 'error');
const logDebugSpy = jest.spyOn(Log, 'debug');
const runAsyncMock = jest.spyOn(TestEasCommand.prototype as any, 'runAsync');
Expand All @@ -196,7 +196,7 @@ describe(EasCommand.name, () => {
throw error;
});
try {
await TestEasCommand.run();
await testEasCommand.run();
} catch {}

expect(logErrorSpy).toBeCalledWith(
Expand All @@ -211,36 +211,36 @@ describe(EasCommand.name, () => {
});

it('re-throws the error with default base message', async () => {
const TestEasCommand = createTestEasCommand();
const testEasCommand = new TestEasCommand();
const runAsyncMock = jest.spyOn(TestEasCommand.prototype as any, 'runAsync');
runAsyncMock.mockImplementation(() => {
throw new Error('Error message');
});
try {
await TestEasCommand.run();
await testEasCommand.run();
} catch (caughtError) {
expect(caughtError).toBeInstanceOf(Error);
expect((caughtError as Error).message).toEqual('testEasCommand command failed.');
}
});

it('re-throws the error with a different default base message in case of CombinedError', async () => {
const TestEasCommand = createTestEasCommand();
const testEasCommand = new TestEasCommand();
const runAsyncMock = jest.spyOn(TestEasCommand.prototype as any, 'runAsync');
runAsyncMock.mockImplementation(() => {
const graphQLErrors = ['Unexpected GraphQL error message'];
throw new CombinedError({ graphQLErrors });
});
try {
await TestEasCommand.run();
await testEasCommand.run();
} catch (caughtError) {
expect(caughtError).toBeInstanceOf(Error);
expect((caughtError as Error).message).toEqual('GraphQL request failed.');
}
});

it('re-throws the error with a different default base message in case of CombinedError with multiple GraphQLErrors', async () => {
const TestEasCommand = createTestEasCommand();
const testEasCommand = new TestEasCommand();
const runAsyncMock = jest.spyOn(TestEasCommand.prototype as any, 'runAsync');
runAsyncMock.mockImplementation(() => {
const graphQLErrors = [
Expand All @@ -260,7 +260,7 @@ describe(EasCommand.name, () => {
throw new CombinedError({ graphQLErrors });
});
try {
await TestEasCommand.run();
await testEasCommand.run();
} catch (caughtError) {
expect(caughtError).toBeInstanceOf(Error);
expect((caughtError as Error).message).toEqual('GraphQL request failed.');
Expand Down

0 comments on commit 0683110

Please sign in to comment.