Skip to content

Commit

Permalink
Update helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Jun 20, 2024
1 parent d4ed39c commit c5e6849
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 48 deletions.
18 changes: 2 additions & 16 deletions packages/tests/src/factory/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Copyright 2019-Present Datadog, Inc.

import { getPlugins } from '@dd/telemetry-plugins';
import fs from 'fs';
import path from 'path';

import { runBundlers } from '../helpers';

Expand All @@ -16,27 +14,15 @@ jest.mock('@dd/telemetry-plugins', () => {
};
});

const entry = '@dd/tests/fixtures/index.js';
const destination = path.resolve(__dirname, './fixtures/dist');
const getPluginsMocked = jest.mocked(getPlugins);

describe('Factory', () => {
afterEach(() => {
// Clean files
fs.rmSync(destination, {
recursive: true,
force: true,
});
});
test('It should not call a disabled plugin', async () => {
await runBundlers({ entry, destination }, { telemetry: { disabled: true } });
await runBundlers({ telemetry: { disabled: true } });
expect(getPluginsMocked).not.toHaveBeenCalled();
});
test('It should call an enabled plugin', async () => {
const results = await runBundlers(
{ entry, destination },
{ telemetry: { disabled: false } },
);
const results = await runBundlers({ telemetry: { disabled: false } });
expect(getPluginsMocked).toHaveBeenCalledTimes(results.length);
});
});
119 changes: 87 additions & 32 deletions packages/tests/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,57 @@

import { datadogEsbuildPlugin } from '@datadog/esbuild-plugin';
import { datadogWebpackPlugin } from '@datadog/webpack-plugin';
import type { Options } from '@dd/factory';
import type { GlobalContext, Options } from '@dd/core/types';
import esbuild from 'esbuild';
import path from 'path';
import webpack from 'webpack';

type BundlerOptions = {
entry: string;
destination: string;
entry?: string;
destination?: string;
};

export const defaultPluginOptions: Options = {
auth: {
apiKey: '',
apiKey: '123',
},
logLevel: 'debug',
};

const getBundlerOptions = (
{ entry, destination }: BundlerOptions,
export const getContextMock = (options: Partial<GlobalContext> = {}): GlobalContext => {
return {
auth: { apiKey: '123' },
cwd: '/cwd/path',
version: '1.2.3',
bundler: { name: 'esbuild' },
...options,
};
};

export const getFetchMock = (options: Partial<Response> = {}) => {
return Promise.resolve({
...new Response(),
ok: true,
status: 200,
statusText: 'OK',
json: () => Promise.resolve({}),
...options,
});
};

const getWebpackOptions = (
pluginOptionOverrides: Options = {},
) => {
bundlerOptions: BundlerOptions = {},
): webpack.Configuration => {
const entry = bundlerOptions?.entry || defaultEntry;
const destination = bundlerOptions?.destination || defaultDestination;

const newPluginOptions = {
...defaultPluginOptions,
...pluginOptionOverrides,
};
// Bundler configs.
const esbuildConfig: esbuild.BuildOptions = {
bundle: true,
sourcemap: true,
entryPoints: [entry],
outfile: path.join(destination, 'esbuild', 'index.js'),
plugins: [datadogEsbuildPlugin(newPluginOptions)],
};

const configWebpack: webpack.Configuration = {
return {
entry,
output: {
path: path.join(destination, 'webpack'),
Expand All @@ -47,26 +63,65 @@ const getBundlerOptions = (
devtool: 'source-map',
plugins: [datadogWebpackPlugin(newPluginOptions)],
};
};

const getEsbuildOptions = (
pluginOptionOverrides: Options = {},
bundlerOptions: BundlerOptions = {},
): esbuild.BuildOptions => {
const entry = bundlerOptions?.entry || defaultEntry;
const destination = bundlerOptions?.destination || defaultDestination;

const newPluginOptions = {
...defaultPluginOptions,
...pluginOptionOverrides,
};

return {
webpack: configWebpack,
esbuild: esbuildConfig,
bundle: true,
sourcemap: true,
entryPoints: [entry],
outfile: path.join(destination, 'esbuild', 'index.js'),
plugins: [datadogEsbuildPlugin(newPluginOptions)],
};
};

export const runBundlers = async (bundlerOptions: BundlerOptions, pluginOptions?: Options) => {
export const defaultEntry = '@dd/tests/fixtures/index.js';
export const defaultDestination = path.resolve(__dirname, './dist');

export const runWebpack = async (
pluginOptions: Options = {},
bundlerOptions: BundlerOptions = {},
) => {
const bundlerConfigs = getWebpackOptions(pluginOptions, bundlerOptions);
return new Promise((resolve) => {
webpack(bundlerConfigs, (err, stats) => {
if (err) {
console.log(err);
}
resolve(stats);
});
});
};

export const runEsbuild = async (
pluginOptions: Options = {},
bundlerOptions: BundlerOptions = {},
) => {
const bundlerConfigs = getEsbuildOptions(pluginOptions, bundlerOptions);
return esbuild.build(bundlerConfigs);
};

export const runBundlers = async (
pluginOptions: Options = {},
bundlerOptions: BundlerOptions = {},
) => {
const promises = [];
const bundlerConfigs = getBundlerOptions(bundlerOptions, pluginOptions);
promises.push(
new Promise((resolve) => {
webpack(bundlerConfigs.webpack, (err, stats) => {
if (err) {
console.log(err);
}
resolve(stats);
});
}),
);
promises.push(esbuild.build(bundlerConfigs.esbuild));
return Promise.all(promises);

promises.push(runWebpack(pluginOptions, bundlerOptions));
promises.push(runEsbuild(pluginOptions, bundlerOptions));

const results = await Promise.all(promises);

return results;
};

0 comments on commit c5e6849

Please sign in to comment.