Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including chai-jscodeshift with all npm packages installed. Try it out:

require("jscodeshift/package.json"); // jscodeshift is a peer dependency. require("chai/package.json"); // chai is a peer dependency. var chaiJscodeshift = require("chai-jscodeshift")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

chai-jscodeshift v15.0.0

Chai assertion utilities for writing JSCodeShift Codemods.

chai-jscodeshift

chai-jscodeshift provides a custom assertion for jscodeshift to check whether a given input fixture matches an output fixture after being transformed by the transformer under test.

Setup

import chai from 'chai';
import chaiJSCodeShift from 'chai-jscodeshift';
chai.use(chaiJSCodeShift());

Usage

import myTransform from '../src/my-transform';

describe('myTransform', () => {
  it('transforms properly', () => {
    expect(myTransform).to.transform('my-fixture');

    // or

    assert.transforms(myTransform, 'my-fixture');
  });
});

Customization

By default, this plugin will look for fixtures in the fixtures in the working directory. You will probably want to customize this by setting the directory in which your fixtures appear:

import path from 'path';

chai.use(chaiJSCodeShift({
  fixtureDirectory: path.join(__dirname, 'fixtures')
}));

When you declare that you want a transformer to transform a particular fixture, this plugin will look for a file named <fixtureName>.input.js, relative to the fixtureDirectory declared above. It will read this file, transform the contents using your transformer, and compare it against a file named <fixtureName>.output.js relative to the fixture directory. You can customize either of these paths by passing a custom inputFixturePath or outputFixturePath in your options (both of which take two arguments, the fixture name and the root of the fixture directory):

chai.use(chaiJSCodeShift({
  fixtureDirectory: path.join(__dirname, 'fixtures'),
  inputFixturePath(fixtureName, fixtureDirectory) {
    return path.join(fixtureDirectory, 'input', `${fixtureName}.js`);
  },
  inputFixturePath(fixtureName, fixtureDirectory) {
    return path.join(fixtureDirectory, 'output', `${fixtureName}.js`);
  },
}));

You can also set custom options that will be passed as the third argument to your transformer on every call using the transformOptions configuration option:

chai.use(chaiJSCodeShift({
  transformOptions: {
    printOptions: {space: 'single'},
  },
}));
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free