From 2d158936dea57d5f3add4b5a90404b073f31a9d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 28 Jul 2023 15:15:33 -0400 Subject: [PATCH] find fixture basic index at runtime In webpack 5.0.0, the mapObj.sources are [ 'webpack://babel-loader/./test/fixtures/basic.js', 'webpack://babel-loader/./test/fixtures/constant.js', 'webpack://babel-loader/./test/fixtures/import.js', 'webpack://babel-loader/webpack/bootstrap', 'webpack://babel-loader/webpack/startup' ] In later versions the `'webpack://babel-loader/./test/fixtures/basic.js'` is moved after webpack bootstrap, causing different indices across different webpack versions. --- test/sourcemaps.test.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/test/sourcemaps.test.js b/test/sourcemaps.test.js index 021e893..85e6ded 100644 --- a/test/sourcemaps.test.js +++ b/test/sourcemaps.test.js @@ -100,10 +100,14 @@ test("should output webpack's sourcemap properly when set 'inline'", async t => const data = fs.readFileSync(path.resolve(t.context.directory, map[0])); const mapObj = JSON.parse(data); - t.is(mapObj.sources[3], "webpack://babel-loader/./test/fixtures/basic.js"); + const fixtureBasicIndex = mapObj.sources.indexOf( + "webpack://babel-loader/./test/fixtures/basic.js", + ); + // The index may vary across webpack versions + t.not(fixtureBasicIndex, -1); // Ensure that the map contains the original code, not the compiled src. - t.falsy(mapObj.sourcesContent[3].includes("__esModule")); + t.falsy(mapObj.sourcesContent[fixtureBasicIndex].includes("__esModule")); }); test("should output webpack's devtoolModuleFilename option", async t => { @@ -188,11 +192,15 @@ test("should disable sourcemap output with 'sourceMaps:false'", async t => { const data = fs.readFileSync(path.resolve(t.context.directory, map[0])); const mapObj = JSON.parse(data); - t.is(mapObj.sources[3], "webpack://babel-loader/./test/fixtures/basic.js"); + const fixtureBasicIndex = mapObj.sources.indexOf( + "webpack://babel-loader/./test/fixtures/basic.js", + ); + // The index may vary across webpack versions + t.not(fixtureBasicIndex, -1); // Ensure that the code contains Babel's compiled output, because // sourcemaps from Babel are disabled. - t.truthy(mapObj.sourcesContent[3].includes("__esModule")); + t.truthy(mapObj.sourcesContent[fixtureBasicIndex].includes("__esModule")); }); test("should disable sourcemap output with 'sourceMap:false'", async t => { @@ -228,9 +236,13 @@ test("should disable sourcemap output with 'sourceMap:false'", async t => { const data = fs.readFileSync(path.resolve(t.context.directory, map[0])); const mapObj = JSON.parse(data); - t.is(mapObj.sources[3], "webpack://babel-loader/./test/fixtures/basic.js"); + const fixtureBasicIndex = mapObj.sources.indexOf( + "webpack://babel-loader/./test/fixtures/basic.js", + ); + // The index may vary across webpack versions + t.not(fixtureBasicIndex, -1); // Ensure that the code contains Babel's compiled output, because // sourcemaps from Babel are disabled. - t.truthy(mapObj.sourcesContent[3].includes("__esModule")); + t.truthy(mapObj.sourcesContent[fixtureBasicIndex].includes("__esModule")); });