Skip to content

Commit d207977

Browse files
committed
add test for arguments mutation
1 parent 339d1f8 commit d207977

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

tests/arguments_mutation/mod.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2025 Datadog, Inc.
4+
**/
5+
const assert = require('assert');
6+
7+
function fetch_simple (url, cb) {
8+
assert.strictEqual(this.this, 'this');
9+
assert.strictEqual(url, 'https://example.com');
10+
assert.strictEqual(cb.length, 2);
11+
const result = cb.apply(this, ['arg1', 'arg2']);
12+
assert.strictEqual(result, 'result');
13+
return 'return';
14+
}
15+
16+
function fetch_complex ({ url, tuple: [a = 'a', b = 'b'] }, cb, optional = 'default', ...rest) {
17+
assert.strictEqual(this.this, 'this');
18+
assert.strictEqual(url, 'https://example.com');
19+
assert.strictEqual(a, 'a');
20+
assert.strictEqual(b, 'b');
21+
assert.strictEqual(cb.length, 2);
22+
assert.strictEqual(optional, 'default');
23+
assert.deepStrictEqual(rest, []);
24+
const result = cb.apply(this, ['arg1', 'arg2']);
25+
assert.strictEqual(result, 'result');
26+
return 'return';
27+
}
28+
29+
30+
module.exports = { fetch_simple, fetch_complex };

tests/arguments_mutation/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use crate::common::*;
2+
use orchestrion_js::*;
3+
4+
#[test]
5+
fn arguments_mutation() {
6+
transpile_and_test(
7+
file!(),
8+
false,
9+
Config::new(
10+
vec![
11+
InstrumentationConfig::new(
12+
"fetch_simple",
13+
test_module_matcher(),
14+
FunctionQuery::function_declaration("fetch_simple", FunctionKind::Sync),
15+
),
16+
InstrumentationConfig::new(
17+
"fetch_complex",
18+
test_module_matcher(),
19+
FunctionQuery::function_declaration("fetch_complex", FunctionKind::Sync),
20+
),
21+
],
22+
None,
23+
),
24+
);
25+
}

tests/arguments_mutation/test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2025 Datadog, Inc.
4+
**/
5+
const { fetch_simple, fetch_complex } = require('./instrumented.js');
6+
const assert = require('assert');
7+
const { tracingChannel } = require('diagnostics_channel');
8+
9+
const handler = {
10+
start (message) {
11+
const originalCb = message.arguments[1];
12+
const wrappedCb = function (a, b) {
13+
assert.strictEqual(this.this, 'this');
14+
assert.strictEqual(a, 'arg1');
15+
assert.strictEqual(b, 'arg2');
16+
arguments[1] = 'arg2_mutated';
17+
return originalCb.apply(this, arguments);
18+
}
19+
20+
message.arguments[1] = wrappedCb;
21+
}
22+
};
23+
24+
tracingChannel('orchestrion:undici:fetch_simple').subscribe(handler);
25+
tracingChannel('orchestrion:undici:fetch_complex').subscribe(handler);
26+
27+
28+
assert.strictEqual(fetch_simple.length, 2);
29+
assert.strictEqual(fetch_complex.length, 2);
30+
31+
const cb = function (a, b) {
32+
assert.strictEqual(this.this, 'this');
33+
assert.strictEqual(a, 'arg1');
34+
assert.strictEqual(b, 'arg2_mutated');
35+
return 'result';
36+
};
37+
38+
assert.strictEqual(fetch_simple.apply({ this: 'this' }, ['https://example.com', cb]), 'return');
39+
assert.strictEqual(fetch_complex.apply({ this: 'this' }, [{ url: 'https://example.com' }, cb]), 'return');

tests/instrumentor_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
**/
55
mod common;
66

7+
mod arguments_mutation;
78
mod class_method_cjs;
89
mod constructor_cjs;
910
mod constructor_mjs;

0 commit comments

Comments
 (0)