From 1f9c35238b75dec911143a40c5220219908610ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 7 Oct 2018 16:08:59 +0200 Subject: [PATCH 1/3] fix: stacktraces for get APIs --- src/helpers/errorWithStack.js | 2 +- src/helpers/getByAPI.js | 108 ++++++++++++++++------------------ 2 files changed, 53 insertions(+), 57 deletions(-) diff --git a/src/helpers/errorWithStack.js b/src/helpers/errorWithStack.js index 1ee1cb3c2..43f9940d7 100644 --- a/src/helpers/errorWithStack.js +++ b/src/helpers/errorWithStack.js @@ -1,6 +1,6 @@ // @flow export default class ErrorWithStack extends Error { - constructor(message: ?string, callsite: Function) { + constructor(message: ?string, callsite?: Function) { super(message); if (Error.captureStackTrace) { Error.captureStackTrace(this, callsite); diff --git a/src/helpers/getByAPI.js b/src/helpers/getByAPI.js index 999d45ced..74e523dac 100644 --- a/src/helpers/getByAPI.js +++ b/src/helpers/getByAPI.js @@ -13,68 +13,64 @@ const getNodeByText = (node, text) => ? text === node.props.children : text.test(node.props.children)); -export const getByName = (instance: ReactTestInstance) => ( - name: string | React.ComponentType<*> -) => { - try { - return instance.find(node => getNodeByName(node, name)); - } catch (error) { - throw new ErrorWithStack(`Error: Component not found.`, getByName); - } -}; +export const getByName = (instance: ReactTestInstance) => + function getByNameFn(name: string | React.ComponentType<*>) { + try { + return instance.find(node => getNodeByName(node, name)); + } catch (error) { + throw new ErrorWithStack(`Component not found.`, getByNameFn); + } + }; -export const getByText = (instance: ReactTestInstance) => ( - text: string | RegExp -) => { - try { - return instance.find(node => getNodeByText(node, text)); - } catch (error) { - throw new ErrorWithStack(`Error: Component not found.`, getByText); - } -}; +export const getByText = (instance: ReactTestInstance) => + function getByTextFn(text: string | RegExp) { + try { + return instance.find(node => getNodeByText(node, text)); + } catch (error) { + throw new ErrorWithStack(`Component not found.`, getByTextFn); + } + }; -export const getByProps = (instance: ReactTestInstance) => (props: { - [propName: string]: any, -}) => { - try { - return instance.findByProps(props); - } catch (error) { - throw new ErrorWithStack(`Error: Component not found.`, getByProps); - } -}; +export const getByProps = (instance: ReactTestInstance) => + function getByPropsFn(props: { [propName: string]: any }) { + try { + return instance.findByProps(props); + } catch (error) { + throw new ErrorWithStack(`Component not found.`, getByPropsFn); + } + }; -export const getByTestId = (instance: ReactTestInstance) => (testID: string) => - getByProps(instance)({ testID }); +export const getByTestId = (instance: ReactTestInstance) => + function getByTestIdFn(testID: string) { + return getByProps(instance)({ testID }); + }; -export const getAllByName = (instance: ReactTestInstance) => ( - name: string | React.ComponentType<*> -) => { - const results = instance.findAll(node => getNodeByName(node, name)); - if (results.length === 0) { - throw new ErrorWithStack(`Error: Components not found.`, getAllByName); - } - return results; -}; +export const getAllByName = (instance: ReactTestInstance) => + function getAllByNameFn(name: string | React.ComponentType<*>) { + const results = instance.findAll(node => getNodeByName(node, name)); + if (results.length === 0) { + throw new ErrorWithStack(`Components not found.`, getAllByNameFn); + } + return results; + }; -export const getAllByText = (instance: ReactTestInstance) => ( - text: string | RegExp -) => { - const results = instance.findAll(node => getNodeByText(node, text)); - if (results.length === 0) { - throw new ErrorWithStack(`Error: Components not found.`, getAllByText); - } - return results; -}; +export const getAllByText = (instance: ReactTestInstance) => + function getAllByTextFn(text: string | RegExp) { + const results = instance.findAll(node => getNodeByText(node, text)); + if (results.length === 0) { + throw new ErrorWithStack(`Components not found.`, getAllByTextFn); + } + return results; + }; -export const getAllByProps = (instance: ReactTestInstance) => (props: { - [propName: string]: any, -}) => { - const results = instance.findAllByProps(props); - if (results.length === 0) { - throw new ErrorWithStack(`Error: Components not found.`, getAllByProps); - } - return results; -}; +export const getAllByProps = (instance: ReactTestInstance) => + function getAllByPropsFn(props: { [propName: string]: any }) { + const results = instance.findAllByProps(props); + if (results.length === 0) { + throw new ErrorWithStack(`Components not found.`, getAllByPropsFn); + } + return results; + }; export const getByAPI = (instance: ReactTestInstance) => ({ getByTestId: getByTestId(instance), From a764bf7abcf6626ad7537ff435a9cb3da5468c94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 7 Oct 2018 17:04:25 +0200 Subject: [PATCH 2/3] fix getByTestId error --- src/helpers/getByAPI.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/helpers/getByAPI.js b/src/helpers/getByAPI.js index 74e523dac..5fc2380ca 100644 --- a/src/helpers/getByAPI.js +++ b/src/helpers/getByAPI.js @@ -42,7 +42,11 @@ export const getByProps = (instance: ReactTestInstance) => export const getByTestId = (instance: ReactTestInstance) => function getByTestIdFn(testID: string) { - return getByProps(instance)({ testID }); + try { + return instance.findByProps({ testID }); + } catch (error) { + throw new ErrorWithStack(`Component not found.`, getByTestIdFn); + } }; export const getAllByName = (instance: ReactTestInstance) => From 50641a87f168f42222d751122b9104ea589a195e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 7 Oct 2018 17:08:59 +0200 Subject: [PATCH 3/3] require callsite --- src/helpers/errorWithStack.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/errorWithStack.js b/src/helpers/errorWithStack.js index 43f9940d7..1ee1cb3c2 100644 --- a/src/helpers/errorWithStack.js +++ b/src/helpers/errorWithStack.js @@ -1,6 +1,6 @@ // @flow export default class ErrorWithStack extends Error { - constructor(message: ?string, callsite?: Function) { + constructor(message: ?string, callsite: Function) { super(message); if (Error.captureStackTrace) { Error.captureStackTrace(this, callsite);