10
10
11
11
/*global jest, describe, beforeEach, it, expect*/
12
12
13
- "use strict" ;
14
-
15
13
jest . autoMockOff ( ) ;
16
14
17
15
describe ( 'resolveToValue' , ( ) => {
18
- var astNodesAreEquivalent ;
19
16
var builders ;
20
17
var utils ;
21
18
var resolveToValue ;
@@ -27,7 +24,6 @@ describe('resolveToValue', () => {
27
24
28
25
beforeEach ( ( ) => {
29
26
var recast = require ( 'recast' ) ;
30
- astNodesAreEquivalent = recast . types . astNodesAreEquivalent ;
31
27
builders = recast . types . builders ;
32
28
resolveToValue = require ( '../resolveToValue' ) ;
33
29
utils = require ( '../../../tests/utils' ) ;
@@ -36,72 +32,77 @@ describe('resolveToValue', () => {
36
32
it ( 'resolves simple variable declarations' , ( ) => {
37
33
var path = parse ( [
38
34
'var foo = 42;' ,
39
- 'foo;'
35
+ 'foo;' ,
40
36
] . join ( '\n' ) ) ;
41
- expect ( astNodesAreEquivalent (
42
- resolveToValue ( path ) . node ,
43
- builders . literal ( 42 )
44
- ) ) . toBe ( true ) ;
37
+ expect ( resolveToValue ( path ) . node ) . toEqualASTNode ( builders . literal ( 42 ) ) ;
45
38
} ) ;
46
39
47
40
it ( 'resolves object destructuring' , ( ) => {
48
41
var path = parse ( [
49
42
'var {foo: {bar: baz}} = bar;' ,
50
- 'baz;'
43
+ 'baz;' ,
51
44
] . join ( '\n' ) ) ;
52
45
53
46
// Node should be equal to bar.foo.bar
54
- expect ( astNodesAreEquivalent (
55
- resolveToValue ( path ) . node ,
47
+ expect ( resolveToValue ( path ) . node ) . toEqualASTNode (
56
48
builders . memberExpression (
57
49
builders . memberExpression (
58
50
builders . identifier ( 'bar' ) ,
59
51
builders . identifier ( 'foo' )
60
52
) ,
61
53
builders . identifier ( 'bar' )
62
54
)
63
- ) ) . toBe ( true ) ;
55
+ ) ;
64
56
} ) ;
65
57
66
58
it ( 'handles SpreadProperties properly' , ( ) => {
67
59
var path = parse ( [
68
60
'var {foo: {bar}, ...baz} = bar;' ,
69
- 'baz;'
61
+ 'baz;' ,
70
62
] . join ( '\n' ) ) ;
71
63
72
- expect ( astNodesAreEquivalent (
73
- resolveToValue ( path ) . node ,
74
- path . node
75
- ) ) . toBe ( true ) ;
64
+ expect ( resolveToValue ( path ) . node ) . toEqualASTNode ( path . node ) ;
76
65
} ) ;
77
66
78
67
it ( 'returns the original path if it cannot be resolved' , ( ) => {
79
68
var path = parse ( [
80
69
'function foo() {}' ,
81
- 'foo()'
70
+ 'foo()' ,
82
71
] . join ( '\n' ) ) ;
83
72
84
- expect ( astNodesAreEquivalent (
85
- resolveToValue ( path ) . node ,
86
- path . node
87
- ) ) . toBe ( true ) ;
73
+ expect ( resolveToValue ( path ) . node ) . toEqualASTNode ( path . node ) ;
88
74
} ) ;
89
75
90
76
it ( 'resolves variable declarators to their init value' , ( ) => {
91
77
var path = utils . parse ( 'var foo = 42;' ) . get ( 'body' , 0 , 'declarations' , 0 ) ;
92
78
93
- expect ( astNodesAreEquivalent (
94
- resolveToValue ( path ) . node ,
95
- builders . literal ( 42 )
96
- ) ) . toBe ( true ) ;
79
+ expect ( resolveToValue ( path ) . node ) . toEqualASTNode ( builders . literal ( 42 ) ) ;
80
+ } ) ;
81
+
82
+ it ( 'resolves to class declarations' , ( ) => {
83
+ var program = utils . parse ( `
84
+ class Foo {}
85
+ Foo;
86
+ ` ) ;
87
+ expect ( resolveToValue ( program . get ( 'body' , 1 , 'expression' ) ) . node . type )
88
+ . toBe ( 'ClassDeclaration' ) ;
89
+ } ) ;
90
+
91
+ it ( 'resolves to class function declaration' , ( ) => {
92
+ var program = utils . parse ( `
93
+ function foo() {}
94
+ foo;
95
+ ` ) ;
96
+ expect ( resolveToValue ( program . get ( 'body' , 1 , 'expression' ) ) . node . type )
97
+ . toBe ( 'FunctionDeclaration' ) ;
97
98
} ) ;
98
99
99
100
describe ( 'ImportDeclaration' , ( ) => {
100
101
101
102
it ( 'resolves default import references to the import declaration' , ( ) => {
102
103
var path = parse ( [
103
104
'import foo from "Foo"' ,
104
- 'foo;'
105
+ 'foo;' ,
105
106
] . join ( '\n' ) ) ;
106
107
107
108
expect ( resolveToValue ( path ) . node . type ) . toBe ( 'ImportDeclaration' ) ;
@@ -110,7 +111,7 @@ describe('resolveToValue', () => {
110
111
it ( 'resolves named import references to the import declaration' , ( ) => {
111
112
var path = parse ( [
112
113
'import {foo} from "Foo"' ,
113
- 'foo;'
114
+ 'foo;' ,
114
115
] . join ( '\n' ) ) ;
115
116
116
117
expect ( resolveToValue ( path ) . node . type ) . toBe ( 'ImportDeclaration' ) ;
@@ -119,7 +120,7 @@ describe('resolveToValue', () => {
119
120
it ( 'resolves aliased import references to the import declaration' , ( ) => {
120
121
var path = parse ( [
121
122
'import {foo as bar} from "Foo"' ,
122
- 'bar;'
123
+ 'bar;' ,
123
124
] . join ( '\n' ) ) ;
124
125
125
126
expect ( resolveToValue ( path ) . node . type ) . toBe ( 'ImportDeclaration' ) ;
0 commit comments