question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

testing application components with jest fails

See original GitHub issue

Good day, we are using this amazing library in our app, a bare expo app and we faced 2 challenges. specifically with jest.

  1. the main key in package.json points to the esmodule build and not the commonjs build as expected
  2. after loading the commonjs build, tests fail with this message: Native RNSkia Module cannot be found when testing a component that uses any of react-native-skia exports

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
salzhranicommented, Jun 16, 2022

My solution which I can share is to mock views and methods exposed and bypass everything and return plain views for views and noop function for everything else

import { View } from 'react-native';
import { createElement } from 'react';

const PlainView = ({ children, ...props }: any) =>
  createElement(View, props, children);
const noop = () => null;

jest.mock('@shopify/react-native-skia', () => {
  const mock: { [key: string]: any } = {
    // other props can be added which
    // aren't handled properly
    // by the handler
    Canvas: PlainView,
  };
  const handler = {
    get(_: any, prop: string, __: any) {
      // first look for the prop in the mock
      if (prop in mock) {
        return mock[prop];
      }
      // class case? return a view
      if (prop[0] === prop[0].toUpperCase()) {
        return PlainView;
      }
      // probably a method
      return noop;
    },
  };
  return new Proxy(mock, handler);
});

if you think it is useful I can make a PR

1reaction
chrfalchcommented, Jun 14, 2022

Hi @flo-sch, super awesome findings and write-up, we really appreciate this! Would be super happy if you could help with creating Mocks for the library. Maybe we could collaborate and find a solution for the NativeSetup issue you’re seeing - I would imagine that we could create some kind of Platform test for this - the same goes for the global object issue. We already have Platform switches for Native / Web in the code!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing components with Jest fails - Stack Overflow
I am new to Jest and Java Script in general. I wrote a test to one of my components but it seems to...
Read more >
A Practical Guide To Testing React Applications With Jest
In this article, you will learn everything you need to create a solid test for your React components and application.
Read more >
Testing React Apps - Jest
React 16 triggers these warnings due to how it checks element types, and the mocked module fails these checks. Your options are: Render...
Read more >
How To Test a React App with Jest and React Testing Library
Press `a` to run all tests, or run Jest with `--watchAll`. Watch Usage › Press a to run all tests. › Press f...
Read more >
Testing - React Native
Your app's logic would work without any React components at all! ... The default template of React Native ships with Jest testing framework....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found