-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathWorker.spec.js
203 lines (181 loc) · 6.18 KB
/
Worker.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* eslint-disable @typescript-eslint/no-var-requires */
'use strict';
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const temp = require('temp');
function renameFileTo(oldPath, newFilename) {
const projectPath = path.dirname(oldPath);
const newPath = path.join(projectPath, newFilename);
mkdirp.sync(path.dirname(newPath));
fs.renameSync(oldPath, newPath);
return newPath;
}
function createTempFileWith(content, filename, extension) {
const info = temp.openSync({ suffix: extension });
let filePath = info.path;
fs.writeSync(info.fd, content);
fs.closeSync(info.fd);
if (filename) {
filePath = renameFileTo(filePath, filename);
}
return filePath;
}
// Test transform files need a js extension to work with @babel/register
// .ts or .tsx work as well
function createTransformWith(content, ext = '.js') {
return createTempFileWith(
'module.exports = function(fileInfo, api, options) { ' + content + ' }',
undefined,
ext,
);
}
function getFileContent(filePath) {
return fs.readFileSync(filePath).toString();
}
describe('Worker API', () => {
it('transforms files', done => {
const worker = require('./Worker');
const transformPath = createTransformWith(
'return fileInfo.source + " changed";',
);
const sourcePath = createTempFileWith('foo');
const emitter = worker([transformPath]);
emitter.send({ files: [sourcePath] });
emitter.once('message', data => {
expect(data.status).toBe('ok');
expect(data.msg).toBe(sourcePath);
expect(getFileContent(sourcePath)).toBe('foo changed');
done();
});
});
it('transforms files with tranformId as extension', done => {
const worker = require('./Worker');
const configPath = createTempFileWith(
`
const transfomer = (fileInfo) => fileInfo.source + " changed";
module.exports = { transforms: { "1.0.0": transfomer } };
`,
'codeshift1.config.js',
'.js',
);
const sourcePath = createTempFileWith('foo');
const emitter = worker([configPath + '@1.0.0']);
emitter.send({ files: [sourcePath] });
emitter.once('message', data => {
expect(data.status).toBe('ok');
expect(data.msg).toBe(sourcePath);
expect(getFileContent(sourcePath)).toBe('foo changed');
done();
});
});
it('transforms files with presetId as extension', done => {
const worker = require('./Worker');
const configPath = createTempFileWith(
`
const transfomer = (fileInfo) => fileInfo.source + " changed";
module.exports = { presets: { "my-preset": transfomer } };
`,
'codeshift2.config.js',
'.js',
);
const sourcePath = createTempFileWith('foo');
const emitter = worker([configPath + '#my-preset']);
emitter.send({ files: [sourcePath] });
emitter.once('message', data => {
expect(data.status).toBe('ok');
expect(data.msg).toBe(sourcePath);
expect(getFileContent(sourcePath)).toBe('foo changed');
done();
});
});
it('passes j as argument', done => {
const worker = require('./Worker');
const transformPath = createTempFileWith(
`module.exports = function (file, api) {
return api.j(file.source).toSource() + ' changed';
}`,
);
const sourcePath = createTempFileWith('const x = 10;');
const emitter = worker([transformPath]);
emitter.send({ files: [sourcePath] });
emitter.once('message', data => {
expect(data.status).toBe('ok');
expect(getFileContent(sourcePath)).toBe('const x = 10;' + ' changed');
done();
});
});
describe('custom parser', () => {
function getTransformForParser(parser) {
return createTempFileWith(
`function transform(fileInfo, api) {
api.jscodeshift(fileInfo.source);
return "changed";
}
${parser ? `transform.parser = '${parser}';` : ''}
module.exports = transform;
`,
);
}
function getSourceFile() {
// This code cannot be parsed by Babel v5
return createTempFileWith('const x = (a: Object, b: string): void => {}');
}
it('errors if new flow type code is parsed with babel v5', done => {
const worker = require('./Worker');
const transformPath = createTransformWith(
'api.jscodeshift(fileInfo.source); return "changed";',
);
const sourcePath = getSourceFile();
const emitter = worker([transformPath]);
emitter.send({ files: [sourcePath] });
emitter.once('message', data => {
expect(data.status).toBe('error');
expect(data.msg).toMatch('SyntaxError');
done();
});
});
['flow', 'babylon'].forEach(parser => {
it(`uses ${parser} if configured as such`, done => {
const worker = require('./Worker');
const transformPath = getTransformForParser(parser);
const sourcePath = getSourceFile();
const emitter = worker([transformPath]);
emitter.send({ files: [sourcePath] });
emitter.once('message', data => {
expect(data.status).toBe('ok');
expect(getFileContent(sourcePath)).toBe('changed');
done();
});
});
});
['babylon', 'flow', 'tsx'].forEach(parser => {
it(`can parse JSX with ${parser}`, done => {
const worker = require('./Worker');
const transformPath = getTransformForParser(parser);
const sourcePath = createTempFileWith(
'var component = <div>{foobar}</div>;',
);
const emitter = worker([transformPath]);
emitter.send({ files: [sourcePath] });
emitter.once('message', data => {
expect(data.status).toBe('ok');
expect(getFileContent(sourcePath)).toBe('changed');
done();
});
});
});
it('can parse enums with flow', done => {
const worker = require('./Worker');
const transformPath = getTransformForParser('flow');
const sourcePath = createTempFileWith('enum E {A, B}');
const emitter = worker([transformPath]);
emitter.send({ files: [sourcePath] });
emitter.once('message', data => {
expect(data.status).toBe('ok');
expect(getFileContent(sourcePath)).toBe('changed');
done();
});
});
});
});