Skip to content

Commit 2f00201

Browse files
authored
fix: stack traces for get APIs (#17)
<!-- Please provide enough information so that others can review your pull request. --> <!-- Keep pull requests small and focused on a single change. --> ### Summary When moving `get` APIs outside of `render`, we needed to wrap them with a closure injecting `instance` to act on. This resulted in an anonymous callsite that wasn't properly processed by our `ErrorWithStack` helper. ### Test plan |before|after| |--|--| |<img width="305" alt="screenshot 2018-10-07 at 16 09 27" src="https://user-images.githubusercontent.com/5106466/46582694-6fb08f00-ca4b-11e8-94f8-40dcb2211d46.png">|<img width="491" alt="screenshot 2018-10-07 at 16 09 18" src="https://user-images.githubusercontent.com/5106466/46582698-73dcac80-ca4b-11e8-94ce-4fd16ce1f845.png">|
1 parent 90fb410 commit 2f00201

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed

src/helpers/getByAPI.js

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,68 +13,68 @@ const getNodeByText = (node, text) =>
1313
? text === node.props.children
1414
: text.test(node.props.children));
1515

16-
export const getByName = (instance: ReactTestInstance) => (
17-
name: string | React.ComponentType<*>
18-
) => {
19-
try {
20-
return instance.find(node => getNodeByName(node, name));
21-
} catch (error) {
22-
throw new ErrorWithStack(`Error: Component not found.`, getByName);
23-
}
24-
};
16+
export const getByName = (instance: ReactTestInstance) =>
17+
function getByNameFn(name: string | React.ComponentType<*>) {
18+
try {
19+
return instance.find(node => getNodeByName(node, name));
20+
} catch (error) {
21+
throw new ErrorWithStack(`Component not found.`, getByNameFn);
22+
}
23+
};
2524

26-
export const getByText = (instance: ReactTestInstance) => (
27-
text: string | RegExp
28-
) => {
29-
try {
30-
return instance.find(node => getNodeByText(node, text));
31-
} catch (error) {
32-
throw new ErrorWithStack(`Error: Component not found.`, getByText);
33-
}
34-
};
25+
export const getByText = (instance: ReactTestInstance) =>
26+
function getByTextFn(text: string | RegExp) {
27+
try {
28+
return instance.find(node => getNodeByText(node, text));
29+
} catch (error) {
30+
throw new ErrorWithStack(`Component not found.`, getByTextFn);
31+
}
32+
};
3533

36-
export const getByProps = (instance: ReactTestInstance) => (props: {
37-
[propName: string]: any,
38-
}) => {
39-
try {
40-
return instance.findByProps(props);
41-
} catch (error) {
42-
throw new ErrorWithStack(`Error: Component not found.`, getByProps);
43-
}
44-
};
34+
export const getByProps = (instance: ReactTestInstance) =>
35+
function getByPropsFn(props: { [propName: string]: any }) {
36+
try {
37+
return instance.findByProps(props);
38+
} catch (error) {
39+
throw new ErrorWithStack(`Component not found.`, getByPropsFn);
40+
}
41+
};
4542

46-
export const getByTestId = (instance: ReactTestInstance) => (testID: string) =>
47-
getByProps(instance)({ testID });
43+
export const getByTestId = (instance: ReactTestInstance) =>
44+
function getByTestIdFn(testID: string) {
45+
try {
46+
return instance.findByProps({ testID });
47+
} catch (error) {
48+
throw new ErrorWithStack(`Component not found.`, getByTestIdFn);
49+
}
50+
};
4851

49-
export const getAllByName = (instance: ReactTestInstance) => (
50-
name: string | React.ComponentType<*>
51-
) => {
52-
const results = instance.findAll(node => getNodeByName(node, name));
53-
if (results.length === 0) {
54-
throw new ErrorWithStack(`Error: Components not found.`, getAllByName);
55-
}
56-
return results;
57-
};
52+
export const getAllByName = (instance: ReactTestInstance) =>
53+
function getAllByNameFn(name: string | React.ComponentType<*>) {
54+
const results = instance.findAll(node => getNodeByName(node, name));
55+
if (results.length === 0) {
56+
throw new ErrorWithStack(`Components not found.`, getAllByNameFn);
57+
}
58+
return results;
59+
};
5860

59-
export const getAllByText = (instance: ReactTestInstance) => (
60-
text: string | RegExp
61-
) => {
62-
const results = instance.findAll(node => getNodeByText(node, text));
63-
if (results.length === 0) {
64-
throw new ErrorWithStack(`Error: Components not found.`, getAllByText);
65-
}
66-
return results;
67-
};
61+
export const getAllByText = (instance: ReactTestInstance) =>
62+
function getAllByTextFn(text: string | RegExp) {
63+
const results = instance.findAll(node => getNodeByText(node, text));
64+
if (results.length === 0) {
65+
throw new ErrorWithStack(`Components not found.`, getAllByTextFn);
66+
}
67+
return results;
68+
};
6869

69-
export const getAllByProps = (instance: ReactTestInstance) => (props: {
70-
[propName: string]: any,
71-
}) => {
72-
const results = instance.findAllByProps(props);
73-
if (results.length === 0) {
74-
throw new ErrorWithStack(`Error: Components not found.`, getAllByProps);
75-
}
76-
return results;
77-
};
70+
export const getAllByProps = (instance: ReactTestInstance) =>
71+
function getAllByPropsFn(props: { [propName: string]: any }) {
72+
const results = instance.findAllByProps(props);
73+
if (results.length === 0) {
74+
throw new ErrorWithStack(`Components not found.`, getAllByPropsFn);
75+
}
76+
return results;
77+
};
7878

7979
export const getByAPI = (instance: ReactTestInstance) => ({
8080
getByTestId: getByTestId(instance),

0 commit comments

Comments
 (0)