Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Apr 26, 2024
1 parent 4225a44 commit b6fc2e0
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 38 deletions.
7 changes: 4 additions & 3 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export interface Stats {
fileDependencies: Set<any>;
emittedAssets: Set<any>;
warnings: string[];
modules: Set<Module> & Module[];
chunks: Set<Chunk> & Chunk[];
entries: any[] & Map<string, any>;
modules: Set<Module> | Module[];
chunks: Set<Chunk> | Chunk[];
entries: any[] | Map<string, any>;
};
}

Expand Down Expand Up @@ -112,6 +112,7 @@ export interface Tapable {
}

export interface Compiler extends Tapable {
options: {};
hooks: {
thisCompilation: { tap(opts: any, callback: (compilation: Compilation) => void): void };
done: {
Expand Down
5 changes: 0 additions & 5 deletions packages/plugins/telemetry/src/common/metrics/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export const getIndexed = (stats: EsbuildStats, cwd: string): EsbuildIndexedObje
entryNames.set(getModulePath(entryPath, cwd), entryName);
}
}
console.log('EntryNames', entryNames);
// First loop to index inputs dependencies.
const outputs = stats.outputs ? Object.entries(stats.outputs) : [];
for (const [outputName, output] of outputs) {
Expand All @@ -70,14 +69,11 @@ export const getIndexed = (stats: EsbuildStats, cwd: string): EsbuildIndexedObje
}
}

console.log('Deps', inputsDependencies, outputsDependencies, entryNames);

