-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
298 additions
and
5 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
26 changes: 26 additions & 0 deletions
26
package/plugin/codemods/jest-mock/extract_mocked_specifier.js
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,26 @@ | ||
const babelTypes = require("@babel/types"); | ||
|
||
function extract_mocked_specifier(path, specifier, callback) { | ||
// console.log(path.parent) | ||
const expression = path.parent; | ||
|
||
if (path.node.arguments[1].body.properties.length === 1) { | ||
callback?.(path.parent); | ||
return; | ||
} | ||
|
||
const clone = babelTypes.cloneNode(expression); | ||
clone.expression.arguments[1].body.properties = | ||
clone.expression.arguments[1].body.properties.filter((prop) => { | ||
return prop.key.name === specifier; | ||
}); | ||
path.node.arguments[1].body.properties = | ||
path.node.arguments[1].body.properties.filter((prop) => { | ||
return prop.key.name !== specifier; | ||
}); | ||
callback?.(clone); | ||
|
||
path.insertBefore(clone); | ||
} | ||
|
||
module.exports = { extract_mocked_specifier }; |
122 changes: 122 additions & 0 deletions
122
package/plugin/codemods/jest-mock/extract_mocked_specifier.test.js
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,122 @@ | ||
const { babelParse } = require("../../utils"); | ||
const traverse = require("@babel/traverse").default; | ||
const babelGenerate = require("@babel/generator").default; | ||
const { extract_mocked_specifier } = require("./extract_mocked_specifier"); | ||
const { | ||
multilineTrim, | ||
} = require("../../../../spec/test-utils/expectTransform"); | ||
|
||
function parse(code, callback) { | ||
const ast = babelParse(code); | ||
|
||
traverse(ast, { | ||
CallExpression(path) { | ||
if (path.node.callee?.property?.name === "mock") { | ||
if (path.node.arguments.length > 1) { | ||
callback(path); | ||
} | ||
} | ||
}, | ||
}); | ||
|
||
return ast; | ||
} | ||
|
||
function expectCodemod(code, codemod, expectedOutput) { | ||
const ast = parse(code, (path) => { | ||
return codemod(path); | ||
}); | ||
|
||
const output = multilineTrim(babelGenerate(ast).code); | ||
|
||
expect(output).toBe(multilineTrim(expectedOutput)); | ||
} | ||
|
||
describe("codemods/jest-mock/extract_mocked_specifier", () => { | ||
it("leaves code as is if there is only one key", () => { | ||
expectCodemod( | ||
` | ||
jest.mock('./origin.js', () => ({ | ||
a: 'a' | ||
})); | ||
`, | ||
(path) => extract_mocked_specifier(path, "a"), | ||
` | ||
jest.mock('./origin.js', () => ({ | ||
a: 'a' | ||
})); | ||
`, | ||
); | ||
}); | ||
|
||
it("extracts a separate jest.mock statement for a given specifier", () => { | ||
expectCodemod( | ||
` | ||
jest.mock('./origin.js', () => ({ | ||
a: 'a', | ||
b: 'b' | ||
})); | ||
`, | ||
(path) => extract_mocked_specifier(path, "b"), | ||
` | ||
jest.mock('./origin.js', () => ({ | ||
b: 'b' | ||
})); | ||
jest.mock('./origin.js', () => ({ | ||
a: 'a' | ||
})); | ||
`, | ||
); | ||
}); | ||
|
||
it("can extract multiple specifiers", () => { | ||
expectCodemod( | ||
` | ||
jest.mock('./origin.js', () => ({ | ||
a: 'a', | ||
b: 'b', | ||
c: 'c' | ||
})); | ||
`, | ||
(path) => { | ||
extract_mocked_specifier(path, "b"); | ||
extract_mocked_specifier(path, "c"); | ||
}, | ||
` | ||
jest.mock('./origin.js', () => ({ | ||
b: 'b' | ||
})); | ||
jest.mock('./origin.js', () => ({ | ||
c: 'c' | ||
})); | ||
jest.mock('./origin.js', () => ({ | ||
a: 'a' | ||
})); | ||
`, | ||
); | ||
}); | ||
|
||
it("returns the created node for further processing", () => { | ||
expectCodemod( | ||
` | ||
jest.mock('./origin.js', () => ({ | ||
a: 'a', | ||
b: 'b' | ||
})); | ||
`, | ||
(path) => { | ||
extract_mocked_specifier(path, "b", (node) => { | ||
node.expression.arguments[1].body.properties[0].key.name = "bar"; | ||
}); | ||
}, | ||
` | ||
jest.mock('./origin.js', () => ({ | ||
bar: 'b' | ||
})); | ||
jest.mock('./origin.js', () => ({ | ||
a: 'a' | ||
})); | ||
`, | ||
); | ||
}); | ||
}); |
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,61 @@ | ||
const { | ||
createExpectTransform, | ||
createExpectJestTransform, | ||
} = require("./test-utils/expectTransform.js"); | ||
|
||
const expectJestTransform = createExpectJestTransform(__filename); | ||
const expectTransform = createExpectTransform(__filename); | ||
|
||
describe("babel-jest-boost plugin jest.mock rewrites", () => { | ||
it("resolves modules mocked in jest.mock", () => { | ||
expectTransform( | ||
"import { a } from './test_tree/consts'", | ||
`import { a } from "${__dirname}/test_tree/consts/a.js";`, | ||
); | ||
expectJestTransform( | ||
"jest.mock('./test_tree/default');", | ||
`_getJestObj().mock("${__dirname}/test_tree/default/index.js");`, | ||
); | ||
|
||
expectJestTransform( | ||
"jest.mock('./test_tree/default', () => {});", | ||
`_getJestObj().mock("${__dirname}/test_tree/default/index.js", () => {});`, | ||
); | ||
|
||
expectJestTransform( | ||
`jest.mock('./test_tree/consts', () => ({ a: 1 }));`, | ||
` | ||
_getJestObj().mock("${__dirname}/test_tree/consts/a.js", () => ({ | ||
a: 1 | ||
})); | ||
`, | ||
); | ||
expectJestTransform( | ||
`jest.mock('./test_tree/consts', () => ({ a: 1, b: 2 }));`, | ||
` | ||
_getJestObj().mock("${__dirname}/test_tree/consts/b.js", () => ({ | ||
b: 2 | ||
})); | ||
_getJestObj().mock("${__dirname}/test_tree/consts/a.js", () => ({ | ||
a: 1 | ||
})); | ||
`, | ||
); | ||
|
||
expectJestTransform( | ||
`jest.mock("${__dirname}/test_tree/consts/consts.js", () => ({ a: 1, b: 2, c: 3, d: 4 }));`, | ||
` | ||
_getJestObj().mock("${__dirname}/test_tree/consts/consts.js", () => ({ | ||
c: 3, | ||
d: 4 | ||
})); | ||
_getJestObj().mock("${__dirname}/test_tree/consts/a.js", () => ({ | ||
a: 1 | ||
})); | ||
_getJestObj().mock("${__dirname}/test_tree/consts/b.js", () => ({ | ||
b: 2 | ||
})); | ||
`, | ||
); | ||
}); | ||
}); |
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 @@ | ||
export const a = 1; |
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 @@ | ||
export const b = 2; |
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,4 @@ | ||
export * from "./a.js"; | ||
export * from "./b.js"; | ||
export const c = 3; | ||
export const d = 4; |
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 @@ | ||
export * from "./consts"; |