feat(es/jest): Support chaining of jest function calls (#6747)

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/6540.
This commit is contained in:
Donny/강동윤 2023-01-04 14:15:21 +09:00 committed by GitHub
parent 1638105865
commit 72fb606eb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 6 deletions

View File

@ -0,0 +1,23 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"dynamicImport": true,
"decorators": true
},
"transform": {
"legacyDecorator": true,
"hidden": {
"jest": true
}
},
"target": "es2016",
"loose": false,
"externalHelpers": false,
"keepClassNames": false
},
"module": {
"type": "commonjs"
}
}

View File

@ -0,0 +1,7 @@
import foo from "./foo";
jest.mock("./foo").mock("./bar");
test("Foo is a mock", () => {
expect(jest.isMockFunction(foo)).toBe(true);
});

View File

@ -0,0 +1,10 @@
"use strict";
jest.mock("./foo").mock("./bar");
Object.defineProperty(exports, "__esModule", {
value: true
});
const _interopRequireDefault = require("@swc/helpers/lib/_interop_require_default.js").default;
const _foo = /*#__PURE__*/ _interopRequireDefault(require("./foo"));
test("Foo is a mock", ()=>{
expect(jest.isMockFunction(_foo.default)).toBe(true);
});

View File

@ -44,14 +44,13 @@ impl Jest {
prop: MemberProp::Ident(prop),
..
},
) => match &*callee.obj {
Expr::Ident(i)
if i.sym == *"jest" && HOIST_METHODS.contains(&*prop.sym) =>
{
) => {
if is_jest(&callee.obj) && HOIST_METHODS.contains(&*prop.sym) {
hoisted.push(T::from_stmt(stmt))
} else {
new.push(T::from_stmt(stmt));
}
_ => new.push(T::from_stmt(stmt)),
},
}
_ => new.push(T::from_stmt(stmt)),
},
_ => new.push(T::from_stmt(stmt)),
@ -80,3 +79,15 @@ impl VisitMut for Jest {
self.visit_mut_stmt_like(stmts)
}
}
fn is_jest(e: &Expr) -> bool {
match e {
Expr::Ident(i) => i.sym == *"jest",
Expr::Member(MemberExpr { obj, .. }) => is_jest(obj),
Expr::Call(CallExpr {
callee: Callee::Expr(callee),
..
}) => is_jest(callee),
_ => false,
}
}