Skip to content

Commit

Permalink
use testiohost
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizencc committed Jan 17, 2025
1 parent 3dad7c6 commit 82466be
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
12 changes: 8 additions & 4 deletions packages/@aws-cdk/toolkit/test/_helpers/test-io-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ import { IIoHost, IoMessage, IoMessageLevel, IoRequest, isMessageRelevantForLeve
* Optional set a level to filter out all irrelevant messages.
*/
export class TestIoHost implements IIoHost {
public readonly spy: jest.Mock<any, any, any>;
public readonly notifySpy: jest.Mock<any, any, any>;
public readonly requestSpy: jest.Mock<any, any, any>;

constructor(public level: IoMessageLevel = 'info') {
this.spy = jest.fn();
this.notifySpy = jest.fn();
this.requestSpy = jest.fn();
}

public async notify<T>(msg: IoMessage<T>): Promise<void> {
if (isMessageRelevantForLevel(msg, this.level)) {
this.spy(msg);
this.notifySpy(msg);
}
}

public async requestResponse<T, U>(msg: IoRequest<T, U>): Promise<U> {
await this.notify(msg);
if (isMessageRelevantForLevel(msg, this.level)) {
this.requestSpy(msg);
}
return msg.defaultResponse;
}
}
11 changes: 4 additions & 7 deletions packages/@aws-cdk/toolkit/test/actions/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { Toolkit } from '../../lib/toolkit';
import { TestIoHost } from '../_helpers';

const ioHost = new TestIoHost();
const notifySpy = jest.spyOn(ioHost, 'notify');
const requestResponseSpy = jest.spyOn(ioHost, 'requestResponse');
const cdk = new Toolkit({ ioHost });

jest.mock('../../lib/api/aws-cdk', () => {
Expand Down Expand Up @@ -38,8 +36,7 @@ const cxFromBuilder = async () => {
};

beforeEach(() => {
requestResponseSpy.mockClear();
notifySpy.mockClear();
ioHost.notifySpy.mockClear();
jest.clearAllMocks();
});

Expand All @@ -50,7 +47,7 @@ describe('deploy', () => {
await cdk.deploy(cx);

// THEN
expect(notifySpy).toHaveBeenCalledWith(expect.objectContaining({
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({
action: 'deploy',
level: 'info',
message: expect.stringContaining('Deployment time:'),
Expand All @@ -73,7 +70,7 @@ describe('deploy', () => {
});

// THEN
expect(requestResponseSpy).toHaveBeenCalledWith(expect.objectContaining({
expect(ioHost.requestSpy).toHaveBeenCalledWith(expect.objectContaining({
action: 'deploy',
level: 'info',
code: 'CDK_TOOLKIT_I5060',
Expand All @@ -97,7 +94,7 @@ describe('deploy', () => {
});

// THEN
expect(requestResponseSpy).not.toHaveBeenCalledWith(expect.objectContaining({
expect(ioHost.requestSpy).not.toHaveBeenCalledWith(expect.objectContaining({
action: 'deploy',
level: 'info',
code: 'CDK_TOOLKIT_I5060',
Expand Down
11 changes: 6 additions & 5 deletions packages/@aws-cdk/toolkit/test/actions/synth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const ioHost = new TestIoHost();
const toolkit = new Toolkit({ ioHost });

beforeEach(() => {
ioHost.spy.mockClear();
ioHost.notifySpy.mockClear();
ioHost.requestSpy.mockClear();
});

describe('synth', () => {
Expand All @@ -15,7 +16,7 @@ describe('synth', () => {
await toolkit.synth(cx);

// THEN
expect(ioHost.spy).toHaveBeenCalledWith(expect.objectContaining({
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({
action: 'synth',
level: 'info',
message: expect.stringContaining('Successfully synthesized'),
Expand All @@ -28,7 +29,7 @@ describe('synth', () => {
await toolkit.synth(cx);

// THEN
expect(ioHost.spy).toHaveBeenCalledWith(expect.objectContaining({
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({
action: 'synth',
level: 'info',
message: expect.stringContaining('Successfully synthesized'),
Expand All @@ -41,7 +42,7 @@ describe('synth', () => {
await toolkit.synth(cx);

// THEN
expect(ioHost.spy).toHaveBeenCalledWith(expect.objectContaining({
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({
action: 'synth',
level: 'info',
code: 'CDK_TOOLKIT_I0001',
Expand All @@ -62,7 +63,7 @@ describe('synth', () => {
await toolkit.synth(await appFixture(toolkit, 'two-empty-stacks'));

// THEN
expect(ioHost.spy).toHaveBeenCalledWith(expect.objectContaining({
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({
action: 'synth',
level: 'info',
code: 'CDK_TOOLKIT_I0002',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const ioHost = new TestIoHost();
const toolkit = new Toolkit({ ioHost });

beforeEach(() => {
ioHost.spy.mockClear();
ioHost.notifySpy.mockClear();
ioHost.requestSpy.mockClear();
});

describe('fromAssemblyBuilder', () => {
Expand Down

0 comments on commit 82466be

Please sign in to comment.