-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathstartTestAgent.ts
More file actions
39 lines (34 loc) · 1.32 KB
/
startTestAgent.ts
File metadata and controls
39 lines (34 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import type { ReportingAPI } from "../agent/api/ReportingAPI";
import type { Token } from "../agent/api/Token";
import { __internalRewritePackageName } from "../agent/hooks/wrapRequire";
import type { Logger } from "../agent/logger/Logger";
import { Wrapper } from "../agent/Wrapper";
import { createTestAgent } from "./createTestAgent";
type PackageName = string;
type AliasToRequire = string;
/**
* Start a test agent for testing purposes
*/
export function startTestAgent(opts: {
block?: boolean;
logger?: Logger;
api?: ReportingAPI;
token?: Token;
serverless?: string;
wrappers: Wrapper[];
rewrite: Record<PackageName, AliasToRequire>;
}) {
const agent = createTestAgent(opts);
agent.start(opts.wrappers);
if (!agent.isUsingNewInstrumentation()) {
// In order to support multiple versions of the same package, we need to rewrite the package name
// e.g. In our sources and sinks, we use the real package name `hooks.addPackage("undici")`
// but in the tests we want to `require("undici-v6")` instead of `require("undici")`
// The `__internalRewritePackageName` function allows us to do this
Object.keys(opts.rewrite).forEach((packageName) => {
__internalRewritePackageName(packageName, opts.rewrite[packageName]);
});
}
// Todo support this in the new instrumentation
return agent;
}