From b39ac58d748af2257843752c7ff9aaa5102b27fa Mon Sep 17 00:00:00 2001 From: Max Virgil Date: Tue, 7 Jan 2025 11:00:08 -0800 Subject: [PATCH] Fix test compile errors --- test/sourcemaps/discoverJsMapFilePath.test.ts | 26 ++++++++++++------- test/sourcemaps/injectFile.test.ts | 24 +++++++++++------ 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/test/sourcemaps/discoverJsMapFilePath.test.ts b/test/sourcemaps/discoverJsMapFilePath.test.ts index 6548d16..0bcd99a 100644 --- a/test/sourcemaps/discoverJsMapFilePath.test.ts +++ b/test/sourcemaps/discoverJsMapFilePath.test.ts @@ -20,6 +20,7 @@ import { discoverJsMapFilePath } from '../../src/sourcemaps/discoverJsMapFilePat import { UserFriendlyError } from '../../src/utils/userFriendlyErrors'; import { SourceMapInjectOptions } from '../../src/sourcemaps'; import * as filesystem from '../../src/utils/filesystem'; +import { Logger } from '../../src/utils/logger'; describe('discoverJsMapFilePath', () => { const opts = getMockCommandOptions(); @@ -54,61 +55,68 @@ describe('discoverJsMapFilePath', () => { jest.spyOn(filesystem, 'readlines').mockImplementation(() => throwErrnoException('EACCES')); } + const mockLogger: Logger = { + error: jest.fn(), + warn: jest.fn(), + info: jest.fn(), + debug: jest.fn() + }; + afterEach(() => { jest.restoreAllMocks(); }); test('should return a match if we already know the file name with ".map" is present in the directory', async () => { - const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/to/file.js.map' ], opts); + const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/to/file.js.map' ], opts, mockLogger); expect(path).toBe('path/to/file.js.map'); }); test('should return a match if "//# sourceMappingURL=" comment has a relative path', async () => { mockJsFileContents('//# sourceMappingURL=mappings/file.js.map\n'); - const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/to/mappings/file.js.map' ], opts); + const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/to/mappings/file.js.map' ], opts, mockLogger); expect(path).toBe('path/to/mappings/file.js.map'); }); test('should return a match if "//# sourceMappingURL=" comment has a relative path with ..', async () => { mockJsFileContents('//# sourceMappingURL=../mappings/file.js.map\n'); - const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/mappings/file.js.map' ], opts); + const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/mappings/file.js.map' ], opts, mockLogger); expect(path).toBe('path/mappings/file.js.map'); }); test('should not return a match if "//# sourceMappingURL=" comment points to a file outside of our directory', async () => { mockJsFileContents('//# sourceMappingURL=../../../some/other/folder/file.js.map'); - const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/to/mappings/file.js.map' ], opts); + const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/to/mappings/file.js.map' ], opts, mockLogger); expect(path).toBeNull(); }); test('should not return a match if "//# sourceMappingURL=" comment has a data URL', async () => { mockJsFileContents('//# sourceMappingURL=data:application/json;base64,abcd\n'); - const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/to/data:application/json;base64,abcd' ], opts); + const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/to/data:application/json;base64,abcd' ], opts, mockLogger); expect(path).toBeNull(); }); test('should not return a match if "//# sourceMappingURL=" comment has an HTTP URL', async () => { mockJsFileContents('//# sourceMappingURL=http://www.splunk.com/dist/file.js.map\n'); - const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/to/http://www.splunk.com/dist/file.js.map' ], opts); + const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/to/http://www.splunk.com/dist/file.js.map' ], opts, mockLogger); expect(path).toBeNull(); }); test('should not return a match if "//# sourceMappingURL=" comment has an HTTPS URL', async () => { mockJsFileContents('//# sourceMappingURL=https://www.splunk.com/dist/file.js.map\n'); - const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/to/https://www.splunk.com/dist/file.js.map' ], opts); + const path = await discoverJsMapFilePath('path/to/file.js', [ 'path/to/https://www.splunk.com/dist/file.js.map' ], opts, mockLogger); expect(path).toBeNull(); }); test('should not return a match if file is not already known and sourceMappingURL comment is absent', async () => { mockJsFileContents('console.log("hello world!");'); - const path = await discoverJsMapFilePath('path/to/file.js', [ 'file.map.js' ], opts); + const path = await discoverJsMapFilePath('path/to/file.js', [ 'file.map.js' ], opts, mockLogger); expect(path).toBeNull(); }); @@ -116,7 +124,7 @@ describe('discoverJsMapFilePath', () => { mockJsFileContents('console.log("hello world!");'); mockJsFileError(); - await expect(discoverJsMapFilePath('path/to/file.js', [], opts)).rejects.toThrowError(UserFriendlyError); + await expect(discoverJsMapFilePath('path/to/file.js', [], opts, mockLogger)).rejects.toThrowError(UserFriendlyError); }); }); diff --git a/test/sourcemaps/injectFile.test.ts b/test/sourcemaps/injectFile.test.ts index d703fc1..c028dd8 100644 --- a/test/sourcemaps/injectFile.test.ts +++ b/test/sourcemaps/injectFile.test.ts @@ -20,6 +20,7 @@ import { Readable } from 'stream'; import { injectFile } from '../../src/sourcemaps/injectFile'; import { UserFriendlyError } from '../../src/utils/userFriendlyErrors'; import { SourceMapInjectOptions } from '../../src/sourcemaps'; +import { Logger } from '../../src/utils/logger'; describe('injectFile', () => { const mockJsFileContentBeforeInjection = (lines: string[]) => { @@ -42,6 +43,13 @@ describe('injectFile', () => { return jest.spyOn(filesystem, 'overwriteFileContents').mockImplementation(() => Promise.reject(throwErrnoException('EACCES'))); }; + const mockLogger: Logger = { + error: jest.fn(), + warn: jest.fn(), + info: jest.fn(), + debug: jest.fn() + }; + const opts = getMockCommandOptions(); const dryRunOpts = getMockCommandOptions({ dryRun: true }); @@ -52,7 +60,7 @@ describe('injectFile', () => { ]); const mockOverwriteFn = mockJsFileOverwrite(); - await injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts); + await injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts, mockLogger); expect(mockOverwriteFn).toHaveBeenCalledWith( expect.any(String), @@ -72,7 +80,7 @@ describe('injectFile', () => { ]); const mockOverwriteFn = mockJsFileOverwrite(); - await injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts); + await injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts, mockLogger); expect(mockOverwriteFn).toHaveBeenCalledWith( expect.any(String), @@ -94,7 +102,7 @@ describe('injectFile', () => { ]); const mockOverwriteFn = mockJsFileOverwrite(); - await injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts); + await injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts, mockLogger); expect(mockOverwriteFn).toHaveBeenCalledWith( expect.any(String), @@ -113,7 +121,7 @@ describe('injectFile', () => { ); const mockOverwriteFn = mockJsFileOverwrite(); - await injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts); + await injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts, mockLogger); expect(mockOverwriteFn).toHaveBeenCalledWith( expect.any(String), @@ -142,7 +150,7 @@ describe('injectFile', () => { ]); const mockOverwriteFn = mockJsFileOverwrite(); - await injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts); + await injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts, mockLogger); expect(mockOverwriteFn).not.toHaveBeenCalled(); }); @@ -154,7 +162,7 @@ describe('injectFile', () => { ]); const mockOverwriteFn = mockJsFileOverwrite(); - await injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', dryRunOpts); + await injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', dryRunOpts, mockLogger); expect(mockOverwriteFn).not.toHaveBeenCalled(); }); @@ -162,7 +170,7 @@ describe('injectFile', () => { test('should throw a UserFriendlyError if reading jsFilePath fails due to known error code', async () => { mockJsFileReadError(); - await expect(injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts)) + await expect(injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts, mockLogger)) .rejects .toThrowError(UserFriendlyError); }); @@ -174,7 +182,7 @@ describe('injectFile', () => { ]); mockJsFileOverwriteError(); - await expect(injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts)) + await expect(injectFile('file.js', '647366e7-d3db-6cf4-8693-2c321c377d5a', opts, mockLogger)) .rejects .toThrowError(UserFriendlyError); });