Skip to content

Commit b0c1490

Browse files
committed
fix: fix unit tesst
1 parent 5adb4a6 commit b0c1490

File tree

6 files changed

+30
-18
lines changed

6 files changed

+30
-18
lines changed

src/commands/template.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export const template = async (
1111
const iac = parseYaml(getIacLocation(options.location));
1212

1313
await setContext({ ...options, stackName, provider: iac.provider.name }, true);
14-
1514
const { template } = generateStackTemplate(stackName, iac);
1615
if (typeof template === 'string') {
1716
logger.info(`\n${template}`);

src/stack/rosStack/bootstrap.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ module.exports.handler = async (rawEvent, context) => {
4747
// 处理 Buffer 类型的事件
4848
const event = parseEvent(rawEvent);
4949
50-
console.log('Parsed Event:', JSON.stringify(event, null, 2));
51-
console.log('Context:', JSON.stringify(context, null, 2));
52-
5350
const commonResponse = {
5451
RequestId: event.requestId,
5552
LogicalResourceId: event.logicalResourceId,
@@ -74,8 +71,6 @@ module.exports.handler = async (rawEvent, context) => {
7471
delete rosResponse.Data;
7572
}
7673
77-
console.log('Constructed ROS Response:', JSON.stringify(rosResponse, null, 2));
78-
7974
// 发送响应到 ROS 服务(如果提供了 ResponseURL)
8075
if (event.responseURL) {
8176
await sendResponse(event.responseURL, rosResponse);
@@ -123,7 +118,6 @@ module.exports.handler = async (rawEvent, context) => {
123118
124119
// 使用原生 fetch API 发送响应
125120
async function sendResponse(responseUrl, responseBody) {
126-
console.log(\`Sending response to: \${responseUrl}\`);
127121
128122
try {
129123
const body = JSON.stringify(responseBody);

tests/commands/deploy.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import { deploy } from '../../src/commands/deploy';
22

3+
const mockedIamInfo = jest.fn();
34
const mockedDeployStack = jest.fn();
5+
6+
jest.mock('../../src/common/imsClient', () => ({
7+
...jest.requireActual('../../src/common/imsClient'),
8+
getIamInfo: (...rags: unknown[]) => mockedIamInfo(...rags),
9+
}));
10+
411
jest.mock('../../src/stack', () => ({
512
...jest.requireActual('../../src/stack'),
613
deployStack: (...args: unknown[]) => mockedDeployStack(...args),
714
}));
815

916
describe('unit test for deploy command', () => {
1017
it('should construct valid context and deploy the stack when deploy with valid iac', async () => {
18+
mockedIamInfo.mockResolvedValue({ accountId: '123456789012', region: 'cn-hangzhou' });
19+
1120
const stackName = 'my-demo-stack';
1221

1322
await deploy(stackName, {

tests/commands/template.test.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,45 @@ import { TemplateFormat } from '../../src/types';
33
import { jsonTemplate, yamlTemplate } from '../fixtures/templateFixture';
44

55
const mockedLogger = jest.fn();
6+
const mockedIamInfo = jest.fn();
7+
68
jest.mock('../../src/common/logger', () => ({
79
logger: { info: (...args: unknown[]) => mockedLogger(...args), debug: jest.fn() },
810
}));
11+
12+
jest.mock('../../src/common/imsClient', () => ({
13+
getIamInfo: (...rags: unknown[]) => mockedIamInfo(...rags),
14+
}));
15+
916
const stackName = 'printTemplateStack';
1017
const location = 'tests/fixtures/serverless-insight.yml';
1118

1219
describe('Unit test for template command', () => {
1320
beforeEach(() => {
21+
mockedIamInfo.mockResolvedValue({ accountId: '123456789012', region: 'cn-hangzhou' });
22+
});
23+
24+
afterEach(() => {
1425
mockedLogger.mockRestore();
26+
mockedIamInfo.mockRestore();
1527
});
16-
it('should print the template in JSON format by default', () => {
28+
29+
it('should print the template in JSON format by default', async () => {
1730
const options = {
1831
format: TemplateFormat.JSON,
1932
location,
2033
stage: undefined,
2134
};
2235

23-
template(stackName, options);
36+
await template(stackName, options);
2437

2538
expect(mockedLogger).toHaveBeenCalledWith(`\n${JSON.stringify(jsonTemplate, null, 2)}`);
2639
});
2740

28-
it('should print the template in YAML format when specified', () => {
41+
it('should print the template in YAML format when specified', async () => {
2942
const options = { format: TemplateFormat.YAML, location, stage: undefined };
3043

31-
template(stackName, options);
44+
await template(stackName, options);
3245

3346
expect(mockedLogger).toHaveBeenCalledWith(yamlTemplate);
3447
});

tests/fixtures/deploy-fixtures/oneFcLargeCode.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export const largeCodeRos = {
210210
ServiceConfig: {
211211
FunctionComputeConfig: {
212212
FcVersion: '3.0',
213+
FcRegionId: 'cn-hangzhou',
213214
FunctionName: {
214215
'Fn::GetAtt': ['hello_fn', 'FunctionName'],
215216
},
@@ -234,9 +235,7 @@ export const largeCodeRos = {
234235
DependsOn: ['sls_project', 'sls_logstore', 'sls_index', 'si_auto_artifacts_code_deployment'],
235236
Properties: {
236237
Code: {
237-
OssBucketName: {
238-
'Fn::Sub': 'si-bootstrap-artifacts-${ALIYUN::AccountId}-${ALIYUN::Region}',
239-
},
238+
OssBucketName: 'si-bootstrap-artifacts-123456789012-cn-hangzhou',
240239
OssObjectName: 'hello-fn/43cb4c356149762dbe507fc1baede172-large-artifact.zip',
241240
},
242241
EnvironmentVariables: {
@@ -262,9 +261,7 @@ export const largeCodeRos = {
262261
si_auto_artifacts_code_deployment: {
263262
Properties: {
264263
Parameters: {
265-
destinationBucket: {
266-
'Fn::Sub': 'si-bootstrap-artifacts-${ALIYUN::AccountId}-${ALIYUN::Region}',
267-
},
264+
destinationBucket: 'si-bootstrap-artifacts-123456789012-cn-hangzhou',
268265
retainOnCreate: false,
269266
sources: [
270267
{

tests/stack/deploy.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('Unit tests for stack deployment', () => {
132132

133133
it('should create bucket and store code artifact to bucket when code size > 15MB', async () => {
134134
const stackName = 'my-large-code-stack';
135-
mockedGetStore.mockReturnValue({ stackName });
135+
mockedGetStore.mockReturnValue({ stackName, accountId: '123456789012', region: 'cn-hangzhou' });
136136
mockedRosStackDeploy.mockResolvedValueOnce(stackName);
137137

138138
await deployStack(

0 commit comments

Comments
 (0)