Skip to content

Commit 656300a

Browse files
author
Robert Jackson
committed
Move test support to dedicated file.
1 parent 4e1f60d commit 656300a

2 files changed

Lines changed: 68 additions & 61 deletions

File tree

src/index.js

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,7 @@
11
'use strict';
22

3-
/* global it, describe */
4-
5-
const { runInlineTest } = require('jscodeshift/dist/testUtils');
6-
const fs = require('fs-extra');
7-
const path = require('path');
8-
9-
function transformDetails(options) {
10-
let root = process.cwd() + `/transforms/${options.name}/`;
11-
12-
return {
13-
name: options.name,
14-
root,
15-
transformPath: root + 'index.js',
16-
fixtureDir: root + '__testfixtures__/',
17-
};
18-
}
19-
20-
function jscodeshiftTest(options) {
21-
let details = transformDetails(options);
22-
23-
let transform = require(details.transformPath);
24-
25-
describe(details.name, function() {
26-
fs.readdirSync(details.fixtureDir)
27-
.filter(filename => /\.input$/.test(path.basename(filename, path.extname(filename))))
28-
.forEach(filename => {
29-
let extension = path.extname(filename);
30-
let testName = filename.replace(`.input${extension}`, '');
31-
let inputPath = path.join(details.fixtureDir, `${testName}.input${extension}`);
32-
let outputPath = path.join(details.fixtureDir, `${testName}.output${extension}`);
33-
34-
describe(testName, function() {
35-
it('transforms correctly', function() {
36-
runInlineTest(
37-
transform,
38-
{},
39-
{ source: fs.readFileSync(inputPath, 'utf8') },
40-
fs.readFileSync(outputPath, 'utf8')
41-
);
42-
});
43-
44-
it('is idempotent', function() {
45-
runInlineTest(
46-
transform,
47-
{},
48-
{ source: fs.readFileSync(outputPath, 'utf8') },
49-
fs.readFileSync(outputPath, 'utf8')
50-
);
51-
});
52-
});
53-
});
54-
});
55-
}
56-
57-
function runTransformTest(options) {
58-
switch (options.type) {
59-
case 'jscodeshift':
60-
return jscodeshiftTest(options);
61-
}
62-
}
3+
const TestSupport = require('./test-support');
634

645
module.exports = {
65-
runTransformTest,
6+
runTransformTest: TestSupport.runTransformTest,
667
};

src/test-support.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
/* global it, describe */
4+
5+
const { runInlineTest } = require('jscodeshift/dist/testUtils');
6+
const fs = require('fs-extra');
7+
const path = require('path');
8+
9+
function transformDetails(options) {
10+
let root = process.cwd() + `/transforms/${options.name}/`;
11+
12+
return {
13+
name: options.name,
14+
root,
15+
transformPath: root + 'index.js',
16+
fixtureDir: root + '__testfixtures__/',
17+
};
18+
}
19+
20+
function jscodeshiftTest(options) {
21+
let details = transformDetails(options);
22+
23+
let transform = require(details.transformPath);
24+
25+
describe(details.name, function() {
26+
fs.readdirSync(details.fixtureDir)
27+
.filter(filename => /\.input$/.test(path.basename(filename, path.extname(filename))))
28+
.forEach(filename => {
29+
let extension = path.extname(filename);
30+
let testName = filename.replace(`.input${extension}`, '');
31+
let inputPath = path.join(details.fixtureDir, `${testName}.input${extension}`);
32+
let outputPath = path.join(details.fixtureDir, `${testName}.output${extension}`);
33+
34+
describe(testName, function() {
35+
it('transforms correctly', function() {
36+
runInlineTest(
37+
transform,
38+
{},
39+
{ source: fs.readFileSync(inputPath, 'utf8') },
40+
fs.readFileSync(outputPath, 'utf8')
41+
);
42+
});
43+
44+
it('is idempotent', function() {
45+
runInlineTest(
46+
transform,
47+
{},
48+
{ source: fs.readFileSync(outputPath, 'utf8') },
49+
fs.readFileSync(outputPath, 'utf8')
50+
);
51+
});
52+
});
53+
});
54+
});
55+
}
56+
57+
function runTransformTest(options) {
58+
switch (options.type) {
59+
case 'jscodeshift':
60+
return jscodeshiftTest(options);
61+
}
62+
}
63+
64+
module.exports = {
65+
runTransformTest,
66+
};

0 commit comments

Comments
 (0)