// Second loop to index output dependencies.
// Input dependencies are needed, hence the second loop.
for (const [outputName, output] of outputs) {
// Check which entry has generated this output.
const inputs = output.inputs ? Object.keys(output.inputs) : [];
console.log('Inputs', inputs, Object.entries(inputsDependencies));
for (const inputName of inputs) {
for (const [entryName, entry] of Object.entries(inputsDependencies)) {
if (entry.has(inputName)) {
Expand Down Expand Up @@ -174,7 +170,6 @@ export const getEntries = (
if (entryName) {
const inputs = getInputsDependencies(stats.inputs, output.entryPoint);
const tags = [formatEntryTag(entryName, cwd)];
console.log(entryName, Object.keys(indexed.outputsDependencies));
metrics.push(
{
metric: 'entries.size',
Expand Down
15 changes: 12 additions & 3 deletions packages/plugins/telemetry/src/common/output/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,20 @@ export const outputWebpack = (stats: Stats) => {
: stats.compilation.emittedAssets.size;
const nbWarnings = stats.compilation.warnings.length;
// In Webpack 5, stats.compilation.modules is a Set.
const nbModules = stats.compilation.modules.size || stats.compilation.modules.length;
const nbModules =
'size' in stats.compilation.modules
? stats.compilation.modules.size
: stats.compilation.modules.length;
// In Webpack 5, stats.compilation.chunks is a Set.
const nbChunks = stats.compilation.chunks.size || stats.compilation.chunks.length;
const nbChunks =
'size' in stats.compilation.chunks
? stats.compilation.chunks.size
: stats.compilation.chunks.length;
// In Webpack 5, stats.compilation.entries is a Map.
const nbEntries = stats.compilation.entries.size || stats.compilation.entries.length;
const nbEntries =
'size' in stats.compilation.entries
? stats.compilation.entries.size
: stats.compilation.entries.length;
console.log(`duration: ${chalk.bold(formatDuration(duration))}
nbDeps: ${chalk.bold(nbDeps.toString())}
nbFiles: ${chalk.bold(nbFiles.toString())}
Expand Down
6 changes: 5 additions & 1 deletion packages/plugins/telemetry/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ export type TelemetryOptions = {
datadog?: DatadogOptions;
};

export interface TelemetryOptionsEnabled extends TelemetryOptions {
disabled?: false;
}

export interface OptionsWithTelemetryEnabled extends GetPluginsOptionsWithCWD {
[CONFIG_KEY]: TelemetryOptions & { disabled: false | undefined };
[CONFIG_KEY]: TelemetryOptionsEnabled;
}

interface EsbuildBundlerResult extends Metafile {
Expand Down
11 changes: 7 additions & 4 deletions packages/tests/src/telemetry/common/aggregator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import { mockReport, mockStats } from '@datadog/build-plugins-tests/testHelpers';
import {
mockReport,
mockBundler,
mockOptionsWithTelemetryEnabled,
} from '@datadog/build-plugins-tests/testHelpers';
import { getMetrics } from '@dd/telemetry-plugins/common/aggregator';

describe('Aggregator', () => {
test('It should aggregate metrics without throwing.', () => {
const { getMetrics } = require('@dd/telemetry-plugins/common/aggregator');
const opts = { context: '', filters: [], tags: [] };
expect(() => {
getMetrics(opts, mockReport, mockStats);
getMetrics(mockOptionsWithTelemetryEnabled, mockReport, mockBundler);
}).not.toThrow();
});
});
11 changes: 6 additions & 5 deletions packages/tests/src/telemetry/common/output/files.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { mockReport } from '@datadog/build-plugins-tests/testHelpers';
import { outputFiles } from '@dd/telemetry-plugins/common/output/files';
import type { OutputOptions } from '@dd/telemetry-plugins/types';
import fs from 'fs-extra';
import path from 'path';

describe('Output Files', () => {
const directoryName = '/test/';
const init = async (output: OutputOptions, context: string) => {
const { outputFiles } = require('@dd/telemetry-plugins/common/output/files');
const init = async (output: OutputOptions, cwd: string) => {
await outputFiles(
{
start: 0,
report: mockReport,
metrics: {},
stats: { toJson: () => ({}) },
metrics: [],
bundler: {},
},
{ output, context },
{ auth: { apiKey: '', appKey: '' }, telemetry: { output }, cwd },
);
};

Expand Down
9 changes: 5 additions & 4 deletions packages/tests/src/telemetry/webpack-plugin/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@

import webpackPlugin from '@datadog/webpack-plugin';

import { mockCompiler, mockOptions } from '../../testHelpers';

describe('webpack', () => {
test('It should not execute if disabled', () => {
const compiler = {
...mockCompiler,
hooks: {
thisCompilation: {
...mockCompiler.hooks.thisCompilation,
tap: jest.fn(),
},
},
};

const plugin = webpackPlugin({
auth: {
apiKey: '',
appKey: '',
},
...mockOptions,
telemetry: {
disabled: true,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/tests/src/telemetry/webpack-plugin/modules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright 2019-Present Datadog, Inc.

import type { LocalModule, Module, Compilation, Chunk } from '@datadog/build-plugins-core/types';
import { mockLocalOptions } from '@datadog/build-plugins-tests/testHelpers';
import { mockTelemetryOptions } from '@datadog/build-plugins-tests/testHelpers';
import { Modules } from '@dd/telemetry-plugins/webpack-plugin/modules';

describe('Modules', () => {
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('Modules', () => {
},
};

const modules = new Modules('', mockLocalOptions);
const modules = new Modules('', mockTelemetryOptions);
modules.afterOptimizeTree({}, mockedModules, mockCompilation);

test('It should filter modules the same with Webpack 5 and 4', () => {
Expand Down
42 changes: 31 additions & 11 deletions packages/tests/src/testHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import type {
Compilation,
Compiler,
} from '@datadog/build-plugins-core/types';
import type { TelemetryOptions } from '@dd/telemetry-plugins/types';
import type { Options } from '@dd/factory';
import type {
OptionsWithTelemetryEnabled,
TelemetryOptions,
TelemetryOptionsEnabled,
} from '@dd/telemetry-plugins/types';
import type { PluginBuild, Metafile } from 'esbuild';
import esbuild from 'esbuild';
import path from 'path';
Expand Down Expand Up @@ -37,7 +42,7 @@ export const getMockBuild = (overrides: Partial<PluginBuild>): PluginBuild => {
};
};

export const mockStats = {
export const mockStats: Stats = {
toJson: jest.fn(() => ({
modules: [],
chunks: [],
Expand All @@ -58,7 +63,7 @@ export const mockStats = {
chunks: new Set(),
entries: new Map(),
},
} as unknown as Stats;
};

export const mockBundler: BundlerStats = {
webpack: mockStats,
Expand All @@ -72,17 +77,17 @@ export const mockBundler: BundlerStats = {
},
};

export const mockReport = {
export const mockReport: Report = {
timings: {
tapables: new Map(),
loaders: new Map(),
modules: new Map(),
},
dependencies: {},
} as Report;
};

const mockTapable = { tap: jest.fn() };
export const mockCompilation = {
export const mockCompilation: Compilation = {
options: {
context: '/default/context',
},
Expand All @@ -91,20 +96,22 @@ export const mockCompilation = {
succeedModule: mockTapable,
afterOptimizeTree: mockTapable,
},
} as Compilation;
};

export const mockCompiler = {
export const mockCompiler: Compiler = {
options: {},
hooks: {
thisCompilation: {
tap: (opts: any, cb: (c: Compilation) => void) => {
cb(mockCompilation);
},
},
done: {
tap: (opts: any, cb: (s: Stats) => void) => cb(mockStats),
tapPromise: (opts: any, cb: any) => cb(mockStats),
},
},
} as Compiler;
};

export const mockMetaFile: Metafile = {
inputs: {
Expand Down Expand Up @@ -133,6 +140,19 @@ export const mockMetaFile: Metafile = {
},
};

export const mockLocalOptions: TelemetryOptions = {
datadog: {},
export const mockOptions: Options = {
auth: {
apiKey: '',
appKey: '',
},
};
export const mockTelemetryOptions: TelemetryOptions = {};
export const mockTelemetryOptionsEnabled: TelemetryOptionsEnabled = {};
export const mockOptionsWithTelemetryEnabled: OptionsWithTelemetryEnabled = {
auth: {
apiKey: '',
appKey: '',
},
cwd: '',
telemetry: mockTelemetryOptionsEnabled,
};

0 comments on commit b6fc2e0

Please sign in to comment.