This repository has been archived by the owner on Jul 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add mocha to jest transform (by example of jest-codemods). Also add get and forEach methods to collection
- Loading branch information
1 parent
561e638
commit acf44d6
Showing
9 changed files
with
241 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as ts from 'typescript'; | ||
import { Collection } from './collection'; | ||
import * as types from './types'; | ||
|
||
const api = { | ||
tscodeshift: (source: string|ts.Node) => { | ||
if (typeof source === 'string') { | ||
return Collection.fromSource(source); | ||
} else { | ||
return Collection.fromNode(source); | ||
} | ||
} | ||
}; | ||
|
||
/* @internal */ | ||
export function applyTransforms(path: string, source: string, transforms: types.Transform[]): string { | ||
const file: types.File = { | ||
path, | ||
source | ||
}; | ||
return transforms | ||
.reduce((file, transform) => { | ||
return { | ||
path: file.path, | ||
source: transform(file, api) | ||
}; | ||
}, file) | ||
.source; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { stripIndent } from 'common-tags'; | ||
import { applyTransforms } from '../transform'; | ||
|
||
import transform from './jest'; | ||
|
||
test('Convert mocha test to jest test', () => { | ||
const source = stripIndent` | ||
before(() => { | ||
// something | ||
}); | ||
suite('Array', function() { | ||
setup(() => { | ||
}); | ||
specify.skip('Array', function(done) { | ||
done(); | ||
}); | ||
}); | ||
`; | ||
const expected = stripIndent` | ||
beforeAll(()=> { | ||
// something | ||
}); | ||
describe('Array', function() { | ||
beforeEach(()=> { | ||
}); | ||
it.skip('Array', function(done) { | ||
done(); | ||
}); | ||
}); | ||
`; | ||
const actual = applyTransforms('path.ts', source, [transform]); | ||
expect(actual).toBe(expected); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import * as ts from 'typescript'; | ||
import { File, API } from '../types'; | ||
|
||
const methodMap: { [key: string]: string } = { | ||
suite: 'describe', | ||
context: 'describe', | ||
specify: 'it', | ||
test: 'it', | ||
before: 'beforeAll', | ||
beforeEach: 'beforeEach', | ||
setup: 'beforeEach', | ||
after: 'afterAll', | ||
afterEach: 'afterEach', | ||
teardown: 'afterEach', | ||
suiteSetup: 'beforeAll', | ||
suiteTeardown: 'afterAll' | ||
}; | ||
|
||
// const jestMethodsWithDescriptionsAllowed = new Set(['it', 'describe']); | ||
|
||
const methodModifiers = ['only', 'skip']; | ||
|
||
// function hasBinding(name, scope) { | ||
// if (!scope) { | ||
// return false; | ||
// } | ||
|
||
// const bindings = Object.keys(scope.getBindings()) || []; | ||
// if (bindings.indexOf(name) >= 0) { | ||
// return true; | ||
// } | ||
|
||
// return scope.isGlobal ? false : hasBinding(name, scope.parent); | ||
// } | ||
|
||
export default function mochaToJest(file: File, api: API): string { | ||
const t = api.tscodeshift; | ||
const ast = t(file.source); | ||
|
||
Object.keys(methodMap).forEach(mochaMethod => { | ||
const jestMethod = methodMap[mochaMethod]; | ||
|
||
ast | ||
.find(ts.SyntaxKind.CallExpression, { | ||
expression: { | ||
kind: ts.SyntaxKind.Identifier, | ||
text: mochaMethod | ||
} | ||
}) | ||
// .filter(({ scope }) => !hasBinding(mochaMethod, scope)) | ||
.get(node => node.expression) | ||
.replaceWith(() => { | ||
// let args = node.arguments; | ||
// if (!jestMethodsWithDescriptionsAllowed.has(jestMethod)) { | ||
// args = args.filter(a => a.kind !== ts.SyntaxKind.LiteralType); | ||
// } | ||
return ts.createIdentifier(jestMethod); | ||
}); | ||
|
||
methodModifiers.forEach(modifier => { | ||
ast | ||
.find(ts.SyntaxKind.CallExpression, { | ||
expression: { | ||
kind: ts.SyntaxKind.PropertyAccessExpression, | ||
expression: { | ||
kind: ts.SyntaxKind.Identifier, | ||
text: mochaMethod | ||
}, | ||
name: { | ||
kind: ts.SyntaxKind.Identifier, | ||
text: modifier | ||
} | ||
} | ||
}) | ||
.get(node => (node.expression as ts.PropertyAccessExpression).expression) | ||
.replaceWith(() => ts.createIdentifier(jestMethod)); | ||
ast | ||
.find(ts.SyntaxKind.CallExpression, { | ||
expression: { | ||
kind: ts.SyntaxKind.PropertyAccessExpression, | ||
expression: { | ||
kind: ts.SyntaxKind.Identifier, | ||
text: mochaMethod | ||
}, | ||
name: { | ||
kind: ts.SyntaxKind.Identifier, | ||
text: modifier | ||
} | ||
} | ||
}) | ||
.get(node => (node.expression as ts.PropertyAccessExpression).name) | ||
.replaceWith(() => ts.createIdentifier(modifier)); | ||
}); | ||
}); | ||
|
||
return ast.toSource(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters