Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest test case failing with StyleSheet.create

I am currently facing an issue where the every pages Stylesheet .create is the cause of the failure and not sure entirely why it is.. but can someone give me an explanation or a solution?

And one more thing, without enzyme (I am using react-native 0.56, I haven't found an enzyme installation file just yet.) can I simulate a press button based on a class such as a homepage with two buttons as a test case with both on Press?

● Test suite failed to run

TypeError: Cannot read property 'create' of undefined

  162 | }
  163 |
> 164 | const styles = StyleSheet.create({
      |                           ^
  165 |     container: {
  166 |         flex: 1,
  167 |         justifyContent: 'center',

  at Object.<anonymous> (components/landingpage.js:164:27)
  at Object.<anonymous> (__tests__/landingpage.spec.js:3:1)
like image 787
DaemonS Avatar asked Jul 17 '18 04:07

DaemonS


2 Answers

Correct syntax for Amin's answer:

jest.mock('react-native', () => {
  return {
    StyleSheet: {
      create: () => ({}),
    },
  };
});
like image 150
Jay Koutavas Avatar answered Nov 15 '22 23:11

Jay Koutavas


You need to mock it.

jest.mock('react-native', () => {
    let items = {};
        StyleSheet: {
            create: () => ({})
        },
    }
});
like image 4
Amin Avatar answered Nov 16 '22 00:11

Amin