Skip to content

Commit 1c66df4

Browse files
committed
test: unit tests for babel plugin helpers
1 parent 8655cdb commit 1c66df4

File tree

3 files changed

+164
-1
lines changed

3 files changed

+164
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { getLocationDescription } from '../../src/utils/getLocationDescription';
2+
3+
describe('getLocationDescription', () => {
4+
it('returns correct location for NodePath with loc', () => {
5+
const path = {
6+
node: {
7+
loc: {
8+
start: { line: 1, column: 2 },
9+
end: { line: 3, column: 4 },
10+
},
11+
},
12+
type: 'NodePath',
13+
} as any;
14+
const pluginPass = { filename: 'file.ts' } as any;
15+
expect(getLocationDescription(path, pluginPass)).toBe(
16+
'file.ts:1:2 through 3:4'
17+
);
18+
});
19+
20+
it('returns correct location for ClassMethod', () => {
21+
const classMethod = {
22+
type: 'ClassMethod',
23+
loc: {
24+
start: { line: 10, column: 20 },
25+
end: { line: 30, column: 40 },
26+
},
27+
} as any;
28+
const pluginPass = { filename: 'class.ts' } as any;
29+
expect(getLocationDescription(classMethod, pluginPass)).toBe(
30+
'class.ts:10:20 through 30:40'
31+
);
32+
});
33+
34+
it('handles missing loc gracefully', () => {
35+
const path = {
36+
node: {},
37+
type: 'NodePath',
38+
} as any;
39+
const pluginPass = { filename: 'nofile.ts' } as any;
40+
expect(getLocationDescription(path, pluginPass)).toBe(
41+
'nofile.ts:undefined:undefined through undefined:undefined'
42+
);
43+
});
44+
});

packages/instrumentation-react/tests/utils.test.ts renamed to packages/instrumentation-react/tests/utils/misc.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { uniqueVarName } from '../src/utils';
1+
import { uniqueVarName } from '../../src/utils/misc';
22

33
describe('Utils', () => {
44
describe('uniqueVarName', () => {
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import * as t from '@babel/types';
2+
3+
import { parseDirective } from '../../src/utils/parseDirective';
4+
5+
describe('parseDirective', () => {
6+
const makePath = (node: any, parent: any = {}) => ({ node, parent });
7+
const makeDirective = (value: string) =>
8+
({ value: { value } }) as t.Directive;
9+
10+
it('parses use trace dev', () => {
11+
const path = makePath({ id: { name: 'MyComponent' } });
12+
const directive = makeDirective('use trace dev');
13+
const result = parseDirective(path as any, directive, 'loc');
14+
expect(result).toEqual({
15+
componentName: 'MyComponent',
16+
isFunctionComponent: true,
17+
apiToUse: 'dev',
18+
});
19+
});
20+
21+
it('parses use trace otel', () => {
22+
const path = makePath({ id: { name: 'MyComponent' } });
23+
const directive = makeDirective('use trace otel');
24+
const result = parseDirective(path as any, directive, 'loc');
25+
expect(result.apiToUse).toBe('otel');
26+
expect(result.componentName).toBe('MyComponent');
27+
expect(result.isFunctionComponent).toBe(true);
28+
});
29+
30+
it('parses use trace dev MyComponent', () => {
31+
const path = makePath({ id: { name: 'OtherComponent' } });
32+
const directive = makeDirective('use trace dev MyComponent');
33+
const result = parseDirective(path as any, directive, 'loc');
34+
expect(result.apiToUse).toBe('dev');
35+
expect(result.componentName).toBe('MyComponent');
36+
expect(result.isFunctionComponent).toBe(true);
37+
});
38+
39+
it('parses use trace otel MyComponent', () => {
40+
const path = makePath({ id: { name: 'OtherComponent' } });
41+
const directive = makeDirective('use trace otel MyComponent');
42+
const result = parseDirective(path as any, directive, 'loc');
43+
expect(result.apiToUse).toBe('otel');
44+
expect(result.componentName).toBe('MyComponent');
45+
expect(result.isFunctionComponent).toBe(true);
46+
});
47+
48+
it('parses use trace MyComponent', () => {
49+
const path = makePath({ id: { name: 'OtherComponent' } });
50+
const directive = makeDirective('use trace MyComponent');
51+
const result = parseDirective(path as any, directive, 'loc');
52+
expect(result.apiToUse).toBe('dev');
53+
expect(result.componentName).toBe('MyComponent');
54+
expect(result.isFunctionComponent).toBe(true);
55+
});
56+
57+
it('throws on invalid API', () => {
58+
const path = makePath({ id: { name: 'OtherComponent' } });
59+
const directive = makeDirective('use trace invalidAPI MyComponent');
60+
expect(() => parseDirective(path as any, directive, 'loc')).toThrow(
61+
/Invalid tracing API specified/
62+
);
63+
});
64+
65+
it('throws on anonymous export default', () => {
66+
const path = makePath({}, { type: 'ExportDefaultDeclaration' });
67+
const directive = makeDirective('use trace dev');
68+
expect(() => parseDirective(path as any, directive, 'loc')).toThrow(
69+
/anonymous default export/
70+
);
71+
});
72+
73+
it('infers name from variable assignment', () => {
74+
const path = makePath(
75+
{},
76+
{
77+
type: 'VariableDeclarator',
78+
id: { type: 'Identifier', name: 'VarComponent' },
79+
}
80+
);
81+
const directive = makeDirective('use trace dev');
82+
const result = parseDirective(path as any, directive, 'loc');
83+
expect(result.componentName).toBe('VarComponent');
84+
});
85+
86+
it('infers name from object property', () => {
87+
const path = makePath(
88+
{},
89+
{
90+
type: 'ObjectProperty',
91+
key: { type: 'Identifier', name: 'ObjComponent' },
92+
}
93+
);
94+
const directive = makeDirective('use trace dev');
95+
const result = parseDirective(path as any, directive, 'loc');
96+
expect(result.componentName).toBe('ObjComponent');
97+
});
98+
99+
it('infers name from class method and sets isFunctionComponent false', () => {
100+
const path = makePath(
101+
{},
102+
{
103+
type: 'ClassMethod',
104+
key: { type: 'Identifier', name: 'MethodComponent' },
105+
}
106+
);
107+
const directive = makeDirective('use trace dev');
108+
const result = parseDirective(path as any, directive, 'loc');
109+
expect(result.componentName).toBe('MethodComponent');
110+
expect(result.isFunctionComponent).toBe(false);
111+
});
112+
113+
it('returns unknown if no name found', () => {
114+
const path = makePath({});
115+
const directive = makeDirective('use trace dev');
116+
const result = parseDirective(path as any, directive, 'loc');
117+
expect(result.componentName).toBe('unknown');
118+
});
119+
});

0 commit comments

Comments
 (0)