-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add parseExpect() util function
- Loading branch information
Showing
4 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
'use strict'; | ||
|
||
const espree = require('espree'); | ||
const estraverse = require('estraverse'); | ||
const parseExpect = require('../parse-expect'); | ||
|
||
const CASES = { | ||
notExpect(parsed) { | ||
expect(parsed).toBeNull(); | ||
}, | ||
'expect()'(parsed) { | ||
expect(parsed).toMatchObject({ | ||
arguments: [], | ||
properties: [], | ||
matcher: undefined, | ||
matcherArguments: [], | ||
}); | ||
}, | ||
'expect().toBe'(parsed) { | ||
expect(parsed).toMatchObject({ | ||
arguments: [], | ||
properties: [Identifier('toBe')], | ||
matcher: undefined, | ||
matcherArguments: [], | ||
}); | ||
}, | ||
'expect().toBe()'(parsed) { | ||
expect(parsed).toMatchObject({ | ||
arguments: [], | ||
properties: [], | ||
matcher: Identifier('toBe'), | ||
matcherArguments: [], | ||
}); | ||
}, | ||
'expect(Promise.resolve(2)).not.resolves.toEqual(3)'(parsed) { | ||
expect(parsed).toMatchObject({ | ||
arguments: [ | ||
CallExpression(Identifier('Promise'), Identifier('resolve'), [ | ||
Literal(2), | ||
]), | ||
], | ||
properties: [Identifier('not'), Identifier('resolves')], | ||
matcher: Identifier('toEqual'), | ||
matcherArguments: [Literal(3)], | ||
}); | ||
}, | ||
'expect().a.b.c.d.e.f()'(parsed) { | ||
expect(parsed).toMatchObject({ | ||
arguments: [], | ||
properties: [ | ||
Identifier('a'), | ||
Identifier('b'), | ||
Identifier('c'), | ||
Identifier('d'), | ||
Identifier('e'), | ||
], | ||
matcher: Identifier('f'), | ||
matcherArguments: [], | ||
}); | ||
}, | ||
}; | ||
|
||
Object.keys(CASES).forEach(code => { | ||
test(code, () => { | ||
const assertion = CASES[code]; | ||
const node = findExpect(code); | ||
assertion(parseExpect(node)); | ||
}); | ||
}); | ||
|
||
function findExpect(code) { | ||
let callExpression = null; | ||
|
||
const ast = espree.parse(code); | ||
estraverse.traverse(ast, { | ||
enter(node, parent) { | ||
node.parent = parent; | ||
if ( | ||
!callExpression && | ||
node.type === 'CallExpression' && | ||
node.callee.name === 'expect' | ||
) { | ||
callExpression = node; | ||
} | ||
}, | ||
}); | ||
|
||
return callExpression; | ||
} | ||
|
||
function Identifier(name) { | ||
return { | ||
type: 'Identifier', | ||
name, | ||
}; | ||
} | ||
|
||
function Literal(value) { | ||
return { | ||
type: 'Literal', | ||
value, | ||
}; | ||
} | ||
|
||
function CallExpression(object, property, args) { | ||
return { | ||
type: 'CallExpression', | ||
callee: { | ||
object, | ||
property, | ||
}, | ||
arguments: args || [], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
module.exports = function parseExpect(node) { | ||
if (node && node.type === 'CallExpression' && node.callee.name === 'expect') { | ||
const properties = []; | ||
let matcherArguments = []; | ||
let matcher = undefined; | ||
let parent = node.parent; | ||
while (parent) { | ||
if (parent.type === 'MemberExpression') { | ||
const grandParentType = parent.parent && parent.parent.type; | ||
switch (grandParentType) { | ||
case 'CallExpression': { | ||
matcher = parent.property; | ||
matcherArguments = matcherArguments.concat(parent.parent.arguments); | ||
break; | ||
} | ||
case 'MemberExpression': | ||
case 'ExpressionStatement': { | ||
properties.push(parent.property); | ||
break; | ||
} | ||
} | ||
} | ||
parent = parent.parent; | ||
} | ||
return { | ||
arguments: node.arguments, | ||
properties, | ||
matcher, | ||
matcherArguments, | ||
}; | ||
} | ||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters