@@ -8,6 +8,7 @@ import * as identifier from '@dd/apps-plugin/identifier';
88import * as uploader from '@dd/apps-plugin/upload' ;
99import { getPlugins } from '@dd/apps-plugin' ;
1010import * as fsHelpers from '@dd/core/helpers/fs' ;
11+ import type { PluginOptions } from '@dd/core/types' ;
1112import {
1213 getGetPluginsArg ,
1314 getMockBundler ,
@@ -20,6 +21,13 @@ import path from 'path';
2021
2122import { APPS_API_PATH } from './constants' ;
2223
24+ /** Extract and assert closeBundle from the first plugin's vite hooks. */
25+ function extractCloseBundle ( plugins : PluginOptions [ ] ) {
26+ const plugin = plugins [ 0 ] ;
27+ expect ( typeof plugin ?. vite ?. closeBundle ) . toBe ( 'function' ) ;
28+ return plugin . vite ! . closeBundle as ( ) => Promise < void > ;
29+ }
30+
2331describe ( 'Apps Plugin - getPlugins' , ( ) => {
2432 const buildRoot = '/project' ;
2533 const outDir = '/project/dist' ;
@@ -58,10 +66,8 @@ describe('Apps Plugin - getPlugins', () => {
5866 } ) ;
5967 jest . spyOn ( identifier , 'resolveIdentifier' ) . mockReturnValue ( { } ) ;
6068
61- const plugin = getPlugins ( getArgs ( ) ) [ 0 ] ;
62- await expect ( ( plugin as any ) . vite . closeBundle ( ) ) . rejects . toThrow (
63- 'Missing apps identification' ,
64- ) ;
69+ const closeBundle = extractCloseBundle ( getPlugins ( getArgs ( ) ) ) ;
70+ await expect ( closeBundle ( ) ) . rejects . toThrow ( 'Missing apps identification' ) ;
6571
6672 expect ( uploadSpy ) . not . toHaveBeenCalled ( ) ;
6773 expect ( collectSpy ) . not . toHaveBeenCalled ( ) ;
@@ -86,16 +92,18 @@ describe('Apps Plugin - getPlugins', () => {
8692 errors : [ ] ,
8793 warnings : [ ] ,
8894 } ) ;
89- const rmSpy = jest . spyOn ( fsHelpers , 'rm' ) . mockResolvedValue ( undefined as any ) ;
90-
91- const plugin = getPlugins (
92- getGetPluginsArg (
93- { apps : { include : [ 'public/**/*' ] } } ,
94- { bundler : { ...getMockBundler ( { name : 'vite' } ) , outDir } , buildRoot } ,
95+ const rmSpy = jest . spyOn ( fsHelpers , 'rm' ) . mockResolvedValue ( undefined ) ;
96+
97+ const closeBundle = extractCloseBundle (
98+ getPlugins (
99+ getGetPluginsArg (
100+ { apps : { include : [ 'public/**/*' ] } } ,
101+ { bundler : { ...getMockBundler ( { name : 'vite' } ) , outDir } , buildRoot } ,
102+ ) ,
95103 ) ,
96- ) [ 0 ] ;
104+ ) ;
97105
98- await ( plugin as any ) . vite . closeBundle ( ) ;
106+ await closeBundle ( ) ;
99107
100108 expect ( assets . collectAssets ) . toHaveBeenCalledWith ( [ 'public/**/*' , 'dist/**/*' ] , buildRoot ) ;
101109 expect ( archive . createArchive ) . not . toHaveBeenCalled ( ) ;
@@ -116,7 +124,7 @@ describe('Apps Plugin - getPlugins', () => {
116124 { absolutePath : '/project/dist/index.js' , relativePath : 'dist/index.js' } ,
117125 ] ;
118126 jest . spyOn ( assets , 'collectAssets' ) . mockResolvedValue ( mockedAssets ) ;
119- jest . spyOn ( fsHelpers , 'rm' ) . mockResolvedValue ( undefined as any ) ;
127+ jest . spyOn ( fsHelpers , 'rm' ) . mockResolvedValue ( undefined ) ;
120128 jest . spyOn ( archive , 'createArchive' ) . mockResolvedValue ( {
121129 archivePath : '/tmp/dd-apps-123/datadog-apps-assets.zip' ,
122130 assets : mockedAssets ,
@@ -127,8 +135,8 @@ describe('Apps Plugin - getPlugins', () => {
127135 warnings : [ 'first warning' ] ,
128136 } ) ;
129137
130- const plugin = getPlugins ( getArgs ( ) ) [ 0 ] ;
131- await ( plugin as any ) . vite . closeBundle ( ) ;
138+ const closeBundle = extractCloseBundle ( getPlugins ( getArgs ( ) ) ) ;
139+ await closeBundle ( ) ;
132140
133141 expect ( assets . collectAssets ) . toHaveBeenCalledWith ( [ 'dist/**/*' ] , buildRoot ) ;
134142 expect ( archive . createArchive ) . toHaveBeenCalledWith ( [
@@ -167,7 +175,7 @@ describe('Apps Plugin - getPlugins', () => {
167175 { absolutePath : '/project/dist/app.js' , relativePath : 'dist/app.js' } ,
168176 ] ;
169177 jest . spyOn ( assets , 'collectAssets' ) . mockResolvedValue ( mockedAssets ) ;
170- jest . spyOn ( fsHelpers , 'rm' ) . mockResolvedValue ( undefined as any ) ;
178+ jest . spyOn ( fsHelpers , 'rm' ) . mockResolvedValue ( undefined ) ;
171179 jest . spyOn ( archive , 'createArchive' ) . mockResolvedValue ( {
172180 archivePath : '/tmp/dd-apps-456/datadog-apps-assets.zip' ,
173181 assets : mockedAssets ,
@@ -178,8 +186,8 @@ describe('Apps Plugin - getPlugins', () => {
178186 warnings : [ ] ,
179187 } ) ;
180188
181- const plugin = getPlugins ( getArgs ( ) ) [ 0 ] ;
182- await expect ( ( plugin as any ) . vite . closeBundle ( ) ) . rejects . toThrow ( 'upload failed' ) ;
189+ const closeBundle = extractCloseBundle ( getPlugins ( getArgs ( ) ) ) ;
190+ await expect ( closeBundle ( ) ) . rejects . toThrow ( 'upload failed' ) ;
183191
184192 expect ( mockLogFn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'upload failed' ) , 'error' ) ;
185193 expect ( fsHelpers . rm ) . toHaveBeenCalledWith ( path . resolve ( '/tmp/dd-apps-456' ) ) ;
0 commit comments