Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jfmengels/jscodeshift-ava-tester
jscodeshift wrapper to write smaller and better tests for your codemods using AVA
https://github.com/jfmengels/jscodeshift-ava-tester
Last synced: 2 months ago
JSON representation
jscodeshift wrapper to write smaller and better tests for your codemods using AVA
- Host: GitHub
- URL: https://github.com/jfmengels/jscodeshift-ava-tester
- Owner: jfmengels
- License: mit
- Created: 2016-07-27T16:40:09.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-14T23:40:32.000Z (almost 8 years ago)
- Last Synced: 2024-10-06T13:15:42.344Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 13
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
- awesome-codemods - jscodeshift-ava-tester - Codeshift wrapper to write smaller and better tests for your codemods using AVA. (Libraries / AVA)
- awesome-jscodeshift - jscodeshift-ava-tester - Test codemods using [AVA](https://github.com/avajs/ava) test runner. (Table of Contents / Tools)
- awesome-ava - jscodeshift-ava-tester - Test [jscodeshift](https://github.com/facebook/jscodeshift) codemods with AVA. (Packages)
README
# jscodeshift-ava-tester [![Build Status](https://travis-ci.org/jfmengels/jscodeshift-ava-tester.svg?branch=master)](https://travis-ci.org/jfmengels/jscodeshift-ava-tester)
> [`jscodeshift`] wrapper to write smaller and better tests for your codemods using [`AVA`]
## Install
```
$ npm install --save jscodeshift-ava-tester
```## Usage
Let's say you want to write tests for a [`jscodeshift`] codemod in `/lib/codemod-to-test.js`. You can create a test file `/test/codemod-to-test.js` and write plenty of small tests to test your codemod as thoroughly as you want.
```js
import test from 'ava';
import jscodeshift from 'jscodeshift';
import testCodemod from 'jscodeshift-ava-tester';
import codemod from '../lib/codemod-to-test';const {testChanged, testUnchanged} = testCodemod(jscodeshift, test, codemod);
// Let's assume `codemod` modifies `var` declarations to either `let` or `const`
// Test things that should be modified
testChanged('var foo = 2;', 'const foo = 2;');
testChanged('var foo;', 'let foo;');
testChanged('var foo = 2; foo = 3;', 'let foo = 2; foo = 3;');
testChanged('var foo = 2; foo++;', 'let foo = 2; foo++;');
testChanged('var foo = 2; foo++;', 'let foo = 2; foo++;');
// ...// Test things that should stay as is
testUnchanged('const foo = 2;');
testUnchanged('let foo = 2;');
testUnchanged('let foo;');
// ...
```You can then run [`AVA`] as you would for other [`AVA`] tests (using `ava` or maybe even `ava --watch`).
## API
### testCodemod(jscodeshift, test, codemod)
Return methods that run the codemod on the given input and assert whether modifications are made, using the [`jscodeshift`] and [`AVA`] modules that you provide.
#### jscodeshift
The [`jscodeshift`] module that you use in your project.
#### test
The [`AVA`] module that you use in your project.
#### codemod
The codemod to test.
#### Returns
An object containing two methods: `testChanged` and `testUnchanged`.
### testChanged([title], input, expectedOutput)
Create a test that ensures that the codemod modifies `input` to `expectedOutput`.
#### title
Optional title for the test. If omitted, `input` will be used as the title.
#### input
Mock content of an input file.
#### expectedOutput
Expected result of the codemod when given `input`.
### testUnchanged([title], input)
Create a test that ensures that the codemod does not modify `input`.
#### title
Optional title for the test. If omitted, `input` will be used as the title.
#### input
Mock content of an input file.
## Thanks
Special thanks to [@jamestalmage](https://github.com/jamestalmage) who created the original script.
## License
MIT © [Jeroen Engels](https://github.com/jfmengels)
[`jscodeshift`]: https://github.com/facebook/jscodeshift
[`AVA`]: https://github.com/avajs/ava