Skip to content

Commit

Permalink
Fix test compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mvirgil committed Jan 7, 2025
1 parent f6c08c3 commit b39ac58
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
26 changes: 17 additions & 9 deletions test/sourcemaps/discoverJsMapFilePath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -54,69 +55,76 @@ 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();
});

test('should throw UserFriendlyError when file operations fail due to known error code', async () => {
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);
});
});

Expand Down
24 changes: 16 additions & 8 deletions test/sourcemaps/injectFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) => {
Expand All @@ -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 });

Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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();
});
Expand All @@ -154,15 +162,15 @@ 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();
});

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);
});
Expand All @@ -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);
});
Expand Down

0 comments on commit b39ac58

Please sign in to comment.