Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple utility for unit testing jscodeshift transform #104

Merged
merged 1 commit into from
Apr 10, 2016

Conversation

Daniel15
Copy link
Member

@Daniel15 Daniel15 commented Apr 5, 2016

Adds a simple utility to allow unit testing of jscodeshift transforms in a standard way, without having to write a bunch of boilerplate code. This is loosely based off the technique used in react-codemod, with some changes. The main change is that where possible it doesn't depend on any global environment/configuration:

  • It's in an actual module rather than a global function called "test". It took me so long to work out where the test function was coming from in the js-codemod repo 😛
  • Assumes the transform is in the parent directory of the test (eg. transform is at foo/bar.js, test is at foo/__tests__/bar-test.js)
  • Does not use a .js suffix for the test fixtures (input and expected output). This is mainly so they don't accidentally get picked up as unit tests by Jest, or as code that should be bundled with the app.

Also included an example of how to use it, using a simple transform (the default example from AST Explorer).

import fs from 'fs';
import path from 'path';

import jscodeshift from './core';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a lazy require inside of runTest. Jest resets the module registry after each test and we had issues previously where two copies of ast-types was present.

cc @pieterv

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh, good point. Will fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@fkling
Copy link
Contributor

fkling commented Apr 5, 2016

Awesome! Would you mind mentioning this in the readme somewhere?

@Daniel15
Copy link
Member Author

Daniel15 commented Apr 9, 2016

Sure :) I was thinking about it a bit, and I think I'll move the test fixtures to a separate directory to keep them separate from the unit test files themselves.

@Daniel15
Copy link
Member Author

Daniel15 commented Apr 9, 2016

Updated to add a section to the readme.

@fkling
Copy link
Contributor

fkling commented Apr 10, 2016

Thanks, this is great!

@Daniel15
Copy link
Member Author

@fkling - It would be useful to mention this in the release notes for v0.3.20, mainly so people know that this is only included from that version onwards.

@fkling
Copy link
Contributor

fkling commented Apr 29, 2016

Whoops, yes of course! Sorry for forgetting to add this!

@Daniel15
Copy link
Member Author

No worries 👍

@Daniel15 Daniel15 deleted the unit-test-stuff branch April 29, 2016 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants