-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split GlobalContext and Bundler Report plugins
- Loading branch information
1 parent
3140bd4
commit 4eaed23
Showing
4 changed files
with
141 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/tests/src/core/plugins/bundler-report/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2019-Present Datadog, Inc. | ||
|
||
import type { BundlerReport, Options } from '@dd/core/types'; | ||
import { defaultDestination, defaultPluginOptions } from '@dd/tests/helpers/mocks'; | ||
import type { CleanupFn } from '@dd/tests/helpers/runBundlers'; | ||
import { BUNDLERS, runBundlers } from '@dd/tests/helpers/runBundlers'; | ||
|
||
describe('Bundler Report', () => { | ||
// Intercept contexts to verify it at the moment they're used. | ||
const bundlerReports: Record<string, BundlerReport> = {}; | ||
let cleanup: CleanupFn; | ||
beforeAll(async () => { | ||
const pluginConfig: Options = { | ||
...defaultPluginOptions, | ||
// Use a custom plugin to intercept contexts to verify it at the moment they're used. | ||
customPlugins: (opts, context) => { | ||
const bundlerName = context.bundler.fullName; | ||
return [ | ||
{ | ||
name: 'custom-plugin', | ||
writeBundle() { | ||
const config = context.bundler.rawConfig; | ||
bundlerReports[bundlerName] = JSON.parse( | ||
JSON.stringify({ | ||
...context.bundler, | ||
// This is not safe to stringify. | ||
rawConfig: null, | ||
}), | ||
); | ||
bundlerReports[bundlerName].rawConfig = config; | ||
}, | ||
}, | ||
]; | ||
}, | ||
}; | ||
|
||
cleanup = await runBundlers(pluginConfig); | ||
}); | ||
|
||
afterAll(async () => { | ||
await cleanup(); | ||
}); | ||
|
||
describe.each(BUNDLERS)('[$name|$version]', ({ name }) => { | ||
test('Should have the right output directory.', () => { | ||
const report = bundlerReports[name]; | ||
const outDir = report.outDir; | ||
|
||
const expectedOutDir = new RegExp(`^${defaultDestination}/[^/]+/${name}$`); | ||
|
||
expect(outDir).toMatch(expectedOutDir); | ||
}); | ||
|
||
test("Should have the bundler's options object.", () => { | ||
const report = bundlerReports[name]; | ||
const rawConfig = report.rawConfig; | ||
expect(rawConfig).toBeDefined(); | ||
expect(rawConfig).toEqual(expect.any(Object)); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2019-Present Datadog, Inc. | ||
|
||
import type { GlobalContext, Options } from '@dd/core/types'; | ||
import { defaultPluginOptions } from '@dd/tests/helpers/mocks'; | ||
import type { CleanupFn } from '@dd/tests/helpers/runBundlers'; | ||
import { BUNDLERS, runBundlers } from '@dd/tests/helpers/runBundlers'; | ||
|
||
describe('Internal Plugins', () => { | ||
describe('Global Context', () => { | ||
// Intercept contexts to verify it at the moment they're used. | ||
const initialContexts: Record<string, GlobalContext> = {}; | ||
let cleanup: CleanupFn; | ||
|
||
beforeAll(async () => { | ||
const pluginConfig: Options = { | ||
...defaultPluginOptions, | ||
// Use a custom plugin to intercept contexts to verify it at initialization. | ||
customPlugins: (opts, context) => { | ||
const bundlerName = context.bundler.fullName; | ||
initialContexts[bundlerName] = JSON.parse(JSON.stringify(context)); | ||
return []; | ||
}, | ||
}; | ||
|
||
cleanup = await runBundlers(pluginConfig); | ||
}); | ||
|
||
afterAll(async () => { | ||
await cleanup(); | ||
}); | ||
|
||
describe.each(BUNDLERS)('[$name|$version]', ({ name, version }) => { | ||
test('Should have the right initial context.', () => { | ||
const context = initialContexts[name]; | ||
expect(context).toBeDefined(); | ||
expect(context.auth).toEqual(defaultPluginOptions.auth); | ||
expect(context.bundler.name).toBe(name.replace(context.bundler.variant || '', '')); | ||
expect(context.bundler.fullName).toBe(name); | ||
expect(context.cwd).toBe(process.cwd()); | ||
expect(context.version).toBe(version); | ||
}); | ||
}); | ||
}); | ||
}); |