Skip to content

Commit

Permalink
chore(cdk): remove mockery (#27242)
Browse files Browse the repository at this point in the history
Replace usage of mockery in some tests with a more modern way.

Closes https://github.com/aws/aws-cdk/security/dependabot/181

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
mrgrain authored Sep 21, 2023
1 parent 40d304e commit 54537c5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/aws-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@
"constructs": "^10.0.0",
"fast-check": "^3.13.0",
"jest": "^29.7.0",
"jest-mock": "^29.7.0",
"madge": "^5.0.2",
"make-runnable": "^1.4.1",
"mockery": "^2.1.0",
"nock": "^13.3.3",
"sinon": "^9.2.4",
"ts-jest": "^29.1.1",
Expand Down
36 changes: 12 additions & 24 deletions packages/aws-cdk/test/cdk-docs.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,30 @@
/* eslint-disable import/order */
import * as mockery from 'mockery';
import * as child_process from 'child_process';
import { mocked } from 'jest-mock';
import { CommandHandler } from '../lib/command-api';
import { realHandler } from '../lib/commands/docs';

const argv = {
browser: 'echo %u',
commandHandler: undefined as (CommandHandler | undefined),
};

describe('`cdk docs`', () => {
beforeEach(done => {
mockery.registerMock('../../lib/logging', {
debug() { return; },
error() { return; },
print() { return; },
warning() { return; },
});
mockery.enable({ useCleanCache: true, warnOnReplace: true, warnOnUnregistered: false });
done();
});
// eslint-disable-next-line no-console
console.log = jest.fn();
jest.mock('child_process');

afterAll(done => {
mockery.disable();
mockery.deregisterAll();
done();
});
describe('`cdk docs`', () => {

test('exits with 0 when everything is OK', async () => {
const mockChildProcessExec: any = (_: string, cb: (err?: Error, stdout?: string, stderr?: string) => void) => cb();
mocked(child_process.exec).mockImplementation(mockChildProcessExec);

const result = await realHandler({ args: argv } as any);
expect(result).toBe(0);
});

test('exits with 0 when opening the browser fails', async () => {
mockery.registerMock('child_process', {
exec(_: string, cb: (err: Error, stdout?: string, stderr?: string) => void) {
cb(new Error('TEST'));
},
});
const mockChildProcessExec: any = (_: string, cb: (err: Error, stdout?: string, stderr?: string) => void) => cb(new Error('TEST'));
mocked(child_process.exec).mockImplementation(mockChildProcessExec);

const result = await realHandler({ args: argv } as any);
expect(result).toBe(0);
});
Expand Down
19 changes: 3 additions & 16 deletions packages/aws-cdk/test/cdk-doctor.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
/* eslint-disable import/order */
import * as mockery from 'mockery';
import { realHandler } from '../lib/commands/doctor';

describe('`cdk doctor`', () => {
beforeEach(done => {
mockery.registerMock('../../lib/logging', {
print: () => undefined,
});
mockery.enable({ useCleanCache: true, warnOnReplace: true, warnOnUnregistered: false });
done();
});

afterEach(done => {
mockery.disable();
mockery.deregisterAll();
done();
});
// eslint-disable-next-line no-console
console.log = jest.fn();

describe('`cdk doctor`', () => {
test('exits with 0 when everything is OK', async () => {
const argv: any = {};
const result = await realHandler({ args: argv } as any);
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10423,11 +10423,6 @@ mock-fs@^4.14.0:
resolved "https://registry.npmjs.org/mock-fs/-/mock-fs-4.14.0.tgz#ce5124d2c601421255985e6e94da80a7357b1b18"
integrity sha512-qYvlv/exQ4+svI3UOvPUpLDF0OMX5euvUH0Ny4N5QyRyhNdgAgUrVH3iUINSzEPLvx0kbo/Bp28GJKIqvE7URw==

mockery@^2.1.0:
version "2.1.0"
resolved "https://registry.npmjs.org/mockery/-/mockery-2.1.0.tgz#5b0aef1ff564f0f8139445e165536c7909713470"
integrity sha512-9VkOmxKlWXoDO/h1jDZaS4lH33aWfRiJiNT/tKj+8OGzrcFDLo8d0syGdbsc3Bc4GvRXPb+NMMvojotmuGJTvA==

modify-values@^1.0.0, modify-values@^1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
Expand Down

0 comments on commit 54537c5

Please sign in to comment.