Permalink
Browse files

Release AVA support (#7)

* Release AVA support

* Release AVA support
  • Loading branch information...
1 parent 8cf4df9 commit 7697303e9e3cc236bcc053ab1da48577adc021de Kenneth Skovhus committed on GitHub Sep 24, 2016
Showing with 100 additions and 37 deletions.
  1. +10 −9 README.md
  2. +12 −5 package.json
  3. BIN screenshot.gif
  4. +71 −18 src/cli.js
  5. +2 −0 src/transformers/ava.test.js
  6. +2 −1 src/transformers/tape.test.js
  7. +3 −4 src/utils/logger.js
View
@@ -1,14 +1,16 @@
# jest-codemods
-Codemods that simplify migrating JavaScript test files to [Jest](https://facebook.github.io/jest/). Currently we support migrating from [Tape](https://github.com/substack/tape) to Jest. But we plan to support [AVA](https://github.com/avajs/ava) and other test runners in the future.
+<img src="screenshot.gif" width="440" align="right">
+
+Codemods that simplify migrating JavaScript test files from [Tape](https://github.com/substack/tape) and [AVA](https://github.com/avajs/ava) to [Jest](https://facebook.github.io/jest/). We expect to support migrating from other test frameworks in the future.
[![Build Status](https://travis-ci.org/skovhus/jest-codemods.svg?branch=master)](https://travis-ci.org/skovhus/jest-codemods)
[![version][version-badge]][package]
[![Tweet][twitter-badge]][twitter]
Codemods are small programs that help you automate changes to your codebase. Think of them as search and replace on steroids. They are executed by the [Facebook jscodeshift](https://github.com/facebook/jscodeshift) tool.
-This tool is made for trying out Jest on your existing test files. We strive to make the migration as smooth as possible, but some manual intervention and tweaks are to be expected.
+This tool is made for trying out Jest on your existing test files. We strive to make the migration as smooth as possible, but some manual intervention and tweaks to your tests are to be expected.
## Install
@@ -22,29 +24,27 @@ This installs the runner as `jest-codemods`.
## Usage (CLI)
-Currently we support migrating from [Tape](https://github.com/substack/tape) to Jest.
-
```
$ jest-codemods --help
- Codemod that simplify migrating to Jest.
+ Codemods for migrating test files to Jest.
Usage
$ jest-codemods <path> [options]
path Files or directory to transform. Can be a glob like src/**.test.js
- Only files with Tape will be converted.
+ Only files using Tape or AVA will be converted.
Options
--force, -f Bypass Git safety checks and forcibly run codemods
--dry, -d Dry run (no changes are made to files)
--parser The parser to use for parsing your source files (babel | babylon | flow) [babel]
```
-To transform all test files in a directory run `jest-codemods mySrcFolder` in your terminal.
+To transform all test files in a directory run `jest-codemods test-folder` in your terminal.
-Only files requiring or importing Tape will be transformed. Notice the console output for errors, manual intervention and tweaks are to be expected.
+Only files requiring or importing AVA or Tape will be transformed. Notice the console output for errors, manual intervention and tweaks are to be expected.
## Usage (jscodeshift)
@@ -54,7 +54,8 @@ To make the process as simple as possible, we recommend the `jest-codemods` CLI
```
$ npm install -g jscodeshift
$ npm install jest-codemods
-$ jscodeshift -t node_modules/jest-codemods/dist/transformers/tape.js mySrcFolder
+$ jscodeshift -t node_modules/jest-codemods/dist/transformers/tape.js test-folder
+$ jscodeshift -t node_modules/jest-codemods/dist/transformers/ava.js test-folder
```
View
@@ -1,7 +1,7 @@
{
"name": "jest-codemods",
- "version": "0.1.0",
- "description": "Codemods for switching test framework to Jest",
+ "version": "0.2.0",
+ "description": "Codemods for migrating test files to Jest",
"license": "MIT",
"repository": "skovhus/jest-codemods",
"author": {
@@ -30,6 +30,7 @@
"keywords": [
"codemod",
"jest",
+ "ava",
"tape",
"migrate",
"migrations",
@@ -39,23 +40,29 @@
"codemods"
],
"dependencies": {
+ "chalk": "~1.1.3",
"execa": "~0.4.0",
- "is-git-clean": "~1.0.0",
+ "inquirer": "~1.1.3",
+ "is-git-clean": "~1.1.0",
"jscodeshift": "~0.3.28",
"meow": "~3.7.0",
"update-notifier": "~1.0.2"
},
"devDependencies": {
"babel-cli": "^6.14.0",
"babel-jest": "^15.0.0",
- "babel-preset-es2015-node4": "2.1.0",
+ "babel-preset-es2015-node4": "^2.1.0",
"eslint": "^3.5.0",
"eslint-config-airbnb-base": "^7.1.0",
"eslint-plugin-import": "^1.15.0",
"jest": "^15.1.1"
},
"jest": {
- "coveragePathIgnorePatterns": ["/node_modules/", "/dist/"],
+ "collectCoverageFrom": "src/**/*.js",
+ "coveragePathIgnorePatterns": [
+ "/node_modules/",
+ "/dist/"
+ ],
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"testEnvironment": "node"
}
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -2,20 +2,21 @@
import path from 'path';
import execa from 'execa';
+import inquirer from 'inquirer';
import isGitClean from 'is-git-clean';
import meow from 'meow';
import updateNotifier from 'update-notifier';
-const PATH_TRANSFORMER = path.join(__dirname, 'transformers', 'tape.js');
-
function checkGitStatus(force) {
let clean = false;
let errorMessage = 'Unable to determine if git directory is clean';
try {
- clean = isGitClean.sync();
+ clean = isGitClean.sync(process.cwd(), { files: ['!package.json'] });
errorMessage = 'Git directory is not clean';
} catch (err) {
- // Ignoring error
+ if (err && err.stderr && err.stderr.indexOf('Not a git repository') >= 0) {
+ clean = true;
+ }
}
const ENSURE_BACKUP_MESSAGE = 'Ensure you have a backup of your tests or commit the latest changes before continuing.';
@@ -34,13 +35,10 @@ function checkGitStatus(force) {
}
}
-function executeTransformation(files, flags) {
- const spawnOptions = {
- stdio: 'inherit',
- stripEof: false,
- };
+function executeTransformation(files, flags, transformer) {
+ const transformerPath = path.join(__dirname, 'transformers', `${transformer}.js`);
- const args = ['-t', PATH_TRANSFORMER].concat(files);
+ const args = ['-t', transformerPath].concat(files);
if (flags.dry) {
args.push('--dry');
}
@@ -50,22 +48,32 @@ function executeTransformation(files, flags) {
console.log(`Executing command: jscodeshift ${args.join(' ')}`);
- const result = execa.sync('jscodeshift', args, spawnOptions);
+ const result = execa.sync('jscodeshift', args, {
+ stdio: 'inherit',
+ stripEof: false,
+ });
+
if (result.error) {
throw result.error;
}
}
+function executeTransformations(files, flags, transformers) {
+ transformers.forEach(t => {
+ executeTransformation(files, flags, t);
+ });
+}
+
const cli = meow(
{
- description: 'Codemod that simplify migrating to Jest.',
+ description: 'Codemods for migrating test files to Jest.',
help: `
Usage
$ jest-codemods <path> [options]
path Files or directory to transform. Can be a glob like src/**.test.js
- Only files with Tape will be converted.
+ Only files using Tape or AVA will be converted.
Options
--force, -f Bypass Git safety checks and forcibly run codemods
@@ -86,12 +94,57 @@ const cli = meow(
updateNotifier({ pkg: cli.pkg }).notify();
-const files = cli.input;
-if (files.length === 0) {
- cli.showHelp();
-} else {
+if (cli.input.length) {
+ // Apply all transformers if input is given using CLI.
if (!cli.flags.dry) {
checkGitStatus(cli.flags.force);
}
- executeTransformation(files, cli.flags);
+ executeTransformations(cli.input, cli.flags, ['tape', 'ava']);
+} else {
+ // Else show the fancy inquirer prompt.
+ inquirer.prompt([{
+ type: 'list',
+ name: 'transformer',
+ message: 'Which test library would you like to migrate from?',
+ choices: [{
+ name: 'Tape',
+ value: 'tape',
+ }, {
+ name: 'AVA',
+ value: 'ava',
+ }, {
+ name: 'All of the above!',
+ value: 'all',
+ }, {
+ name: 'Other',
+ value: 'other',
+ }],
+ }, {
+ type: 'input',
+ name: 'files',
+ message: 'On which files or directory should the codemods be applied?',
+ default: 'test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js',
+ filter: files => files.trim().split(/\s+/).filter(v => v),
+ }]).then(answers => {
+ const { files, transformer } = answers;
+
+ if (transformer === 'other') {
+ console.log('\nCurrently jest-codemods only have support for AVA and Tape.');
+ console.log('Feel free to create an issue on https://github.com/skovhus/jest-codemods or help contribute!\n');
+ return;
+ }
+
+ if (!files.length) {
+ return;
+ }
+
+ if (!cli.flags.dry) {
+ checkGitStatus(cli.flags.force);
+ }
+
+ const transformers = transformer === 'all' ? ['tape', 'ava'] : [transformer];
+ transformers.forEach(t => {
+ executeTransformation(files, cli.flags, t);
+ });
+ });
}
@@ -1,7 +1,9 @@
/* eslint-env jest */
+import chalk from 'chalk';
import { wrapPlugin } from '../utils/test-helpers';
import plugin from './ava';
+chalk.enabled = false;
const wrappedPlugin = wrapPlugin(plugin);
let consoleWarnings = [];
@@ -1,8 +1,9 @@
/* eslint-env jest */
-
+import chalk from 'chalk';
import { wrapPlugin } from '../utils/test-helpers';
import plugin from './tape';
+chalk.enabled = false;
const wrappedPlugin = wrapPlugin(plugin);
let consoleWarnings = [];
View
@@ -1,7 +1,6 @@
-/**
- * Logs the given warning.
- */
+import chalk from 'chalk';
+
export default function logWarning(fileInfo, msg, node) {
const lineInfo = node ? ` line ${node.value.loc.start.line}` : '';
- console.warn(`jest-codemods warning: (${fileInfo.path}${lineInfo}) ${msg}`);
+ console.warn(chalk.red(`jest-codemods warning: (${fileInfo.path}${lineInfo}) ${msg}`));
}

0 comments on commit 7697303

Please sign in to comment.