Skip to content

Commit

Permalink
Merge pull request #77 from DataDog/yoann/move-tests
Browse files Browse the repository at this point in the history
[tests] Move existing mocks
  • Loading branch information
yoannmoinet authored Jun 4, 2024
2 parents dd56c54 + 66f5b49 commit d3bded7
Show file tree
Hide file tree
Showing 39 changed files with 68 additions and 53 deletions.
7 changes: 0 additions & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,6 @@ module.exports = {
'no-new': 'off',
},
},
{
files: ['packages/tests/src/mocks/**/*.*'],
rules: {
'func-names': 'off',
'no-unused-expressions': 'off',
},
},
{
files: ['packages/tools/**/*.*'],
rules: {
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
"repository": "git://github.com/DataDog/build-plugins.git",
"workspaces": [
"packages/*",
"packages/tests/src/mocks/projects/*",
"packages/tests/src/mocks/projects/*/workspaces/*",
"packages/plugins/*"
],
"volta": {
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import c from 'chalk';

import type { LogLevel } from './types';

const log = (text: string, level: LogLevel, type: LogLevel = 'debug', name?: string) => {
const log = (text: any, level: LogLevel, type: LogLevel = 'debug', name?: string) => {
let color = c;
// eslint-disable-next-line no-console
let logFn = console.log;
Expand All @@ -28,11 +28,12 @@ const log = (text: string, level: LogLevel, type: LogLevel = 'debug', name?: str
(level === 'warn' && ['error', 'warn'].includes(type)) ||
(level === 'error' && type === 'error')
) {
logFn(`${prefix}${color(text)}`);
const content = typeof text === 'string' ? text : JSON.stringify(text, null, 2);
logFn(`${prefix}${color(content)}`);
}
};

export const getLogFn =
(level: LogLevel = 'warn', name?: string) =>
(text: string, type: LogLevel = 'debug') =>
(text: any, type: LogLevel = 'debug') =>
log(text, level, type, name);
20 changes: 2 additions & 18 deletions packages/tests/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
# Tests

All the workspaces are tested in this workspace.<br/>
It helps us have a better control over the test specific dependencies, and more importantly, centralize the mockings.<br/>
Especially useful for having mock projects, built with specific bundlers and run the real thing.<br/>
Right now we have mock projects for:
It helps us have a better control over the test specific dependencies.

- [ESBuild](./src/mocks/projects/esbuild)
- [Webpack 4](./src/mocks/projects/webpack4)
- [Webpack 5](./src/mocks/projects/webpack5)

## Use a mock project

You should build the project part of your test suite.

```js
beforeAll(async () => {
const output = await exec(`yarn workspace project-{{bundler}} build`);

// Setup what you need.
}, 20000);
```
Especially useful for having mock projects, built with specific bundlers and run the real thing.

## Build everything & Run

Expand Down
7 changes: 6 additions & 1 deletion packages/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
"url": "https://github.com/DataDog/build-plugins",
"directory": "packages/tests"
},
"workspaces": [
"src/plugins/**/mocks/projects/*",
"src/plugins/**/mocks/projects/*/workspaces/*"
],
"exports": {
"./*": "./src/*.ts"
"./*": "./src/*.ts",
"./plugins/telemetry/*": "./src/plugins/telemetry/*.ts"
},
"scripts": {
"build": "yarn clean && tsc",
Expand Down
11 changes: 11 additions & 0 deletions packages/tests/src/plugins/telemetry/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
overrides: [
{
files: ['mocks/**/*.*'],
rules: {
'func-names': 'off',
'no-unused-expressions': 'off',
},
},
],
};
19 changes: 19 additions & 0 deletions packages/tests/src/plugins/telemetry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Telemetry Plugin Tests

Right now we have mock projects for:

- [ESBuild](./src/mocks/projects/esbuild)
- [Webpack 4](./src/mocks/projects/webpack4)
- [Webpack 5](./src/mocks/projects/webpack5)

## Use a mock project

You should build the project part of your test suite.

```js
beforeAll(async () => {
const output = await exec(`yarn workspace project-{{bundler}} build`);

// Setup what you need.
}, 20000);
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
// Copyright 2019-Present Datadog, Inc.

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

describe('Telemetry Aggregator', () => {
test('It should aggregate metrics without throwing.', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
getAssets,
} from '@dd/telemetry-plugins/common/metrics/esbuild';
import type { Metric } from '@dd/telemetry-plugins/types';
import { exec, PROJECTS_ROOT } from '@dd/tests/testHelpers';
import { exec, PROJECTS_ROOT } from '@dd/tests/plugins/telemetry/testHelpers';
import path from 'path';

describe('Telemetry ESBuild Metrics', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getAssets,
} from '@dd/telemetry-plugins/common/metrics/webpack';
import type { Metric } from '@dd/telemetry-plugins/types';
import { exec, PROJECTS_ROOT } from '@dd/tests/testHelpers';
import { exec, PROJECTS_ROOT } from '@dd/tests/plugins/telemetry/testHelpers';
import path from 'path';

describe('Telemetry Webpack Metrics', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { outputFiles } from '@dd/telemetry-plugins/common/output/files';
import type { OutputOptions } from '@dd/telemetry-plugins/types';
import { mockOptions, mockReport } from '@dd/tests/testHelpers';
import { mockOptions, mockReport } from '@dd/tests/plugins/telemetry/testHelpers';
import fs from 'fs-extra';
import path from 'path';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright 2019-Present Datadog, Inc.

import { datadogEsbuildPlugin } from '@datadog/esbuild-plugin';
import { mockBuild, mockOptions } from '@dd/tests/testHelpers';
import { mockBuild, mockOptions } from '@dd/tests/plugins/telemetry/testHelpers';

describe('Telemetry ESBuild Plugin', () => {
test('It should not execute if disabled', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import type { LocalModule } from '@dd/core/types';
import { getModulesResults } from '@dd/telemetry-plugins/common/modules';
import { mockMetaFile } from '@dd/tests/testHelpers';
import { mockMetaFile } from '@dd/tests/plugins/telemetry/testHelpers';

describe('Telemetry ESBuild Modules', () => {
test('It should add module size to the results', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright 2019-Present Datadog, Inc.

import { wrapPlugins, getResults } from '@dd/telemetry-plugins/esbuild-plugin/plugins';
import { getMockBuild } from '@dd/tests/testHelpers';
import { getMockBuild } from '@dd/tests/plugins/telemetry/testHelpers';
import type { PluginBuild, Plugin } from 'esbuild';

describe('Telemetry ESBuild Plugins', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (!ROOT) {
throw new Error('Please update the usage of `process.env.PROJECT_CWD`.');
}

export const PROJECTS_ROOT = path.join(ROOT, 'packages/tests/src/mocks/projects');
export const PROJECTS_ROOT = path.join(ROOT, 'packages/tests/src/plugins/telemetry/mocks/projects');
export const exec = require('util').promisify(require('child_process').exec);

export const getMockBuild = (overrides: Partial<PluginBuild>): PluginBuild => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright 2019-Present Datadog, Inc.

import { datadogWebpackPlugin } from '@datadog/webpack-plugin';
import { mockCompiler, mockOptions } from '@dd/tests/testHelpers';
import { mockCompiler, mockOptions } from '@dd/tests/plugins/telemetry/testHelpers';

describe('Telemetry Webpack Plugin', () => {
test('It should not execute if disabled', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

describe('Telemetry Modules', () => {
// Webpack5 is actually throwing an error when using this property.
Expand Down
24 changes: 12 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9356,9 +9356,9 @@ __metadata:
languageName: node
linkType: hard

"project-esbuild@workspace:packages/tests/src/mocks/projects/esbuild":
"project-esbuild@workspace:packages/tests/src/plugins/telemetry/mocks/projects/esbuild":
version: 0.0.0-use.local
resolution: "project-esbuild@workspace:packages/tests/src/mocks/projects/esbuild"
resolution: "project-esbuild@workspace:packages/tests/src/plugins/telemetry/mocks/projects/esbuild"
dependencies:
"@datadog/esbuild-plugin": "workspace:*"
"@types/node": "npm:13.13.4"
Expand All @@ -9369,9 +9369,9 @@ __metadata:
languageName: unknown
linkType: soft

"project-webpack4@workspace:packages/tests/src/mocks/projects/webpack4":
"project-webpack4@workspace:packages/tests/src/plugins/telemetry/mocks/projects/webpack4":
version: 0.0.0-use.local
resolution: "project-webpack4@workspace:packages/tests/src/mocks/projects/webpack4"
resolution: "project-webpack4@workspace:packages/tests/src/plugins/telemetry/mocks/projects/webpack4"
dependencies:
"@datadog/webpack-plugin": "workspace:*"
webpack: "npm:4.44.2"
Expand All @@ -9380,9 +9380,9 @@ __metadata:
languageName: unknown
linkType: soft

"project-webpack5@workspace:packages/tests/src/mocks/projects/webpack5":
"project-webpack5@workspace:packages/tests/src/plugins/telemetry/mocks/projects/webpack5":
version: 0.0.0-use.local
resolution: "project-webpack5@workspace:packages/tests/src/mocks/projects/webpack5"
resolution: "project-webpack5@workspace:packages/tests/src/plugins/telemetry/mocks/projects/webpack5"
dependencies:
"@datadog/webpack-plugin": "workspace:*"
webpack: "npm:5.1.3"
Expand Down Expand Up @@ -10641,9 +10641,9 @@ __metadata:
languageName: node
linkType: hard

"sub_app@workspace:*, sub_app@workspace:packages/tests/src/mocks/projects/esbuild/workspaces/app":
"sub_app@workspace:*, sub_app@workspace:packages/tests/src/plugins/telemetry/mocks/projects/esbuild/workspaces/app":
version: 0.0.0-use.local
resolution: "sub_app@workspace:packages/tests/src/mocks/projects/esbuild/workspaces/app"
resolution: "sub_app@workspace:packages/tests/src/plugins/telemetry/mocks/projects/esbuild/workspaces/app"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -11557,15 +11557,15 @@ __metadata:
languageName: node
linkType: hard

"webpack4_app@workspace:*, webpack4_app@workspace:packages/tests/src/mocks/projects/webpack4/workspaces/app":
"webpack4_app@workspace:*, webpack4_app@workspace:packages/tests/src/plugins/telemetry/mocks/projects/webpack4/workspaces/app":
version: 0.0.0-use.local
resolution: "webpack4_app@workspace:packages/tests/src/mocks/projects/webpack4/workspaces/app"
resolution: "webpack4_app@workspace:packages/tests/src/plugins/telemetry/mocks/projects/webpack4/workspaces/app"
languageName: unknown
linkType: soft

"webpack5_app@workspace:*, webpack5_app@workspace:packages/tests/src/mocks/projects/webpack5/workspaces/app":
"webpack5_app@workspace:*, webpack5_app@workspace:packages/tests/src/plugins/telemetry/mocks/projects/webpack5/workspaces/app":
version: 0.0.0-use.local
resolution: "webpack5_app@workspace:packages/tests/src/mocks/projects/webpack5/workspaces/app"
resolution: "webpack5_app@workspace:packages/tests/src/plugins/telemetry/mocks/projects/webpack5/workspaces/app"
languageName: unknown
linkType: soft

Expand Down

0 comments on commit d3bded7

Please sign in to comment.