From 17b9fe0033c263f21c845390aadd8297dbda9465 Mon Sep 17 00:00:00 2001 From: "Chris West (Faux)" Date: Sat, 5 Dec 2020 11:52:30 +0000 Subject: [PATCH] feat: babel plugin: generate our own expect --- .../deadlinesPlugin.test.ts.snap | 26 +++++++++++++----- .../babel-plugin-jest-deadlines/src/index.ts | 27 +++++++++++++++---- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/packages/babel-plugin-jest-deadlines/src/__tests__/__snapshots__/deadlinesPlugin.test.ts.snap b/packages/babel-plugin-jest-deadlines/src/__tests__/__snapshots__/deadlinesPlugin.test.ts.snap index d36dfdf52e0c..00e3e4c7d2df 100644 --- a/packages/babel-plugin-jest-deadlines/src/__tests__/__snapshots__/deadlinesPlugin.test.ts.snap +++ b/packages/babel-plugin-jest-deadlines/src/__tests__/__snapshots__/deadlinesPlugin.test.ts.snap @@ -8,8 +8,10 @@ async function foo() { ↓ ↓ ↓ ↓ ↓ ↓ +const _expect = require("@jest/globals").expect; + async function foo() { - await expect.withinDeadline(bar()); + await _expect.withinDeadline(bar()); } @@ -23,8 +25,10 @@ async function foo() { ↓ ↓ ↓ ↓ ↓ ↓ +const _expect = require("@jest/globals").expect; + async function foo() { - await expect.withinDeadline(bar()); + await _expect.withinDeadline(expect.withinDeadline(bar())); } @@ -38,8 +42,10 @@ async function foo() { ↓ ↓ ↓ ↓ ↓ ↓ +const _expect = require("@jest/globals").expect; + async function foo() { - await expect.withinDeadline(expect(bar()).resolves.toBe("hot potatoes")); + await _expect.withinDeadline(expect(bar()).resolves.toBe("hot potatoes")); } @@ -53,8 +59,10 @@ async function foo() { ↓ ↓ ↓ ↓ ↓ ↓ +const _expect = require("@jest/globals").expect; + async function foo() { - return await expect.withinDeadline(bar()); + return await _expect.withinDeadline(bar()); } @@ -68,8 +76,10 @@ async function foo() { ↓ ↓ ↓ ↓ ↓ ↓ +const _expect = require("@jest/globals").expect; + async function foo() { - await expect.withinDeadline(bar(1, await expect.withinDeadline(quux()), 3)); + await _expect.withinDeadline(bar(1, await _expect.withinDeadline(quux()), 3)); } @@ -84,9 +94,11 @@ async function foo() { ↓ ↓ ↓ ↓ ↓ ↓ +const _expect = require("@jest/globals").expect; + async function foo() { - await expect.withinDeadline(bar(1, 2, 3)); - await expect.withinDeadline(quux(1, 2, 3)); + await _expect.withinDeadline(bar(1, 2, 3)); + await _expect.withinDeadline(quux(1, 2, 3)); } diff --git a/packages/babel-plugin-jest-deadlines/src/index.ts b/packages/babel-plugin-jest-deadlines/src/index.ts index 685094da0076..a4da501e4790 100644 --- a/packages/babel-plugin-jest-deadlines/src/index.ts +++ b/packages/babel-plugin-jest-deadlines/src/index.ts @@ -7,13 +7,33 @@ */ import type {PluginObj, types as babelTypes} from '@babel/core'; +import type {Identifier} from '@babel/types'; export default ({ types: t, }: { types: typeof babelTypes; }): PluginObj<{ + ourExpect: Identifier; }> => ({ + pre({path: program}) { + this.ourExpect = program.scope.generateUidIdentifier('expect'); + + // const _expect = require("@jest/globals").expect; + const decl = t.variableDeclaration('const', [ + t.variableDeclarator( + this.ourExpect, + t.memberExpression( + t.callExpression(t.identifier('require'), [ + t.stringLiteral('@jest/globals'), + ]), + t.identifier('expect'), + ), + ), + ]); + + program.unshiftContainer('body', decl); + }, visitor: { AwaitExpression(path) { const original = path.node.argument; @@ -25,7 +45,7 @@ export default ({ if (t.isMemberExpression(original.callee)) { const member = original.callee; if ( - t.isIdentifier(member.object, {name: 'expect'}) && + t.isIdentifier(member.object, this.ourExpect) && t.isIdentifier(member.property, {name: 'withinDeadline'}) ) { return; @@ -35,10 +55,7 @@ export default ({ path.replaceWith( t.awaitExpression( t.callExpression( - t.memberExpression( - t.identifier('expect'), - t.identifier('withinDeadline'), - ), + t.memberExpression(this.ourExpect, t.identifier('withinDeadline')), [path.node.argument], ), ),