Skip to content

Commit 20ba4be

Browse files
ConnormihaSimenB
authored andcommitted
Fix getting function name for rare cases (#8362)
1 parent 7e1e3f8 commit 20ba4be

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- `[jest-jasmine2]` Fix describe return value warning being shown if the describe function throws ([#8335](https://github.com/facebook/jest/pull/8335))
2222
- `[jest-environment-jsdom]` Re-declare global prototype of JSDOMEnvironment ([#8352](https://github.com/facebook/jest/pull/8352))
2323
- `[jest-snapshot]` Handle arrays when merging snapshots ([#7089](https://github.com/facebook/jest/pull/7089))
24+
- `[expect]` Extract names of async and generator functions ([#8362](https://github.com/facebook/jest/pull/8362))
2425

2526
### Chore & Maintenance
2627

packages/expect/src/__tests__/asymmetricMatchers.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,47 @@ test('Any.toAsymmetricMatcher()', () => {
4242
jestExpect(any(Number).toAsymmetricMatcher()).toBe('Any<Number>');
4343
});
4444

45+
test('Any.toAsymmetricMatcher() with function name', () => {
46+
[
47+
['someFunc', function someFunc() {}],
48+
['$someFunc', function $someFunc() {}],
49+
[
50+
'$someFunc2',
51+
(function() {
52+
function $someFunc2() {}
53+
Object.defineProperty($someFunc2, 'name', {value: ''});
54+
return $someFunc2;
55+
})(),
56+
],
57+
[
58+
'$someAsyncFunc',
59+
(function() {
60+
async function $someAsyncFunc() {}
61+
Object.defineProperty($someAsyncFunc, 'name', {value: ''});
62+
return $someAsyncFunc;
63+
})(),
64+
],
65+
[
66+
'$someGeneratorFunc',
67+
(function() {
68+
function* $someGeneratorFunc() {}
69+
Object.defineProperty($someGeneratorFunc, 'name', {value: ''});
70+
return $someGeneratorFunc;
71+
})(),
72+
],
73+
[
74+
'$someFuncWithFakeToString',
75+
(function() {
76+
function $someFuncWithFakeToString() {}
77+
$someFuncWithFakeToString.toString = () => 'Fake to string';
78+
return $someFuncWithFakeToString;
79+
})(),
80+
],
81+
].forEach(([name, fn]: [string, any]) => {
82+
jestExpect(any(fn).toAsymmetricMatcher()).toBe(`Any<${name}>`);
83+
});
84+
});
85+
4586
test('Any throws when called with empty constructor', () => {
4687
jestExpect(() => any()).toThrow();
4788
});

packages/expect/src/jasmineUtils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export function equals(
3737
return eq(a, b, [], [], customTesters, strictCheck ? hasKey : hasDefinedKey);
3838
}
3939

40+
const functionToString = Function.prototype.toString;
41+
4042
function isAsymmetric(obj: any) {
4143
return !!obj && isA('Function', obj.asymmetricMatch);
4244
}
@@ -258,7 +260,9 @@ export function fnNameFor(func: Function) {
258260
return func.name;
259261
}
260262

261-
const matches = func.toString().match(/^\s*function\s*(\w*)\s*\(/);
263+
const matches = functionToString
264+
.call(func)
265+
.match(/^(?:async)?\s*function\s*\*?\s*([\w$]+)\s*\(/);
262266
return matches ? matches[1] : '<anonymous>';
263267
}
264268

0 commit comments

Comments
 (0)