Skip to content

Commit e192864

Browse files
Matthew BealeSantosh Sutar
authored andcommitted
Load an JSON file of jscodeshift options in test
1 parent fd6ce79 commit e192864

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

src/test-support.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ function jscodeshiftTest(options) {
3636
let testName = filename.replace(`.input${extension}`, '');
3737
let inputPath = path.join(details.fixtureDir, `${testName}.input${extension}`);
3838
let outputPath = path.join(details.fixtureDir, `${testName}.output${extension}`);
39+
let optionsPath = path.join(details.fixtureDir, `${testName}.options.json`);
3940

4041
describe(testName, function() {
4142
it('transforms correctly', function() {
4243
runInlineTest(
4344
transform,
44-
{},
45+
fs.pathExistsSync(optionsPath) ? JSON.parse(fs.readFileSync(optionsPath)) : {},
4546
{ path: inputPath, source: fs.readFileSync(inputPath, 'utf8') },
4647
fs.readFileSync(outputPath, 'utf8')
4748
);
@@ -50,7 +51,7 @@ function jscodeshiftTest(options) {
5051
it('is idempotent', function() {
5152
runInlineTest(
5253
transform,
53-
{},
54+
fs.pathExistsSync(optionsPath) ? JSON.parse(fs.readFileSync(optionsPath)) : {},
5455
{ path: inputPath, source: fs.readFileSync(outputPath, 'utf8') },
5556
fs.readFileSync(outputPath, 'utf8')
5657
);

tests/cli-test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,47 @@ QUnit.module('codemod-cli', function(hooks) {
186186
})
187187
);
188188

189+
QUnit.test(
190+
'transform should receive options from ${name}.options.json',
191+
wrap(function*(assert) {
192+
const realCodemodProjectPath = fs.realpathSync(codemodProject.path());
193+
const expectedReplacement = 'AAAAHHHHHH';
194+
195+
yield execa(EXECUTABLE_PATH, ['generate', 'codemod', 'main']);
196+
197+
codemodProject.write({
198+
transforms: {
199+
main: {
200+
'index.js': `
201+
const { getParser } = require('codemod-cli').jscodeshift;
202+
203+
module.exports = function transformer(file, api, options) {
204+
const j = getParser(api);
205+
206+
return j(file.source)
207+
.find(j.Literal)
208+
.forEach(path => {
209+
path.replace(
210+
j.stringLiteral(options.replaceAll)
211+
);
212+
})
213+
.toSource();
214+
}
215+
`,
216+
__testfixtures__: {
217+
'basic.input.js': 'var foo = "foo";',
218+
'basic.output.js': `var foo = "${expectedReplacement}";`,
219+
'basic.options.json': `{ "replaceAll": "${expectedReplacement}" }`,
220+
},
221+
},
222+
},
223+
});
224+
225+
let result = yield execa(EXECUTABLE_PATH, ['test']);
226+
assert.equal(result.code, 0, 'exited with zero');
227+
})
228+
);
229+
189230
QUnit.test(
190231
'transform should receive a file path in tests',
191232
wrap(function*(assert) {

0 commit comments

Comments
 (0)