Skip to content

Commit b688905

Browse files
committed
webpack-plugin: fix e2e tests (NFC)
1 parent 476b231 commit b688905

File tree

6 files changed

+28
-73
lines changed

6 files changed

+28
-73
lines changed

tools/webpack-plugin/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import { BacktracePlugin } from './BacktracePlugin';
22
export { BacktracePlugin };
33
export default BacktracePlugin;
4+
export { BacktracePluginOptions } from '@backtrace/sourcemap-tools';

tools/webpack-plugin/tests/__mocks__/TestDebugIdGenerator.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Ok, SourceProcessor, SymbolUploader, ZipArchive } from '@backtrace/sourcemap-tools';
22
import assert from 'assert';
33
import crypto from 'crypto';
4-
import fs from 'fs';
54
import path from 'path';
5+
import { Readable } from 'stream';
66
import webpack from 'webpack';
77
import { 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, /.js$/);
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(/^\/\/# sourceMappingURL=(.+)$/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, /.js.map$/);
78+
const mapFiles = (await getFiles(outputDir, /.js.map$/)).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
}

tools/webpack-plugin/tests/e2e/helpers.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from 'fs';
22
import path from 'path';
33
import webpack from 'webpack';
44
import { BacktracePlugin, BacktracePluginOptions } from '../../src';
5-
import { TestDebugIdGenerator } from '../__mocks__/TestDebugIdGenerator';
65

76
export interface BaseConfigOptions {
87
tsconfigPath?: string;
@@ -62,18 +61,6 @@ export function expectSuccess(stats?: webpack.Stats): asserts stats is webpack.S
6261
}
6362
}
6463

65-
export async function expectSourceSnippet(content: string) {
66-
TestDebugIdGenerator.testForSourceSnippet(content);
67-
}
68-
69-
export async function expectSourceComment(content: string) {
70-
TestDebugIdGenerator.testForSourceComment(content);
71-
}
72-
73-
export async function expectSourceMapSnippet(content: string) {
74-
TestDebugIdGenerator.testForSourceMapKey(content);
75-
}
76-
7764
export async function getFiles(dir: string, test?: RegExp) {
7865
const files = (await fs.promises.readdir(dir)).map((f) => path.join(dir, f));
7966
if (test) {

tools/webpack-plugin/tsconfig.build.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
44
"rootDir": "./src",
5-
"outDir": "./lib"
5+
"outDir": "./lib",
6+
"lib": []
67
},
78
"exclude": ["node_modules", "tests", "lib"]
89
}

tools/webpack-plugin/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"lib": ["ES2020"]
5+
},
36
"references": [
47
{
58
"path": "../sourcemap-tools/tsconfig.json"

0 commit comments

Comments
 (0)