Skip to content

Commit

Permalink
feat: babel plugin: generate our own expect
Browse files Browse the repository at this point in the history
  • Loading branch information
FauxFaux committed Dec 5, 2020
1 parent 009ca12 commit 1f16421
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ async function foo() {
↓ ↓ ↓ ↓ ↓ ↓
const _expect = require("@jest/globals").expect;
async function foo() {
await expect.withinDeadline(bar());
await _expect.withinDeadline(bar());
}
Expand All @@ -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()));
}
Expand All @@ -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"));
}
Expand All @@ -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());
}
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand Down
27 changes: 22 additions & 5 deletions packages/babel-plugin-jest-deadlines/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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],
),
),
Expand Down

0 comments on commit 1f16421

Please sign in to comment.