11import Bugsnag from '@bugsnag/cli'
22import { resolve } from 'path'
33import { build } from 'vite'
4- import { describe , expect , test , vi } from 'vitest'
4+ import { beforeEach , describe , expect , test , vi } from 'vitest'
55import { BugsnagSourceMapUploaderPlugin } from '../src/source-map-uploader-plugin'
66import cleanBuildDir from './lib/clean-build-dir'
77
@@ -16,6 +16,10 @@ vi.mock('@bugsnag/cli', () => ({
1616} ) )
1717
1818describe ( 'BugsnagSourceMapUploaderPlugin' , ( ) => {
19+ beforeEach ( ( ) => {
20+ vi . mocked ( Bugsnag . Upload . Js ) . mockClear ( )
21+ } )
22+
1923 test ( 'should return a valid plugin object' , async ( ) => {
2024 const plugin = BugsnagSourceMapUploaderPlugin ( {
2125 apiKey : 'test-api' ,
@@ -63,23 +67,22 @@ describe('BugsnagSourceMapUploaderPlugin', () => {
6367
6468 const sourcemapUpload = vi . mocked ( Bugsnag . Upload . Js )
6569 const outputDir = resolve ( fixturesPath , 'dist' )
66- const bundlePath = resolve ( outputDir , 'assets/index-DTHX3LI9.js' )
67- const sourceMapPath = resolve ( outputDir , 'assets/index-DTHX3LI9.js.map' )
6870
6971 expect ( mockLogger . info ) . toHaveBeenCalledWith ( '[BugsnagSourceMapUploaderPlugin] uploading sourcemaps using the bugsnag-cli' )
7072 expect ( mockLogger . info ) . toHaveBeenCalledWith ( '[BugsnagSourceMapUploaderPlugin] Sourcemaps uploaded successfully' )
71- expect ( sourcemapUpload ) . toHaveBeenCalledExactlyOnceWith ( {
72- apiKey : 'test-api' ,
73- bundleUrl : 'https://bugsnag.com/assets/index-DTHX3LI9.js' ,
74- bundle : bundlePath ,
75- projectRoot : fixturesPath ,
76- sourceMap : sourceMapPath ,
77- versionName : '1.0.0'
78- } ,
79- outputDir
80- )
73+ expect ( sourcemapUpload ) . toHaveBeenCalledOnce ( )
8174
82- sourcemapUpload . mockClear ( )
75+ const [ uploadOptions , targetDir ] = sourcemapUpload . mock . calls [ 0 ]
76+ expect ( uploadOptions ) . toBeDefined ( )
77+ expect ( uploadOptions ) . toMatchObject ( {
78+ apiKey : 'test-api' ,
79+ projectRoot : fixturesPath ,
80+ versionName : '1.0.0'
81+ } )
82+ expect ( uploadOptions ! . bundleUrl ) . toMatch ( / ^ h t t p s : \/ \/ b u g s n a g \. c o m \/ a s s e t s \/ i n d e x - [ a - z A - Z 0 - 9 ] + \. j s $ / )
83+ expect ( uploadOptions ! . bundle ) . toMatch ( / \/ a s s e t s \/ i n d e x - [ a - z A - Z 0 - 9 ] + \. j s $ / )
84+ expect ( uploadOptions ! . sourceMap ) . toMatch ( / \/ a s s e t s \/ i n d e x - [ a - z A - Z 0 - 9 ] + \. j s \. m a p $ / )
85+ expect ( targetDir ) . toBe ( outputDir )
8386 } )
8487
8588 test ( 'should use the relative filepath for bundleUrl if base is not provided in config' , async ( ) => {
@@ -108,23 +111,22 @@ describe('BugsnagSourceMapUploaderPlugin', () => {
108111
109112 const sourcemapUpload = vi . mocked ( Bugsnag . Upload . Js )
110113 const outputDir = resolve ( fixturePath , 'dist' )
111- const bundlePath = resolve ( outputDir , 'assets/index-DTHX3LI9.js' )
112- const sourceMapPath = resolve ( outputDir , 'assets/index-DTHX3LI9.js.map' )
113114
114115 expect ( mockLogger . info ) . toHaveBeenCalledWith ( '[BugsnagSourceMapUploaderPlugin] uploading sourcemaps using the bugsnag-cli' )
115116 expect ( mockLogger . info ) . toHaveBeenCalledWith ( '[BugsnagSourceMapUploaderPlugin] Sourcemaps uploaded successfully' )
116- expect ( sourcemapUpload ) . toHaveBeenCalledExactlyOnceWith ( {
117- apiKey : 'test-api' ,
118- bundleUrl : '/assets/index-DTHX3LI9.js' ,
119- bundle : bundlePath ,
120- projectRoot : fixturePath ,
121- sourceMap : sourceMapPath ,
122- versionName : version
123- } ,
124- outputDir
125- )
126-
127- sourcemapUpload . mockClear ( )
117+ expect ( sourcemapUpload ) . toHaveBeenCalledOnce ( )
118+
119+ const [ uploadOptions , targetDir ] = sourcemapUpload . mock . calls [ 0 ]
120+ expect ( uploadOptions ) . toBeDefined ( )
121+ expect ( uploadOptions ) . toMatchObject ( {
122+ apiKey : 'test-api' ,
123+ projectRoot : fixturePath ,
124+ versionName : version
125+ } )
126+ expect ( uploadOptions ! . bundleUrl ) . toMatch ( / ^ \/ a s s e t s \/ i n d e x - [ a - z A - Z 0 - 9 ] + \. j s $ / )
127+ expect ( uploadOptions ! . bundle ) . toMatch ( / \/ a s s e t s \/ i n d e x - [ a - z A - Z 0 - 9 ] + \. j s $ / )
128+ expect ( uploadOptions ! . sourceMap ) . toMatch ( / \/ a s s e t s \/ i n d e x - [ a - z A - Z 0 - 9 ] + \. j s \. m a p $ / )
129+ expect ( targetDir ) . toBe ( outputDir )
128130 } )
129131
130132 test ( 'logs an error if the upload fails' , async ( ) => {
@@ -159,7 +161,5 @@ describe('BugsnagSourceMapUploaderPlugin', () => {
159161 expect ( mockLogger . info ) . toHaveBeenCalledWith ( '[BugsnagSourceMapUploaderPlugin] uploading sourcemaps using the bugsnag-cli' )
160162 expect ( mockLogger . error ) . toHaveBeenCalledWith ( '[BugsnagSourceMapUploaderPlugin] Error: Upload failed' )
161163 expect ( mockLogger . info ) . not . toHaveBeenCalledWith ( '[BugsnagSourceMapUploaderPlugin] Sourcemaps uploaded successfully' )
162-
163- sourcemapUpload . mockClear ( )
164164 } )
165165} )
0 commit comments