Skip to content

Commit

Permalink
test: Add stronger jest tests for inference
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Sep 14, 2017
1 parent adf1bd0 commit 699e44b
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
54 changes: 50 additions & 4 deletions __tests__/lib/infer/__snapshots__/params.js.snap
Expand Up @@ -30,6 +30,52 @@ Array [
`;

exports[`inferParams 2`] = `
Array [
Object {
"anonymous": true,
"name": "$0",
"properties": Array [
Object {
"lineNumber": 1,
"name": "$0.0",
"title": "param",
"type": Object {
"name": "string",
"type": "NameExpression",
},
},
Object {
"lineNumber": 1,
"name": "$0.1",
"title": "param",
},
Object {
"anonymous": true,
"name": "$0.2",
"properties": Array [
Object {
"lineNumber": 1,
"name": "$0.2.c",
"title": "param",
},
],
"title": "param",
"type": Object {
"name": "Object",
"type": "NameExpression",
},
},
],
"title": "param",
"type": Object {
"name": "Array",
"type": "NameExpression",
},
},
]
`;

exports[`inferParams 3`] = `
Array [
Object {
"description": Object {
Expand Down Expand Up @@ -102,7 +148,7 @@ Array [
]
`;

exports[`inferParams 3`] = `
exports[`inferParams 4`] = `
Array [
Object {
"default": "4",
Expand All @@ -114,7 +160,7 @@ Array [
]
`;

exports[`inferParams 4`] = `
exports[`inferParams 5`] = `
Array [
Object {
"default": "4",
Expand All @@ -129,7 +175,7 @@ Array [
]
`;

exports[`inferParams 5`] = `
exports[`inferParams 6`] = `
Array [
Object {
"anonymous": true,
Expand All @@ -150,7 +196,7 @@ Array [
]
`;

exports[`inferParams 6`] = `
exports[`inferParams 7`] = `
Array [
Object {
"anonymous": true,
Expand Down
12 changes: 12 additions & 0 deletions __tests__/lib/infer/name.js
Expand Up @@ -62,6 +62,18 @@ test('inferName', function() {
}).name
).toBe('name');

expect(
evaluate(function() {
exports = {
// Property
// Identifier (comment attached here)
// FunctionExpression
/** Test */
name() {}
};
}).name
).toBe('name');

expect(
evaluate(function() {
/** Test */
Expand Down
4 changes: 4 additions & 0 deletions __tests__/lib/infer/params.js
Expand Up @@ -93,6 +93,10 @@ test('inferParams', function() {
evaluate(`/** Test */function f({ x, ...xs }) {};`).params
).toMatchSnapshot();

expect(
evaluate(`/** Test */function f([a: string, b, {c}]) {};`).params
).toMatchSnapshot();

expect(
evaluate(
`
Expand Down
44 changes: 44 additions & 0 deletions __tests__/lib/infer/return.js
@@ -0,0 +1,44 @@
/*eslint-disable no-unused-vars*/
var inferReturn = require('../../../src/infer/return'),
parse = require('../../../src/parsers/javascript');

function toComment(fn, filename) {
return parse(
{
file: filename,
source: fn instanceof Function ? '(' + fn.toString() + ')' : fn
},
{}
)[0];
}

function evaluate(code) {
return inferReturn(toComment(code));
}

test('inferReturn', function() {
expect(evaluate('/** */function a(): number {}').returns).toEqual([
{
title: 'returns',
type: {
name: 'number',
type: 'NameExpression'
}
}
]);
expect(evaluate('/** */var a = function(): number {}').returns).toEqual([
{
title: 'returns',
type: {
name: 'number',
type: 'NameExpression'
}
}
]);
expect(
evaluate('/** @returns {string} */function a(): number {}').returns[0].type
).toEqual({
name: 'string',
type: 'NameExpression'
});
});

0 comments on commit 699e44b

Please sign in to comment.