Skip to content

Commit 4517a4b

Browse files
committed
fix: logger test issue
1 parent dfa8f9a commit 4517a4b

File tree

3 files changed

+72
-66
lines changed

3 files changed

+72
-66
lines changed

package-lock.json

Lines changed: 46 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@
5858
"@alicloud/ros-cdk-elasticsearchserverless": "^1.10.0",
5959
"@alicloud/ros-cdk-fc3": "^1.10.0",
6060
"@alicloud/ros-cdk-nas": "^1.10.0",
61-
"@alicloud/ros-cdk-oss": "^1.9.0",
62-
"@alicloud/ros-cdk-ossdeployment": "^1.9.0",
63-
"@alicloud/ros-cdk-ots": "^1.9.0",
61+
"@alicloud/ros-cdk-oss": "^1.10.0",
62+
"@alicloud/ros-cdk-ossdeployment": "^1.10.0",
63+
"@alicloud/ros-cdk-ots": "^1.10.0",
6464
"@alicloud/ros-cdk-ram": "^1.10.0",
65-
"@alicloud/ros-cdk-rds": "^1.9.0",
66-
"@alicloud/ros-cdk-ros": "^1.9.0",
67-
"@alicloud/ros-cdk-sls": "^1.9.0",
68-
"@alicloud/ros-cdk-vpc": "^1.9.0",
69-
"@alicloud/ros20190910": "^3.5.9",
65+
"@alicloud/ros-cdk-rds": "^1.10.0",
66+
"@alicloud/ros-cdk-ros": "^1.10.0",
67+
"@alicloud/ros-cdk-sls": "^1.10.0",
68+
"@alicloud/ros-cdk-vpc": "^1.10.0",
69+
"@alicloud/ros20190910": "^3.6.0",
7070
"ajv": "^8.17.1",
7171
"ali-oss": "^6.23.0",
7272
"chalk": "^5.6.2",

tests/common/rosClient.test.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { logger, rosStackDelete, rosStackDeploy } from '../../src/common';
1+
import { rosStackDelete, rosStackDeploy } from '../../src/common';
22
import { context } from '../fixtures/contextFixture';
33
import { lang } from '../../src/lang';
44

@@ -8,6 +8,9 @@ const mockedUpdateStack = jest.fn();
88
const mockedListStacks = jest.fn();
99
const mockedGetStack = jest.fn();
1010
const mockedDeleteStack = jest.fn();
11+
const mockedLoggerInfo = jest.fn();
12+
const mockedLoggerWarn = jest.fn();
13+
const mockedLoggerError = jest.fn();
1114

1215
jest.mock('node:async_hooks', () => ({
1316
AsyncLocalStorage: jest.fn().mockImplementation(() => ({
@@ -28,7 +31,13 @@ jest.mock('@alicloud/ros20190910', () => ({
2831
})),
2932
}));
3033

31-
jest.mock('../../src/common/logger');
34+
jest.mock('../../src/common/logger', () => ({
35+
logger: {
36+
info: (...args: unknown[]) => mockedLoggerInfo(...args),
37+
warn: (...args: unknown[]) => mockedLoggerWarn(...args),
38+
error: (...args: unknown[]) => mockedLoggerError(...args),
39+
},
40+
}));
3241

3342
describe('Unit test for rosClient', () => {
3443
beforeEach(() => {
@@ -46,7 +55,7 @@ describe('Unit test for rosClient', () => {
4655
await rosStackDeploy(stackName, {});
4756

4857
expect(mockedCreateStack).toHaveBeenCalled();
49-
expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('createStack success'));
58+
expect(mockedLoggerInfo).toHaveBeenCalledWith(expect.stringContaining('createStack success'));
5059
});
5160

5261
it('should update an existing stack if it exists', async () => {
@@ -62,7 +71,7 @@ describe('Unit test for rosClient', () => {
6271
await rosStackDeploy(stackName, {});
6372

6473
expect(mockedUpdateStack).toHaveBeenCalled();
65-
expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('stackUpdate success'));
74+
expect(mockedLoggerInfo).toHaveBeenCalledWith(expect.stringContaining('stackUpdate success'));
6675
});
6776

6877
it('should throw an error if the stack is in progress', async () => {
@@ -93,7 +102,7 @@ describe('Unit test for rosClient', () => {
93102

94103
await rosStackDeploy(stackName, {});
95104

96-
expect(logger.warn).toHaveBeenCalledWith(`${lang.__('UPDATE_COMPLETELY_SAME_STACK')}`);
105+
expect(mockedLoggerWarn).toHaveBeenCalledWith(`${lang.__('UPDATE_COMPLETELY_SAME_STACK')}`);
97106
});
98107

99108
it('should throw error when deploy stack failed', async () => {
@@ -121,15 +130,15 @@ describe('Unit test for rosClient', () => {
121130

122131
await rosStackDelete(context);
123132

124-
expect(logger.info).toHaveBeenCalledWith('stack status: DELETE_COMPLETE');
125-
expect(logger.info).toHaveBeenCalledWith('Stack: testStack deleted!🗑 ');
133+
expect(mockedLoggerInfo).toHaveBeenCalledWith('stack status: DELETE_COMPLETE');
134+
expect(mockedLoggerInfo).toHaveBeenCalledWith('Stack: testStack deleted!🗑 ');
126135
});
127136

128137
it('should throw an error when the stack does not exist', async () => {
129138
mockedListStacks.mockResolvedValue({ statusCode: 404, body: { stacks: [] } });
130139
await rosStackDelete(context);
131140

132-
expect(logger.warn).toHaveBeenCalledWith('Stack: testStack not exists, skipped! 🚫');
141+
expect(mockedLoggerWarn).toHaveBeenCalledWith('Stack: testStack not exists, skipped! 🚫');
133142
});
134143

135144
it('should throw error when delete stack failed', async () => {
@@ -142,7 +151,7 @@ describe('Unit test for rosClient', () => {
142151
await expect(rosStackDelete(context)).rejects.toThrow(
143152
JSON.stringify({ statusCode: 400, Message: 'DELETE_FAILED' }),
144153
);
145-
expect(logger.error).toHaveBeenCalledWith(
154+
expect(mockedLoggerError).toHaveBeenCalledWith(
146155
expect.stringContaining('Stack: testStack delete failed! ❌'),
147156
);
148157
});

0 commit comments

Comments
 (0)