Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli-lib-alpha): slightly more idiomatic use of mocks #32512

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions packages/@aws-cdk/cli-lib-alpha/test/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import { join } from 'path';
import * as core from 'aws-cdk-lib/core';
import * as cli from 'aws-cdk/lib';
import * as core from 'aws-cdk-lib';
Copy link
Contributor

@mrgrain mrgrain Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is was importing aws-cdk-lib/core on purpose. Curios why it needs changing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this doesn't seem to work in the new monorepo, I assume because aws-cdk-lib has been bundled.

Why must it import the submodule directly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it is best practice to import submodules and if that's not working anymore in the new monorepo that is extremely concerning to me. Especially if we don't understand why it is not working anymore.

import { AwsCdkCli } from '../lib';

// These tests synthesize an actual CDK app and take a bit longer
jest.setTimeout(60_000);

jest.mock('aws-cdk/lib', () => {
const original = jest.requireActual('aws-cdk/lib');
return {
...original,
exec: jest.fn(original.exec),
};
});
const stdoutMock = jest.spyOn(process.stdout, 'write').mockImplementation(() => { return true; });
let stdoutMock = jest.spyOn(process.stdout, 'write').mockReturnValue(true);
let exec = jest.spyOn(cli, 'exec');

beforeEach(() => {
stdoutMock.mockClear();
jest.mocked(cli.exec).mockClear();
jest.clearAllMocks();
});

afterAll(() => jest.clearAllMocks());

describe('fromCloudAssemblyDirectoryProducer', () => {
const testEnv = jest.fn();
const cdk = AwsCdkCli.fromCloudAssemblyDirectoryProducer({
@@ -36,16 +27,12 @@ describe('fromCloudAssemblyDirectoryProducer', () => {
},
});

beforeEach(() => {
testEnv.mockClear();
});

test('can list all stacks in app', async () => {
// WHEN
await cdk.list();

// THEN
expect(jest.mocked(cli.exec)).toHaveBeenCalledWith(
expect(exec).toHaveBeenCalledWith(
['ls', '--all'],
expect.anything(),
);
@@ -78,7 +65,7 @@ describe('fromDirectory', () => {
await cdk.list();

// THEN
expect(jest.mocked(cli.exec)).toHaveBeenCalledWith(
expect(exec).toHaveBeenCalledWith(
['ls', '--all'],
);
expect(stdoutMock.mock.calls[0][0]).toContain('AppStack1');
@@ -97,7 +84,7 @@ describe('fromDirectory with config', () => {
await cdk.list();

// THEN
expect(jest.mocked(cli.exec)).toHaveBeenCalledWith(
expect(exec).toHaveBeenCalledWith(
[
'ls', '--all',
'--app', 'node -r ts-node/register app.ts',
Loading