11import { DEFAULT_SNAP_BUNDLE } from '@metamask/snaps-utils/test-utils' ;
22import fs from 'fs' ;
3+ import type { Compiler } from 'webpack' ;
4+ import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer' ;
35
46import { getMockConfig } from '../../test-utils' ;
57import { evaluate } from '../eval' ;
@@ -10,6 +12,10 @@ jest.mock('fs');
1012jest . mock ( '../eval' ) ;
1113jest . mock ( './implementation' ) ;
1214
15+ jest . mock ( 'webpack-bundle-analyzer' , ( ) => ( {
16+ BundleAnalyzerPlugin : jest . fn ( ) ,
17+ } ) ) ;
18+
1319describe ( 'buildHandler' , ( ) => {
1420 it ( 'builds a snap' , async ( ) => {
1521 await fs . promises . writeFile ( '/input.js' , DEFAULT_SNAP_BUNDLE ) ;
@@ -27,6 +33,7 @@ describe('buildHandler', () => {
2733
2834 expect ( process . exitCode ) . not . toBe ( 1 ) ;
2935 expect ( build ) . toHaveBeenCalledWith ( config , {
36+ analyze : false ,
3037 evaluate : false ,
3138 spinner : expect . any ( Object ) ,
3239 } ) ;
@@ -36,7 +43,54 @@ describe('buildHandler', () => {
3643 ) ;
3744 } ) ;
3845
39- it ( 'does note evaluate if the evaluate option is set to false' , async ( ) => {
46+ it ( 'analyzes a snap bundle' , async ( ) => {
47+ await fs . promises . writeFile ( '/input.js' , DEFAULT_SNAP_BUNDLE ) ;
48+
49+ jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
50+ const config = getMockConfig ( 'webpack' , {
51+ input : '/input.js' ,
52+ output : {
53+ path : '/foo' ,
54+ filename : 'output.js' ,
55+ } ,
56+ } ) ;
57+
58+ const compiler : Compiler = {
59+ // @ts -expect-error: Mock `Compiler` object.
60+ options : {
61+ plugins : [ new BundleAnalyzerPlugin ( ) ] ,
62+ } ,
63+ } ;
64+
65+ const plugin = jest . mocked ( BundleAnalyzerPlugin ) ;
66+ const instance = plugin . mock . instances [ 0 ] ;
67+
68+ // @ts -expect-error: Partial `server` mock.
69+ instance . server = Promise . resolve ( {
70+ http : {
71+ address : ( ) => 'http://localhost:8888' ,
72+ } ,
73+ } ) ;
74+
75+ jest . mocked ( build ) . mockResolvedValueOnce ( compiler ) ;
76+
77+ await buildHandler ( config , true ) ;
78+
79+ expect ( process . exitCode ) . not . toBe ( 1 ) ;
80+ expect ( build ) . toHaveBeenCalledWith ( config , {
81+ analyze : true ,
82+ evaluate : false ,
83+ spinner : expect . any ( Object ) ,
84+ } ) ;
85+
86+ expect ( console . log ) . toHaveBeenCalledWith (
87+ expect . stringContaining (
88+ 'Bundle analyzer running at http://localhost:8888.' ,
89+ ) ,
90+ ) ;
91+ } ) ;
92+
93+ it ( 'does not evaluate if the evaluate option is set to false' , async ( ) => {
4094 await fs . promises . writeFile ( '/input.js' , DEFAULT_SNAP_BUNDLE ) ;
4195
4296 jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
0 commit comments