12
12
const fs = require ( 'fs' ) ;
13
13
const path = require ( 'path' ) ;
14
14
15
- function applyTransform ( module , options , input ) {
15
+ function applyTransform ( module , options , input , testOptions = { } ) {
16
16
// Handle ES6 modules using default export for the transform
17
17
const transform = module . default ? module . default : module ;
18
18
19
19
// Jest resets the module registry after each test, so we need to always get
20
20
// a fresh copy of jscodeshift on every test run.
21
21
let jscodeshift = require ( './core' ) ;
22
- if ( module . parser ) {
23
- jscodeshift = jscodeshift . withParser ( module . parser ) ;
22
+ if ( testOptions . parser || module . parser ) {
23
+ jscodeshift = jscodeshift . withParser ( testOptions . parser || module . parser ) ;
24
24
}
25
25
26
26
const output = transform (
@@ -43,13 +43,23 @@ function runSnapshotTest(module, options, input) {
43
43
}
44
44
exports . runSnapshotTest = runSnapshotTest ;
45
45
46
- function runInlineTest ( module , options , input , expectedOutput ) {
47
- const output = applyTransform ( module , options , input ) ;
46
+ function runInlineTest ( module , options , input , expectedOutput , testOptions ) {
47
+ const output = applyTransform ( module , options , input , testOptions ) ;
48
48
expect ( output ) . toEqual ( expectedOutput . trim ( ) ) ;
49
49
return output ;
50
50
}
51
51
exports . runInlineTest = runInlineTest ;
52
52
53
+ function extensionForParser ( parser ) {
54
+ switch ( parser ) {
55
+ case 'ts' :
56
+ case 'tsx' :
57
+ return parser ;
58
+ default :
59
+ return 'js'
60
+ }
61
+ }
62
+
53
63
/**
54
64
* Utility function to run a jscodeshift script within a unit test. This makes
55
65
* several assumptions about the environment:
@@ -69,38 +79,39 @@ exports.runInlineTest = runInlineTest;
69
79
* - Test data should be located in a directory called __testfixtures__
70
80
* alongside the transform and __tests__ directory.
71
81
*/
72
- function runTest ( dirName , transformName , options , testFilePrefix ) {
82
+ function runTest ( dirName , transformName , options , testFilePrefix , testOptions = { } ) {
73
83
if ( ! testFilePrefix ) {
74
84
testFilePrefix = transformName ;
75
85
}
76
86
87
+ const extension = extensionForParser ( testOptions . parser )
77
88
const fixtureDir = path . join ( dirName , '..' , '__testfixtures__' ) ;
78
- const inputPath = path . join ( fixtureDir , testFilePrefix + ' .input.js' ) ;
89
+ const inputPath = path . join ( fixtureDir , testFilePrefix + ` .input.${ extension } ` ) ;
79
90
const source = fs . readFileSync ( inputPath , 'utf8' ) ;
80
91
const expectedOutput = fs . readFileSync (
81
- path . join ( fixtureDir , testFilePrefix + ' .output.js' ) ,
92
+ path . join ( fixtureDir , testFilePrefix + ` .output.${ extension } ` ) ,
82
93
'utf8'
83
94
) ;
84
95
// Assumes transform is one level up from __tests__ directory
85
96
const module = require ( path . join ( dirName , '..' , transformName ) ) ;
86
97
runInlineTest ( module , options , {
87
98
path : inputPath ,
88
99
source
89
- } , expectedOutput ) ;
100
+ } , expectedOutput , testOptions ) ;
90
101
}
91
102
exports . runTest = runTest ;
92
103
93
104
/**
94
105
* Handles some boilerplate around defining a simple jest/Jasmine test for a
95
106
* jscodeshift transform.
96
107
*/
97
- function defineTest ( dirName , transformName , options , testFilePrefix ) {
108
+ function defineTest ( dirName , transformName , options , testFilePrefix , testOptions ) {
98
109
const testName = testFilePrefix
99
110
? `transforms correctly using "${ testFilePrefix } " data`
100
111
: 'transforms correctly' ;
101
112
describe ( transformName , ( ) => {
102
113
it ( testName , ( ) => {
103
- runTest ( dirName , transformName , options , testFilePrefix ) ;
114
+ runTest ( dirName , transformName , options , testFilePrefix , testOptions ) ;
104
115
} ) ;
105
116
} ) ;
106
117
}
0 commit comments