Skip to content

Commit 54537c5

Browse files
authored
chore(cdk): remove mockery (#27242)
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*
1 parent 40d304e commit 54537c5

File tree

4 files changed

+16
-46
lines changed

4 files changed

+16
-46
lines changed

packages/aws-cdk/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@
8686
"constructs": "^10.0.0",
8787
"fast-check": "^3.13.0",
8888
"jest": "^29.7.0",
89+
"jest-mock": "^29.7.0",
8990
"madge": "^5.0.2",
9091
"make-runnable": "^1.4.1",
91-
"mockery": "^2.1.0",
9292
"nock": "^13.3.3",
9393
"sinon": "^9.2.4",
9494
"ts-jest": "^29.1.1",

packages/aws-cdk/test/cdk-docs.test.ts

+12-24
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,30 @@
1-
/* eslint-disable import/order */
2-
import * as mockery from 'mockery';
1+
import * as child_process from 'child_process';
2+
import { mocked } from 'jest-mock';
33
import { CommandHandler } from '../lib/command-api';
44
import { realHandler } from '../lib/commands/docs';
5-
65
const argv = {
76
browser: 'echo %u',
87
commandHandler: undefined as (CommandHandler | undefined),
98
};
109

11-
describe('`cdk docs`', () => {
12-
beforeEach(done => {
13-
mockery.registerMock('../../lib/logging', {
14-
debug() { return; },
15-
error() { return; },
16-
print() { return; },
17-
warning() { return; },
18-
});
19-
mockery.enable({ useCleanCache: true, warnOnReplace: true, warnOnUnregistered: false });
20-
done();
21-
});
10+
// eslint-disable-next-line no-console
11+
console.log = jest.fn();
12+
jest.mock('child_process');
2213

23-
afterAll(done => {
24-
mockery.disable();
25-
mockery.deregisterAll();
26-
done();
27-
});
14+
describe('`cdk docs`', () => {
2815

2916
test('exits with 0 when everything is OK', async () => {
17+
const mockChildProcessExec: any = (_: string, cb: (err?: Error, stdout?: string, stderr?: string) => void) => cb();
18+
mocked(child_process.exec).mockImplementation(mockChildProcessExec);
19+
3020
const result = await realHandler({ args: argv } as any);
3121
expect(result).toBe(0);
3222
});
3323

3424
test('exits with 0 when opening the browser fails', async () => {
35-
mockery.registerMock('child_process', {
36-
exec(_: string, cb: (err: Error, stdout?: string, stderr?: string) => void) {
37-
cb(new Error('TEST'));
38-
},
39-
});
25+
const mockChildProcessExec: any = (_: string, cb: (err: Error, stdout?: string, stderr?: string) => void) => cb(new Error('TEST'));
26+
mocked(child_process.exec).mockImplementation(mockChildProcessExec);
27+
4028
const result = await realHandler({ args: argv } as any);
4129
expect(result).toBe(0);
4230
});

packages/aws-cdk/test/cdk-doctor.test.ts

+3-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
/* eslint-disable import/order */
2-
import * as mockery from 'mockery';
31
import { realHandler } from '../lib/commands/doctor';
42

5-
describe('`cdk doctor`', () => {
6-
beforeEach(done => {
7-
mockery.registerMock('../../lib/logging', {
8-
print: () => undefined,
9-
});
10-
mockery.enable({ useCleanCache: true, warnOnReplace: true, warnOnUnregistered: false });
11-
done();
12-
});
13-
14-
afterEach(done => {
15-
mockery.disable();
16-
mockery.deregisterAll();
17-
done();
18-
});
3+
// eslint-disable-next-line no-console
4+
console.log = jest.fn();
195

6+
describe('`cdk doctor`', () => {
207
test('exits with 0 when everything is OK', async () => {
218
const argv: any = {};
229
const result = await realHandler({ args: argv } as any);

yarn.lock

-5
Original file line numberDiff line numberDiff line change
@@ -10423,11 +10423,6 @@ mock-fs@^4.14.0:
1042310423
resolved "https://registry.npmjs.org/mock-fs/-/mock-fs-4.14.0.tgz#ce5124d2c601421255985e6e94da80a7357b1b18"
1042410424
integrity sha512-qYvlv/exQ4+svI3UOvPUpLDF0OMX5euvUH0Ny4N5QyRyhNdgAgUrVH3iUINSzEPLvx0kbo/Bp28GJKIqvE7URw==
1042510425

10426-
mockery@^2.1.0:
10427-
version "2.1.0"
10428-
resolved "https://registry.npmjs.org/mockery/-/mockery-2.1.0.tgz#5b0aef1ff564f0f8139445e165536c7909713470"
10429-
integrity sha512-9VkOmxKlWXoDO/h1jDZaS4lH33aWfRiJiNT/tKj+8OGzrcFDLo8d0syGdbsc3Bc4GvRXPb+NMMvojotmuGJTvA==
10430-
1043110426
modify-values@^1.0.0, modify-values@^1.0.1:
1043210427
version "1.0.1"
1043310428
resolved "https://registry.npmjs.org/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"

0 commit comments

Comments
 (0)