Skip to content

Commit 8779369

Browse files
committed
test: Add stronger jest tests for inference
1 parent adf1bd0 commit 8779369

File tree

4 files changed

+110
-4
lines changed

4 files changed

+110
-4
lines changed

__tests__/lib/infer/__snapshots__/params.js.snap

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,52 @@ Array [
3030
`;
3131

3232
exports[`inferParams 2`] = `
33+
Array [
34+
Object {
35+
"anonymous": true,
36+
"name": "$0",
37+
"properties": Array [
38+
Object {
39+
"lineNumber": 1,
40+
"name": "$0.0",
41+
"title": "param",
42+
"type": Object {
43+
"name": "string",
44+
"type": "NameExpression",
45+
},
46+
},
47+
Object {
48+
"lineNumber": 1,
49+
"name": "$0.1",
50+
"title": "param",
51+
},
52+
Object {
53+
"anonymous": true,
54+
"name": "$0.2",
55+
"properties": Array [
56+
Object {
57+
"lineNumber": 1,
58+
"name": "$0.2.c",
59+
"title": "param",
60+
},
61+
],
62+
"title": "param",
63+
"type": Object {
64+
"name": "Object",
65+
"type": "NameExpression",
66+
},
67+
},
68+
],
69+
"title": "param",
70+
"type": Object {
71+
"name": "Array",
72+
"type": "NameExpression",
73+
},
74+
},
75+
]
76+
`;
77+
78+
exports[`inferParams 3`] = `
3379
Array [
3480
Object {
3581
"description": Object {
@@ -102,7 +148,7 @@ Array [
102148
]
103149
`;
104150

105-
exports[`inferParams 3`] = `
151+
exports[`inferParams 4`] = `
106152
Array [
107153
Object {
108154
"default": "4",
@@ -114,7 +160,7 @@ Array [
114160
]
115161
`;
116162

117-
exports[`inferParams 4`] = `
163+
exports[`inferParams 5`] = `
118164
Array [
119165
Object {
120166
"default": "4",
@@ -129,7 +175,7 @@ Array [
129175
]
130176
`;
131177

132-
exports[`inferParams 5`] = `
178+
exports[`inferParams 6`] = `
133179
Array [
134180
Object {
135181
"anonymous": true,
@@ -150,7 +196,7 @@ Array [
150196
]
151197
`;
152198

153-
exports[`inferParams 6`] = `
199+
exports[`inferParams 7`] = `
154200
Array [
155201
Object {
156202
"anonymous": true,

__tests__/lib/infer/name.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ test('inferName', function() {
6262
}).name
6363
).toBe('name');
6464

65+
expect(
66+
evaluate(function() {
67+
exports = {
68+
// Property
69+
// Identifier (comment attached here)
70+
// FunctionExpression
71+
/** Test */
72+
name() {}
73+
};
74+
}).name
75+
).toBe('name');
76+
6577
expect(
6678
evaluate(function() {
6779
/** Test */

__tests__/lib/infer/params.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ test('inferParams', function() {
9393
evaluate(`/** Test */function f({ x, ...xs }) {};`).params
9494
).toMatchSnapshot();
9595

96+
expect(
97+
evaluate(`/** Test */function f([a: string, b, {c}]) {};`).params
98+
).toMatchSnapshot();
99+
96100
expect(
97101
evaluate(
98102
`

__tests__/lib/infer/return.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*eslint-disable no-unused-vars*/
2+
var inferReturn = require('../../../src/infer/return'),
3+
parse = require('../../../src/parsers/javascript');
4+
5+
function toComment(fn, filename) {
6+
return parse(
7+
{
8+
file: filename,
9+
source: fn instanceof Function ? '(' + fn.toString() + ')' : fn
10+
},
11+
{}
12+
)[0];
13+
}
14+
15+
function evaluate(code) {
16+
return inferReturn(toComment(code));
17+
}
18+
19+
test('inferReturn', function() {
20+
expect(evaluate('/** */function a(): number {}').returns).toEqual([
21+
{
22+
title: 'returns',
23+
type: {
24+
name: 'number',
25+
type: 'NameExpression'
26+
}
27+
}
28+
]);
29+
expect(evaluate('/** */var a = function(): number {}').returns).toEqual([
30+
{
31+
title: 'returns',
32+
type: {
33+
name: 'number',
34+
type: 'NameExpression'
35+
}
36+
}
37+
]);
38+
expect(
39+
evaluate('/** @returns {string} */function a(): number {}').returns[0].type
40+
).toEqual({
41+
name: 'string',
42+
type: 'NameExpression'
43+
});
44+
});

0 commit comments

Comments
 (0)