-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Codemods for transitioning from tap/tape/mocha #644
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
Comments
Love this idea! Would be super cool if someone could post links to great medium-sized projects using tap/tape/mocha to test these codemods against :) |
Here's a start for converting mocha -> ava. Certainly not comprehensive but it works for my current needs. Someone can start with this and build it up. All it does for now is rename Also adds the |
Would also be nice to convert mocha's: describe('group', () => {
it('test');
}); into: test('group - test'); |
@vdemedes, Like I said, this is just a start. Changing describe blocks can get hairy because in mocha they might have their own set of lifecycle hooks (beforeEach, etc.) I probably don't have time to do a full codemod, or else I would have opened a pull request. Just thought it might be useful to people until a full one is implemented. |
@spudly Of course, just dumping thoughts ;) Thanks, definitely helpful! |
For completeness, here's a list of everything that I think would be needed for a complete mocha codemod:
Also, it would be nice to have a separate codemod to transform chai assertions to ava assertions. |
Adding to the list
👍 on the separate codemod for chai assertions. |
Short of implementing #222, I am not sure. We could try converting them to setup functions: describe('outer', function () {
beforeEach(function() {
// outer beforeEach
});
it('outerTest', function () {
// outer test
}
describe('inner', function () {
beforeEach(function () {
// inner beforeEach
});
it('innerTest', function() {
// inner test
});
});
}); converts to: function outerBeforeEach() {
// outer beforeEach
}
function innerBeforeEach() {
// inner beforeEach
}
test('outerTest', t => {
outerBeforeEach();
// outer test
});
test('innerTest', t => {
outerBeforeEach();
innerBeforeEach();
// inner test
}); Honestly though - it seems so complicated, it might just be better to implement grouping in AVA if we really want to make the transition easy. |
A big problem will be handling variables declared / setup in describe('group', function () {
var fn, spy;
beforeEach(function () {
spy = sinon.spy();
fn = proxyquire('some-module', {
'some-dependency': spy
});
});
it('test1', function () {
fn(someArgs);
assert(spy.calledWith(...));
});
it('test2', function () {
fn(someArgs);
assert(spy.calledWith(...));
});
}); I think maybe the best solution might be to just use |
For nested describes, or too many side-by-side describes, we could maybe split tests up into multiple files. I know jscodeshift doesn't allow to create new files or update files other than the currently evaluated one, but what we can do is split them up into clearly separated parts in the same file, so that the user can easily cut-paste each section in a new file. Using your previous input example <<<<<< innerTest.js
test.beforeEach(...); // outer beforeEach
test.beforeEach(...); // inner beforeEach
test('innerTest', t => {
});
<<<<<< outerTest.js
test.beforeEach(...); // outer beforeEach
test('outerTest', t => {
}); We could then create a tool that applies the jscodemod, then reads the updated files, creates new files and cut-paste the code inside the appropriate file. We don't have to do a 100% jscodeshift transformation tool. |
Why not? |
Well, it's a simple AST transform tool, but you're right that we don't have to play "by-the-rules", and we could call fs methods from there. Might not be a bad idea |
Even splitting into multiple files will get hairy, because nested lifecycle hooks get merged by mocha. For example, describe('a', () => {
beforeEach(setupA);
describe('b', () => {
beforeEach(setupB);
it('testA', testA);
it('testB', testB);
it('testC', testC);
});
}); This will execute functions in the following order
|
@spudly Isn't that the same thing as? test.beforeEach(setupA);
test.beforeEach(setupB);
test(testA);
test(testB);
test(testC); |
Yes, I suppose you're right. We'd just have to make sure all applicable hooks are included in each sub-file. |
The way the |
Everyone in the conversation so far is added as a collaborator. |
I have created issues for each of the different conversions being discussed in the other repo:
Let's continue conversation over there. |
Using jscodeshift.
It would pretty much let users transform their existing tap/tape/mocha tests to AVA with minimal manual changes. From tap/tape would be the easiest as they have very similar syntax and semantics. This would result in an incredible onboarding experience! 🦄
Let me know if anyone would be interested in working on codemods for transitioning from either tap, tape, or mocha. I would be happy to mentor about the differences between tap/tape/mocha and AVA.
Resources
From #178.
The text was updated successfully, but these errors were encountered: