Skip to content

Commit

Permalink
Add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Jul 3, 2023
1 parent 215cb0b commit d70490b
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Simply output the metadata to the console so it can be checked in a test
// eslint-disable-next-line no-console
console.log(JSON.stringify(global._sentryModuleMetadata));
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable jest/no-standalone-expect */
/* eslint-disable jest/expect-expect */
import { execSync } from "child_process";
import path from "path";
import { testIfNodeMajorVersionIsLessThan18 } from "../../utils/testIf";

function checkBundle(bundlePath: string): void {
const output = execSync(`node ${bundlePath}`, { encoding: "utf-8" });

const map = JSON.parse(output) as Record<string, string>;

// There should be only one key in the map
expect(Object.values(map)).toHaveLength(1);
// The value should be the expected metadata
expect(Object.values(map)).toEqual([{ team: "frontend" }]);
}

describe("metadata injection", () => {
testIfNodeMajorVersionIsLessThan18("webpack 4 bundle", () => {
checkBundle(path.join(__dirname, "out", "webpack4", "bundle.js"));
});

test("webpack 5 bundle", () => {
checkBundle(path.join(__dirname, "out", "webpack5", "bundle.js"));
});
});
17 changes: 17 additions & 0 deletions packages/integration-tests/fixtures/metadata-injection/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as path from "path";
import { createCjsBundles } from "../../utils/create-cjs-bundles";

const outputDir = path.resolve(__dirname, "out");

createCjsBundles(
{
bundle: path.resolve(__dirname, "input", "bundle.js"),
},
outputDir,
{
_experiments: {
moduleMetadata: { team: "frontend" },
},
},
["webpack4", "webpack5"]
);
108 changes: 58 additions & 50 deletions packages/integration-tests/utils/create-cjs-bundles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,53 @@ const nodejsMajorversion = process.version.split(".")[0]!.slice(1);
export function createCjsBundles(
entrypoints: { [name: string]: string },
outFolder: string,
sentryUnpluginOptions: Options
sentryUnpluginOptions: Options,
plugins: string[] = []
): void {
void vite.build({
clearScreen: false,
build: {
outDir: path.join(outFolder, "vite"),
rollupOptions: {
input: entrypoints,
output: {
format: "cjs",
entryFileNames: "[name].js",
if (plugins.length === 0 || plugins.includes("vite")) {
void vite.build({
clearScreen: false,
build: {
outDir: path.join(outFolder, "vite"),
rollupOptions: {
input: entrypoints,
output: {
format: "cjs",
entryFileNames: "[name].js",
},
},
},
},
plugins: [sentryVitePlugin(sentryUnpluginOptions)],
});

void rollup
.rollup({
input: entrypoints,
plugins: [sentryRollupPlugin(sentryUnpluginOptions)],
})
.then((bundle) =>
bundle.write({
dir: path.join(outFolder, "rollup"),
format: "cjs",
exports: "named",
plugins: [sentryVitePlugin(sentryUnpluginOptions)],
});
}
if (plugins.length === 0 || plugins.includes("rollup")) {
void rollup
.rollup({
input: entrypoints,
plugins: [sentryRollupPlugin(sentryUnpluginOptions)],
})
);
.then((bundle) =>
bundle.write({
dir: path.join(outFolder, "rollup"),
format: "cjs",
exports: "named",
})
);
}

void esbuild.build({
entryPoints: entrypoints,
outdir: path.join(outFolder, "esbuild"),
plugins: [sentryEsbuildPlugin(sentryUnpluginOptions)],
minify: true,
bundle: true,
format: "cjs",
});
if (plugins.length === 0 || plugins.includes("esbuild")) {
void esbuild.build({
entryPoints: entrypoints,
outdir: path.join(outFolder, "esbuild"),
plugins: [sentryEsbuildPlugin(sentryUnpluginOptions)],
minify: true,
bundle: true,
format: "cjs",
});
}

// Webpack 4 doesn't work on Node.js versions >= 18
if (parseInt(nodejsMajorversion) < 18) {
if (parseInt(nodejsMajorversion) < 18 && (plugins.length === 0 || plugins.includes("webpack4"))) {
webpack4(
{
mode: "production",
Expand All @@ -77,23 +83,25 @@ export function createCjsBundles(
);
}

webpack5(
{
cache: false,
entry: entrypoints,
output: {
path: path.join(outFolder, "webpack5"),
library: {
type: "commonjs",
if (plugins.length === 0 || plugins.includes("webpack5")) {
webpack5(
{
cache: false,
entry: entrypoints,
output: {
path: path.join(outFolder, "webpack5"),
library: {
type: "commonjs",
},
},
mode: "production",
plugins: [sentryWebpackPlugin(sentryUnpluginOptions)],
},
mode: "production",
plugins: [sentryWebpackPlugin(sentryUnpluginOptions)],
},
(err) => {
if (err) {
throw err;
(err) => {
if (err) {
throw err;
}
}
}
);
);
}
}

0 comments on commit d70490b

Please sign in to comment.