Skip to content

Commit

Permalink
Use customPlugins in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Sep 9, 2024
1 parent 0021913 commit a95eea3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 86 deletions.
69 changes: 24 additions & 45 deletions packages/tests/src/core/plugins/build-report/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,10 @@ import type {
BuildReport,
BundlerReport,
} from '@dd/core/types';
import { outputTexts } from '@dd/telemetry-plugins/common/output/text';
import { defaultDestination, defaultEntry, defaultPluginOptions } from '@dd/tests/helpers/mocks';
import { BUNDLERS, runBundlers } from '@dd/tests/helpers/runBundlers';
import path from 'path';

// Used to intercept shared contexts.
jest.mock('@dd/telemetry-plugins/common/output/text', () => {
const originalModule = jest.requireActual('@dd/telemetry-plugins/common/output/text');
return {
...originalModule,
outputTexts: jest.fn(() => []),
};
});

// Don't send anything.
jest.mock('@dd/telemetry-plugins/common/sender', () => {
const originalModule = jest.requireActual('@dd/telemetry-plugins/common/sender');
return {
...originalModule,
sendMetrics: jest.fn(() => []),
};
});

const outputTextsMocked = jest.mocked(outputTexts);

const sortFiles = (a: File | Output | Entry, b: File | Output | Entry) => {
if (a.name < b.name) {
return -1;
Expand All @@ -50,35 +29,39 @@ const sortFiles = (a: File | Output | Entry, b: File | Output | Entry) => {
return 0;
};

const getOutputTextsImplem: (
const getPluginConfig: (
bundlerReports: Record<string, BundlerReport>,
buildReports: Record<string, BuildReport>,
) => typeof outputTexts = (bundlerReports, buildReports) => (context) => {
const bundlerName = `${context.bundler.name}${context.bundler.variant || ''}`;
// Freeze them in time by deep cloning them safely.
bundlerReports[bundlerName] = JSON.parse(JSON.stringify(context.bundler));
buildReports[bundlerName] = unserializeBuildReport(serializeBuildReport(context.build));
return Promise.resolve();
};

const pluginConfig: Options = {
...defaultPluginOptions,
// TODO: Replace these with an injected custom plugins, once we implemented the feature.
telemetry: {},
) => Options = (bundlerReports, buildReports) => {
return {
...defaultPluginOptions,
customPlugins: [
// Use a custom plugin to intercept contexts to verify it at the moment they're used.
(opts, context) => [
{
name: 'custom-plugin',
enforce: 'post',
writeBundle: () => {
const bundlerName = context.bundler.fullName;
// Freeze them in time by deep cloning them safely.
bundlerReports[bundlerName] = JSON.parse(JSON.stringify(context.bundler));
buildReports[bundlerName] = unserializeBuildReport(
serializeBuildReport(context.build),
);
},
},
],
],
};
};

describe('Build Report Plugin', () => {
describe('Basic build', () => {
// Intercept contexts to verify it at the moment they're used.
const bundlerReports: Record<string, BundlerReport> = {};
const buildReports: Record<string, BuildReport> = {};

beforeAll(async () => {
outputTextsMocked.mockImplementation(
getOutputTextsImplem(bundlerReports, buildReports),
);

await runBundlers(pluginConfig);
await runBundlers(getPluginConfig(bundlerReports, buildReports));
});

const expectedInput = () =>
Expand Down Expand Up @@ -217,11 +200,7 @@ describe('Build Report Plugin', () => {
},
};

outputTextsMocked.mockImplementation(
getOutputTextsImplem(bundlerReports, buildReports),
);

await runBundlers(pluginConfig, bundlerOverrides);
await runBundlers(getPluginConfig(bundlerReports, buildReports), bundlerOverrides);
});

const expectedInput = (name: string) =>
Expand Down
56 changes: 15 additions & 41 deletions packages/tests/src/core/plugins/global-context/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,32 @@
// Copyright 2019-Present Datadog, Inc.

import type { GlobalContext, Options } from '@dd/core/types';
import { uploadSourcemaps } from '@dd/rum-plugins/sourcemaps/index';
import { getPlugins } from '@dd/telemetry-plugins';
import { defaultDestination, defaultPluginOptions } from '@dd/tests/helpers/mocks';
import { BUNDLERS, runBundlers } from '@dd/tests/helpers/runBundlers';
import { getSourcemapsConfiguration } from '@dd/tests/plugins/rum/testHelpers';
import path from 'path';

jest.mock('@dd/telemetry-plugins', () => {
const originalModule = jest.requireActual('@dd/telemetry-plugins');
return {
...originalModule,
getPlugins: jest.fn(() => []),
};
});

jest.mock('@dd/rum-plugins/sourcemaps/index', () => {
const originalModule = jest.requireActual('@dd/rum-plugins/sourcemaps/index');
return {
...originalModule,
uploadSourcemaps: jest.fn(),
};
});

const getTelemetryPluginsMocked = jest.mocked(getPlugins);
const uploadSourcemapsMocked = jest.mocked(uploadSourcemaps);

describe('Global Context Plugin', () => {
// Intercept contexts to verify it at the moment they're used.
const initialContexts: Record<string, GlobalContext> = {};
const lateContexts: Record<string, GlobalContext> = {};
beforeAll(async () => {
// This one is called at initialization, with the initial context.
getTelemetryPluginsMocked.mockImplementation((options, context) => {
const bundlerName = `${context.bundler.name}${context.bundler.variant || ''}`;
initialContexts[bundlerName] = JSON.parse(JSON.stringify(context));
return [];
});

// This one is called late in the build, with the final context.
uploadSourcemapsMocked.mockImplementation((options, context, log) => {
const bundlerName = `${context.bundler.name}${context.bundler.variant || ''}`;
lateContexts[bundlerName] = JSON.parse(JSON.stringify(context));
return Promise.resolve();
});

const pluginConfig: Options = {
...defaultPluginOptions,
// TODO: Replace these with an injected custom plugins, once we implemented the feature.
telemetry: {},
rum: {
sourcemaps: getSourcemapsConfiguration(),
},
customPlugins: [
// Use a custom plugin to intercept contexts to verify it at the moment they're used.
(opts, context) => {
const bundlerName = context.bundler.fullName;
initialContexts[bundlerName] = JSON.parse(JSON.stringify(context));
return [
{
name: 'custom-plugin',
writeBundle() {
lateContexts[bundlerName] = JSON.parse(JSON.stringify(context));
},
},
];
},
],
};

await runBundlers(pluginConfig);
Expand Down

0 comments on commit a95eea3

Please sign in to comment.