11import { Ok , SourceProcessor , SymbolUploader , ZipArchive } from '@backtrace/sourcemap-tools' ;
22import assert from 'assert' ;
33import crypto from 'crypto' ;
4- import fs from 'fs' ;
54import path from 'path' ;
5+ import { Readable } from 'stream' ;
66import webpack from 'webpack' ;
77import { asyncWebpack , expectSuccess , getFiles , removeDir , webpackModeTest } from './helpers' ;
88
@@ -19,9 +19,16 @@ export function createE2ETest(configBuilder: (mode: webpack.Configuration['mode'
1919 }
2020
2121 function mockProcessor ( ) {
22- return jest
23- . spyOn ( SourceProcessor . prototype , 'processSourceAndSourceMapFiles' )
24- . mockImplementation ( async ( _ , __ , debugId ) => Ok ( { debugId : debugId ?? 'debugId' } as never ) ) ;
22+ return jest . spyOn ( SourceProcessor . prototype , 'processSourceAndSourceMapFiles' ) ;
23+ // .mockImplementation(async (sourcePath, sourceMapPath, debugId) =>
24+ // Ok({
25+ // debugId: debugId ?? 'debugId',
26+ // sourcePath,
27+ // sourceMapPath: sourceMapPath ?? sourcePath + '.map',
28+ // source: '',
29+ // sourceMap: {} as never,
30+ // }),
31+ // );
2532 }
2633
2734 function mockZipArchiveAppend ( ) {
@@ -34,7 +41,7 @@ export function createE2ETest(configBuilder: (mode: webpack.Configuration['mode'
3441 let zipArchiveAppendSpy : ReturnType < typeof mockZipArchiveAppend > ;
3542
3643 beforeAll ( async ( ) => {
37- jest . resetAllMocks ( ) ;
44+ jest . restoreAllMocks ( ) ;
3845
3946 uploadSpy = mockUploader ( ) ;
4047 processSpy = mockProcessor ( ) ;
@@ -50,44 +57,35 @@ export function createE2ETest(configBuilder: (mode: webpack.Configuration['mode'
5057 result = webpackResult ;
5158 } , 120000 ) ;
5259
53- it ( 'should call SourceProcessor for every emitted source file and sourcemap pair ' , async ( ) => {
60+ it ( 'should call SourceProcessor for every emitted source file' , async ( ) => {
5461 const outputDir = result . compilation . outputOptions . path ;
5562 assert ( outputDir ) ;
5663
5764 const jsFiles = await getFiles ( outputDir , / .j s $ / ) ;
5865 expect ( jsFiles . length ) . toBeGreaterThan ( 0 ) ;
5966
60- const processedPairs = processSpy . mock . calls . map (
61- ( [ p1 , p2 ] ) => [ path . resolve ( p1 ) , p2 ? path . resolve ( p2 ) : undefined ] as const ,
62- ) ;
63- for ( const file of jsFiles ) {
64- const content = await fs . promises . readFile ( file , 'utf8' ) ;
65- const matches = [ ...content . matchAll ( / ^ \/ \/ # s o u r c e M a p p i n g U R L = ( .+ ) $ / gm) ] ;
66- expect ( matches . length ) . toEqual ( 1 ) ;
67- const [ , sourceMapPath ] = matches [ 0 ] ;
68-
69- expect ( processedPairs ) . toContainEqual ( [
70- path . resolve ( file ) ,
71- path . resolve ( path . dirname ( file ) , sourceMapPath ) ,
72- ] ) ;
73- }
67+ const processedFiles = processSpy . mock . calls
68+ . map ( ( [ sourcePath ] ) => path . resolve ( sourcePath ) )
69+ . sort ( ( a , b ) => a . localeCompare ( b ) ) ;
70+
71+ expect ( processedFiles ) . toEqual ( jsFiles . sort ( ( a , b ) => a . localeCompare ( b ) ) ) ;
7472 } ) ;
7573
7674 it ( 'should append every emitted sourcemap to archive' , async ( ) => {
7775 const outputDir = result . compilation . outputOptions . path ;
7876 assert ( outputDir ) ;
7977
80- const mapFiles = await getFiles ( outputDir , / .j s .m a p $ / ) ;
78+ const mapFiles = ( await getFiles ( outputDir , / .j s .m a p $ / ) ) . map ( ( f ) => path . basename ( f ) ) ;
8179 expect ( mapFiles . length ) . toBeGreaterThan ( 0 ) ;
8280
83- const uploadedFiles = zipArchiveAppendSpy . mock . calls . map ( ( c ) => path . resolve ( c [ 1 ] as string ) ) ;
81+ const uploadedFiles = zipArchiveAppendSpy . mock . calls . map ( ( [ name ] ) => name ) ;
8482 for ( const file of mapFiles ) {
85- expect ( uploadedFiles ) . toContain ( path . resolve ( file ) ) ;
83+ expect ( uploadedFiles ) . toContainEqual ( expect . stringContaining ( file ) ) ;
8684 }
8785 } ) ;
8886
8987 it ( 'should upload archive' , async ( ) => {
90- expect ( uploadSpy ) . toBeCalledWith ( expect . any ( ZipArchive ) ) ;
88+ expect ( uploadSpy ) . toHaveBeenCalledWith ( expect . any ( Readable ) ) ;
9189 } ) ;
9290 } ) ;
9391}
0 commit comments