@@ -3,32 +3,56 @@ import { TemplateFormat } from '../../src/types';
33import { jsonTemplate , yamlTemplate } from '../fixtures/templateFixture' ;
44
55const mockedLogger = jest . fn ( ) ;
6+ const mockedGetContext = jest . fn ( ) ;
7+
68jest . mock ( '../../src/common/logger' , ( ) => ( {
79 logger : { info : ( ...args : unknown [ ] ) => mockedLogger ( ...args ) , debug : jest . fn ( ) } ,
810} ) ) ;
11+
12+ jest . mock ( '../../src/common/imsClient' , ( ) => ( {
13+ getIamInfo : jest . fn ( ) . mockResolvedValue ( { accountId : '123456789012' , region : 'cn-hangzhou' } ) ,
14+ } ) ) ;
15+
16+ jest . mock ( '../../src/common/context' , ( ) => ( {
17+ ...jest . requireActual ( '../../src/common/context' ) ,
18+ getContext : ( ...args : unknown [ ] ) => mockedGetContext ( ...args ) ,
19+ } ) ) ;
20+
921const stackName = 'printTemplateStack' ;
1022const location = 'tests/fixtures/serverless-insight.yml' ;
1123
1224describe ( 'Unit test for template command' , ( ) => {
1325 beforeEach ( ( ) => {
26+ mockedGetContext . mockReturnValue ( {
27+ accountId : '123456789012' ,
28+ region : 'cn-hangzhou' ,
29+ stage : 'default' ,
30+ stackName,
31+ provider : 'aliyun' ,
32+ } ) ;
33+ } ) ;
34+
35+ afterEach ( ( ) => {
1436 mockedLogger . mockRestore ( ) ;
37+ mockedGetContext . mockRestore ( ) ;
1538 } ) ;
16- it ( 'should print the template in JSON format by default' , ( ) => {
39+
40+ it ( 'should print the template in JSON format by default' , async ( ) => {
1741 const options = {
1842 format : TemplateFormat . JSON ,
1943 location,
2044 stage : undefined ,
2145 } ;
2246
23- template ( stackName , options ) ;
47+ await template ( stackName , options ) ;
2448
2549 expect ( mockedLogger ) . toHaveBeenCalledWith ( `\n${ JSON . stringify ( jsonTemplate , null , 2 ) } ` ) ;
2650 } ) ;
2751
28- it ( 'should print the template in YAML format when specified' , ( ) => {
52+ it ( 'should print the template in YAML format when specified' , async ( ) => {
2953 const options = { format : TemplateFormat . YAML , location, stage : undefined } ;
3054
31- template ( stackName , options ) ;
55+ await template ( stackName , options ) ;
3256
3357 expect ( mockedLogger ) . toHaveBeenCalledWith ( yamlTemplate ) ;
3458 } ) ;
0 commit comments