Skip to content

Instantly share code, notes, and snippets.

@ianwremmel
Last active June 29, 2017 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianwremmel/84cc90ac6516c141d2d2255a174e3747 to your computer and use it in GitHub Desktop.
Save ianwremmel/84cc90ac6516c141d2d2255a174e3747 to your computer and use it in GitHub Desktop.
Remove chai-as-promised

Apply this transform using jscodeshift to remove most chai-as-promised assertions.

Note: this codemod does not cover all of the assert.eventually cases, but they should be easy to add.

There's really no alternative to defining the function assert.isRejected. The following should approximate the functionallity from chai-as-promised.

  var chai = require('chai');

  chai.assert.isRejected = function isRejected(promise, expected) {
    return new Assertion(promise).to.be.rejectedWith(expected);
  };
  
  Assertion.addMethod('isRejected', function expectedRejection(promise, expected) {
    var promise = this.then ? this : this._obj;
    return promise
      .then(function(actual) {
        this.assert(
          false,
          'expected #{this} to be rejected but it was fulfilled with #{act}',
          null,
          expected,
          actual
        );
        return actual;
      }.bind(this),
      function(reason) {
        if (expected) {
          if (expected instanceof RegExp || typeof expected === 'string') {
            this.match(reason, expected);
          }

          if (expected instanceof Error) {
            this.instanceOf(reason, expected);
          }


        }
      }.bind(this));
  });
/* eslint-disable new-cap */
/* eslint-disable require-jsdoc */
function removeBecomes(jscodeshift, root) {
root
.find(jscodeshift.CallExpression, {
callee: {
object: {name: `assert`},
property: {name: `becomes`}
}
})
.replaceWith((p) => jscodeshift.template.expression`${p.node.arguments[0]}
.then((result) => assert.deepEqual(result, ${p.node.arguments[1]}))`);
}
function removeEventually(jscodeshift, root) {
root
.find(jscodeshift.CallExpression, {
callee: {
object: {
object: {name: `assert`},
property: {name: `eventually`}
},
property: {name: `isTrue`}
}
})
.replaceWith((p) => jscodeshift.template.expression`${p.node.arguments[0]}
.then((result) => assert.isTrue(result))`);
root
.find(jscodeshift.CallExpression, {
callee: {
object: {
object: {name: `assert`},
property: {name: `eventually`}
},
property: {name: `deepEqual`}
}
})
.replaceWith((p) => jscodeshift.template.expression`${p.node.arguments[0]}
.then((result) => assert.deepEqual(result, ${p.node.arguments[1]}))`);
root
.find(jscodeshift.CallExpression, {
callee: {
object: {
object: {name: `assert`},
property: {name: `eventually`}
},
property: {name: `equal`}
}
})
.replaceWith((p) => jscodeshift.template.expression`${p.node.arguments[0]}
.then((result) => assert.equal(result, ${p.node.arguments[1]}))`);
root
.find(jscodeshift.CallExpression, {
callee: {
object: {
object: {name: `assert`},
property: {name: `eventually`}
},
property: {name: `match`}
}
})
.replaceWith((p) => jscodeshift.template.expression`${p.node.arguments[0]}
.then((result) => assert.match(result, ${p.node.arguments[1]}))`);
}
function removeIsFullfilled(jscodeshift, root) {
root
.find(jscodeshift.CallExpression, {
callee: {
object: {name: `assert`},
property: {name: `isFulfilled`}
}
})
.replaceWith((p) => p.node.arguments[0]);
}
module.exports = function transform({source}, {jscodeshift}) {
const root = jscodeshift(source);
removeEventually(jscodeshift, root);
removeIsFullfilled(jscodeshift, root);
removeBecomes(jscodeshift, root);
return root.toSource();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment