Skip to content

Commit 73d6440

Browse files
author
Robert Jackson
committed
Auto-generate basic fixture when generating codemod.
1 parent 7bafbbf commit 73d6440

4 files changed

Lines changed: 22 additions & 14 deletions

File tree

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ codemod-cli generate codemod <name of codemod>
3232
```
3333

3434
This will setup a new jscodeshift codemod within your project at `transforms/<name of codemod>/index.js`
35-
along with a test harness, README, and fixture directory.
35+
along with a test harness, README, fixture directory, and an initial set of input/output fixtures.
3636

37-
Now that we have a codemod, we can begin generating fixtures for it. We use fixtures as a convienient
38-
way to represent the expected output for a given input. To generate a new fixture, run the following:
37+
Once you have tweaked your codemod and its fixtures to your liking, it is time to run your tests:
38+
39+
```
40+
codemod-cli test
41+
```
42+
43+
As you develop your codemod you may need additional fixtures (e.g. to test various combinations of
44+
inputs). To generate a new fixture, run the following:
3945

4046
```
4147
codemod-cli generate fixture <name of codemod> <name of fixture>
@@ -44,11 +50,6 @@ codemod-cli generate fixture <name of codemod> <name of fixture>
4450
This sets up two new files in `transforms/<name of codemod>/__testfixtures__/` using the fixture name
4551
you provided. These fixtures are used by the testing harness to verify that your codemod is working properly.
4652

47-
Once you have tweaked your codemod and its fixtures to your liking, it is time to run your tests:
48-
49-
```
50-
codemod-cli test
51-
```
5253

5354
Contributing
5455
------------------------------------------------------------------------------

commands/local/generate/codemod.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ module.exports.builder = function builder(yargs) {
88
};
99

1010
module.exports.handler = function handler(options) {
11-
let { codemodName } = options;
12-
let codemodDir = `${process.cwd()}/transforms/${codemodName}`;
13-
1411
const fs = require('fs-extra');
1512
const { stripIndent } = require('common-tags');
13+
const generateFixture = require('./fixture').handler;
14+
15+
let { codemodName } = options;
16+
let codemodDir = `${process.cwd()}/transforms/${codemodName}`;
1617

1718
fs.outputFileSync(
1819
`${codemodDir}/index.js`,
@@ -48,4 +49,6 @@ module.exports.handler = function handler(options) {
4849
'utf8'
4950
);
5051
fs.outputFileSync(`${codemodDir}/README.md`, `# ${codemodName}\n`, 'utf8');
52+
53+
generateFixture({ codemodName, fixtureName: 'basic' });
5154
};

commands/local/generate/fixture.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ module.exports.builder = function builder(yargs) {
1212
};
1313

1414
module.exports.handler = function handler(options) {
15-
let { codemodName, fixtureName } = options;
16-
let codemodDir = `${process.cwd()}/transforms/${codemodName}`;
17-
1815
const fs = require('fs-extra');
1916

17+
let { codemodName, fixtureName } = options;
18+
let codemodDir = `${process.cwd()}/transforms/${codemodName}`;
2019
let fixturePath = `${codemodDir}/__testfixtures__/${fixtureName}`;
2120

2221
fs.outputFileSync(`${fixturePath}.input.js`, '');

tests/cli-test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ QUnit.module('codemod-cli', function(hooks) {
7777
'.gitkeep',
7878
'main/',
7979
'main/README.md',
80+
'main/__testfixtures__/',
81+
'main/__testfixtures__/basic.input.js',
82+
'main/__testfixtures__/basic.output.js',
8083
'main/index.js',
8184
'main/test.js',
8285
]);
@@ -102,6 +105,8 @@ QUnit.module('codemod-cli', function(hooks) {
102105
'main/',
103106
'main/README.md',
104107
'main/__testfixtures__/',
108+
'main/__testfixtures__/basic.input.js',
109+
'main/__testfixtures__/basic.output.js',
105110
'main/__testfixtures__/this-dot-owner.input.js',
106111
'main/__testfixtures__/this-dot-owner.output.js',
107112
'main/index.js',

0 commit comments

Comments
 (0)