20

I am writing a simple test using react-native-testing-library (my first steps with that library) in my react native expo app. But I am getting a confused error coming from somewhere inside react-native code base itself. Either there is something wrong with my code or there is a bug with react-native-testing-library npm library.

Here is simple jest test:

describe("AppTitle", () => {
  it("should display applicaton title", () => {
    const { getByText } = render(<AppTitle />);
    expect(getByText('App Name')).toBeTruthy();
  });
});

And here is the simple <AppTitle /> component (just a View and a Text)

export const AppTitle = () => {
  return (
    <View>
      <Text>App Name</Text>
    </View>
  );
};

But I am getting this error when I run the test:

enter image description here

...../Utilities/warnOnce.js:15

const warnedKeys: {[string]: boolean} = {};
      ^^^^^^^^^^

SyntaxError: Missing initializer in const declaration

at ScriptTransformer.transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:471:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:513:25)
at Object.<anonymous> (node_modules/react-native/Libraries/react-native/react-native-implementation.js:14:18)
...

This is a simple and straight forward template. Any help from a react-native + react-native-testing-library would be appreciate.

  1. react: 16.8.3
  2. react-native: fork from Expo 33
  3. jest-expo: "^33.0.2"
  4. react-native-testing-library": "1.7.0"
4
  • 1
    how did you set up TypeScript compiling? seems like your TS type declaration goes untransformed so JS interpreter suppose : to be just a label or something like that Commented Jul 23, 2019 at 13:47
  • @skyboyer I created this project with "expo init app_name". I have not ejected and I have messed up any setting. The new expo cli tool builds the app with babel7 which has built-in support for typescript. Maybe I should some preset in the babel.config.js...I don't know... Commented Jul 24, 2019 at 10:05
  • you need to install/setup ts-jest or babel-jest or check if it's configured if already installed levelup.gitconnected.com/… beware that TS support for Jest and for building are independent so even if one works another still may be misconfigured Commented Jul 24, 2019 at 10:11
  • Thanks for the suggestion. I installed ts-jest and configured jest.config. But now I get this error: SyntaxError: Unexpected token < (on the line for render(<AppTitle />). It looks like the .tsx file has not been transpiled. I continue to investigate Commented Jul 24, 2019 at 10:58

3 Answers 3

20

I resolved this added "preset": "react-native" in jest.config.js file

Sign up to request clarification or add additional context in comments.

2 Comments

Sadly this didn't help me. I am still stuck at the same error as above :(
As per the Jest documentation, this is now supposed to be included by default in the package.json file
3

This can sometimes happen if the location of the babel preset library has moved or has been updated in a major update migration e.g. RN 74 -> 77. One way you might fix this is by updating the babel.config.js or equivalant preset line from:

module.exports = {
    presets: ['module:metro-react-native-babel-preset'],
};

to

module.exports = {
    presets: ['module:@react-native/babel-preset']
};

Run react-native start --reset-cache and try again

1 Comment

This solution worked for me. The issue seems to have been caused by my update on babel-preset during my migration from 0.74 to 0.78
2

I am using expo in my project. I have same issue. i forgot to add "preset": "jest-expo" to package.json. i added then the problem solved.

 "jest": { "preset": "jest-expo" },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.