Created
February 11, 2021 10:19
-
-
Save FunnyGhost/0cb2ee4549068d40aa616d66e81d98d9 to your computer and use it in GitHub Desktop.
Convert karma to jest script
This file contains hidden or 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
// Can be run with a file like this: | |
// node convert-to-jest.js src/app/home/search/search.component.spec.ts | |
var fs = require('fs'); | |
var filename = process.argv[2]; | |
if (!filename) { | |
let specs = require('glob').sync('src/**/*.spec.ts'); | |
for (spec of specs) { | |
if (!spec.includes('pact')) { | |
convertToJest(spec); | |
} | |
} | |
} else { | |
convertToJest(filename); | |
} | |
function convertToJest(filename) { | |
if (!filename.startsWith('C:')) { | |
filename = './' + filename; | |
} | |
fs.readFile(filename, 'utf8', function(err, data) { | |
if (err) { | |
return console.log(err); | |
} | |
var result = data; | |
result = result.replace(/SpyObj</g, 'SpyObject<'); | |
result = result.replace(/\.and\.returnValue/g, '.mockReturnValue'); | |
result = result.replace(/\.spec\'/g, '.test'); | |
result = result.replace(/jasmine\.SpyObj/g, 'SpyObj'); | |
result = result.replace(/spyOn/g, 'jest.spyOn'); | |
result = result.replace(/spyOnProperty/g, 'spyOn'); | |
result = result.replace( | |
/expect\((.*)\.calls\.first\(\)\.args\)\.toEqual\(\[(.*)\]\);/g, | |
'expect($1).toHaveBeenCalledWith($2);' | |
); | |
result = result.replace( | |
/expect\((.*)\.calls\.any\(\)\)\.toBe\((.*)\);/g, | |
'expect($1).toHaveBeenCalledWith($2);' | |
); | |
result = result.replace( | |
/expect\((.*)\.calls\.mostRecent\(\)(\.args\[.*\])?\)\.toEqual\((.*)\);/g, | |
'expect($1).toHaveBeenCalledWith($2);' | |
); | |
result = result.replace( | |
/expect\((.*)\.calls\.count\(\)\)\.toBe\((.*)\);/g, | |
'expect($1).toHaveBeenCalledTimes($2);' | |
); | |
result = result.replace( | |
/expect\((.*)\.calls\.count\(\)\)\.toEqual\((.*)\);/g, | |
'expect($1).toHaveBeenCalledTimes($2);' | |
); | |
result = result.replace(/\.calls\.first\(\).args/g, '.mock.calls[0].args'); | |
result = result.replace(/\.calls\.reset\(\)/g, '.mockClear()'); | |
result = result.replace(/and.callFake/g, 'mockImplementation'); | |
result = result.replace(/\.and.callThrough\(\)/g, ''); | |
result = result.replace(/innerText/g, 'textContent'); | |
result = result.replace(/jasmine\.createSpy\(.*?\)/g, 'jest.fn()'); | |
result = result.replace('jasmine.any(', 'expect.any('); | |
result = result.replace('jasmine.anything(', 'expect.anything('); | |
result = result.replace(/jasmine\.objectContaining/g, 'expect.objectContaining'); | |
if (result.includes('createSpyObj')) { | |
result = result.replace(/jasmine\.createSpyObj/g, 'createSpyObj'); | |
result = result.replace(/createSpyObject/g, 'createSpyObj'); | |
result = result.replace(/: SpyObject<(.*)>/g, ': any'); | |
result = result.replace(/createSpyObj<(.*)>/g, 'createSpyObj'); | |
result = result.replace(/<Spy>/g, ''); | |
result = "import { createSpyObj } from '@test-utilities/create-spy';\r\n" + result; | |
} | |
result = result.replace('import SpyObj = SpyObj;', ''); | |
result = result.replace('import Spy = jasmine.Spy;', ''); | |
result = result.replace('import createSpyObj = createSpyObj;', ''); | |
result = result.replace(/ Spy;/g, ' jest.SpyInstance;'); | |
result = result.replace(/jasmine\.Spy;/g, 'jest.SpyInstance;'); | |
if (result.includes('withArgs')) { | |
result = result.replace( | |
/(.*)\.withArgs\((.*)\)\.mockReturnValue\((.*)\)/g, | |
`$1.mockImplementation(flag => { | |
switch (flag) { | |
case $2: | |
return $3; | |
} | |
})` | |
); | |
} | |
result = result.replace(/jest\.jest/g, 'jest'); | |
let newFile = filename.replace('.spec.ts', '.test.ts'); | |
fs.writeFile(newFile, result, 'utf8', function(err) { | |
if (err) return console.log(err); | |
console.log('Successfully wrote ' + newFile); | |
if (newFile != filename) { | |
fs.unlinkSync(filename); | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment