diff --git a/lib/transformer.js b/lib/transformer.js index e6fa130..eab2734 100644 --- a/lib/transformer.js +++ b/lib/transformer.js @@ -254,16 +254,34 @@ module.exports = ({ path, source }, { jscodeshift: j }) => { // @example // a.b.should.be.ok() => assert(a.b); - // a.b.should.not.be.ok() => assert(!a.b); + // a.b.should.be.empty() => assert(a.b.length === 0); root.find(j.CallExpression, { callee: { object: { object: { property: { name: 'should' } }, property: { name: 'be' }, }, - property: { name: 'ok' }, + property(node) { + return /^ok|true|false|empty$/.test(node.name); + }, }, }).replaceWith(path => { + if (path.value.callee.property.name === 'false') { + return j.callExpression( + j.identifier('assert'), [ + j.unaryExpression('!', path.value.callee.object.object.object), + ]); + } + if (path.value.callee.property.name === 'empty') { + return j.callExpression( + j.identifier('assert'), [ + j.binaryExpression('===', + j.memberExpression(path.value.callee.object.object.object, j.identifier('length')), + j.literal(0) + ), + ] + ); + } return j.callExpression( j.identifier('assert'), [ path.value.callee.object.object.object ] ); @@ -329,6 +347,17 @@ module.exports = ({ path, source }, { jscodeshift: j }) => { ] )); } + if (path.value.expression.property.name === 'empty') { + return j.expressionStatement( + j.callExpression( + j.identifier('assert'), [ + j.binaryExpression('===', + j.memberExpression(path.value.expression.object.object.object, j.identifier('length')), + j.literal(0) + ), + ] + )); + } return path; }); root.find(j.ExpressionStatement, { diff --git a/test/fixtures/input/test/a.js b/test/fixtures/input/test/a.js index ba55436..c6f7eb4 100644 --- a/test/fixtures/input/test/a.js +++ b/test/fixtures/input/test/a.js @@ -39,5 +39,7 @@ describe('test/xxx.test.js', () => { obj.should.have.properties({ a: '123', c: false }); obj.should.have.property('a'); a.should.length(3); + a.should.be.empty; + a.should.be.empty(); }); }); diff --git a/test/fixtures/input/test/b.js b/test/fixtures/input/test/b.js index 4156ef4..6f00873 100644 --- a/test/fixtures/input/test/b.js +++ b/test/fixtures/input/test/b.js @@ -63,5 +63,7 @@ describe('test/b.test.js', () => { obj.should.not.be.a.Date(); b.should.be.true; c.should.be.false; + b.should.be.true(); + c.should.be.false(); }); }); diff --git a/test/fixtures/output/test/a.js b/test/fixtures/output/test/a.js index e88aa90..0b62536 100644 --- a/test/fixtures/output/test/a.js +++ b/test/fixtures/output/test/a.js @@ -39,5 +39,7 @@ describe('test/xxx.test.js', () => { Object.keys({ a: '123', c: false }).forEach(p => assert.deepEqual((obj)[p], ({ a: '123', c: false })[p])); assert(Object.prototype.hasOwnProperty.call(obj, 'a')); assert(a.length === 3); + assert(a.length === 0); + assert(a.length === 0); }); }); diff --git a/test/fixtures/output/test/b.js b/test/fixtures/output/test/b.js index 9699fe4..56c6442 100644 --- a/test/fixtures/output/test/b.js +++ b/test/fixtures/output/test/b.js @@ -62,5 +62,7 @@ describe('test/b.test.js', () => { assert(Object.prototype.toString.call(obj) !== '[object Date]'); assert(b); assert(!c); + assert(b); + assert(!c); }); });