enzyme icon indicating copy to clipboard operation
enzyme copied to clipboard

[New] add `enzyme-adapter-react-17`

Open layershifter opened this issue 3 years ago • 58 comments

Fixes #2429.

layershifter avatar Aug 12 '20 08:08 layershifter

Codecov Report

Attention: Patch coverage is 91.60156% with 43 lines in your changes are missing coverage. Please review.

Project coverage is 95.61%. Comparing base (cdfb1c6) to head (898978f). Report is 88 commits behind head on master.

Files Patch % Lines
...pter-react-17/src/findCurrentFiberUsingSlowPath.js 68.42% 18 Missing :warning:
...zyme-adapter-react-17/src/ReactSeventeenAdapter.js 95.90% 16 Missing :warning:
...ges/enzyme-adapter-react-17/src/detectFiberTags.js 85.24% 9 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2430      +/-   ##
==========================================
- Coverage   96.12%   95.61%   -0.52%     
==========================================
  Files          49       53       +4     
  Lines        4004     4516     +512     
  Branches     1123     1288     +165     
==========================================
+ Hits         3849     4318     +469     
- Misses        155      198      +43     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Aug 12 '20 08:08 codecov[bot]

Dear @layershifter thanks for the PR!

Any update on this? React team has released V17 2 days ago, and It would be very much appreciated if this PR could go through

silver-snoopy avatar Oct 23 '20 07:10 silver-snoopy

FYI, this might be affected by the bug reported in my temporary adapter repo:

https://github.com/wojtekmaj/enzyme-adapter-react-17/issues/1

wojtekmaj avatar Oct 23 '20 10:10 wojtekmaj

Dear @layershifter thanks for the PR!

Any update on this? React team has released V17 2 days ago, and It would be very much appreciated if this PR could go through

I stopped to work on it after @ljharb comment: https://github.com/enzymejs/enzyme/pull/2430#pullrequestreview-466131356. I am also interested in getting this.

layershifter avatar Oct 23 '20 15:10 layershifter

Indeed, that linked bug is one of the blockers I've run into in my local work.

I'd be more than happy to pull in additional commits to this PR, and to my local implementation, in service of getting something published ASAP.

ljharb avatar Oct 23 '20 20:10 ljharb

I've rebased this branch and included my WIP. There's still 8 failing tests (and 2 I've temporarily patched), that fall into these categories:

  1. unknown node with tag 23. tag 23 is "OffscreenComponent" in the latest react-reconciler repo, but I have no idea how to render something useful here.
  2. simulate issues in shallow: marked with "TODO: figure out why this is broken in shallow rendering in react 17"
  3. issues because the getComponentStack function in enzyme-adapter-utils no longer creates an accurate error stack for React 17.

Please post links to commits/branches, and I'll pull them into the PR.

ljharb avatar Oct 26 '20 06:10 ljharb

  1. unknown node with tag 23. tag 23 is "OffscreenComponent" in the latest react-reconciler repo, but I have no idea how to render something useful here.

getLazyFiber("div").return.return.tag should do: https://codesandbox.io/s/offscreencomponent-tag-9hsq9

eps1lon avatar Oct 26 '20 10:10 eps1lon

@eps1lon awesome, thanks! the other side of this is, how do i locate, off of the FiberNode, what should be rendered? It's got a bunch of circular references. The best I could come up with makes this:

class Component extends React.Component {
        render() {
          return (
            <div>test</div>
          );
        }
      }

      const SuspenseComponent = () => (
        <Suspense fallback={Fallback}>
          <Component />
        </Suspense>
      );

map to this:

<SuspenseComponent>
  <Suspense fallback={[Function: Fallback]}>
    <Suspense mode="visible" />
  </Suspense>
</SuspenseComponent>

with this:

     case FiberTags.OffscreenComponent: {                                                                                                                                                                   
       console.log(node.return.memoizedProps.children);                                                                                                                                                     
       return {                                                                                                                                                                                             
         nodeType: 'function',                                                                                                                                                                              
         type: Suspense,                                                                                                                                                                                    
         props: { ...node.memoizedProps },                                                                                                                                                                  
         key: ensureKeyOrUndefined(node.key),                                                                                                                                                               
         ref: node.ref,                                                                                                                                                                                     
         instance: null,                                                                                                                                                                                    
         rendered: childrenToTree(nodeToHostNode(node.return.memoizedProps.children)),                                                                                                                      
       };                                                                                                                                                                                                   
     }                                                                                     

ljharb avatar Oct 26 '20 18:10 ljharb

I don't have more context on how the OffscreenComponent is handled internally in React. From what I could gather it's used in different scenarios. Some only available with experimental APIs. If you can add a test I might be able to help.

eps1lon avatar Oct 26 '20 18:10 eps1lon

@eps1lon thanks! the test is already there; i've updated the branch to have my best output so far.

ljharb avatar Oct 26 '20 18:10 ljharb

@ljharb With

diff --git a/packages/enzyme-adapter-react-17/src/ReactSeventeenAdapter.js b/packages/enzyme-adapter-react-17/src/ReactSeventeenAdapter.js
index 0e69131..bb3477d 100644
--- a/packages/enzyme-adapter-react-17/src/ReactSeventeenAdapter.js
+++ b/packages/enzyme-adapter-react-17/src/ReactSeventeenAdapter.js
@@ -299,16 +299,7 @@ function toTree(vnode) {
     case FiberTags.Lazy:
       return childrenToTree(node.child);
     case FiberTags.OffscreenComponent: {
-      console.log(node.return.memoizedProps.children);
-      return {
-        nodeType: 'function',
-        type: Suspense,
-        props: { ...node.memoizedProps },
-        key: ensureKeyOrUndefined(node.key),
-        ref: node.ref,
-        instance: null,
-        rendered: childrenToTree(nodeToHostNode(node.return.memoizedProps.children)),
-      };
+      return toTree(node.child);
     }
     default:
       throw new Error(`Enzyme Internal Error: unknown node with tag ${node.tag}`);
diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
index c77bf15..234da0b 100644
--- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
+++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
@@ -1112,7 +1112,7 @@ describeWithDOM('mount', () => {
       }
     }
 
-    it.only('finds Suspense and its children when no lazy component', () => {
+    it('finds Suspense and its children when no lazy component', () => {
       class Component extends React.Component {
         render() {
           return (
@@ -1130,7 +1130,6 @@ describeWithDOM('mount', () => {
       const wrapper = mount(<SuspenseComponent />);
 
       expect(wrapper.is(SuspenseComponent)).to.equal(true);
-      console.log(wrapper.debug());
       expect(wrapper.find(Component)).to.have.lengthOf(1);
       expect(wrapper.find(Fallback)).to.have.lengthOf(0);
     });

the suspense tests are passing. npm run test:only is still failing on 2 component stack related tests though.

eps1lon avatar Oct 29 '20 19:10 eps1lon

Awesome, thank you :-) now we're just down to the stack errors.

ljharb avatar Oct 29 '20 22:10 ljharb

Please add a test case for <Suspense> with multiple children. Looks like current approach doesn't cover it properly

<Suspense fallback="">
  <div />
  <div />
</Suspense>

Hypnosphi avatar Oct 30 '20 18:10 Hypnosphi

@Hypnosphi thanks - added in 2c15e59a. The next rebase of this PR will run on it.

ljharb avatar Oct 31 '20 20:10 ljharb

Rebased the branch and fixed some things; we're now at 5 test failures (new tests have landed on master in the interim), all stack trace related. https://travis-ci.com/github/enzymejs/enzyme/jobs/463388239

ljharb avatar Dec 20 '20 22:12 ljharb

@ljharb any updates on this? Would love an official adapter 😍

kamronbatman avatar Feb 23 '21 22:02 kamronbatman

tests on master are still failing; and this PR still has too many failing tests to cut a prerelease. I'm still working on that.

ljharb avatar Feb 23 '21 22:02 ljharb

Appears as though the only issues now (3) are with the Prop Types

jmitchell38488 avatar Mar 07 '21 02:03 jmitchell38488

Curious whether you have seen issues with wrapper.unMount? similar to this: https://github.com/wojtekmaj/enzyme-adapter-react-17/issues/12

I pulled in the changes in this p.r locally and set them as the adaptor being used and seeing a similar issue.

J-Son89 avatar Mar 12 '21 17:03 J-Son89

Appears as though the only issues now (3) are with the Prop Types

I was seeing issues with this too :)

J-Son89 avatar Mar 18 '21 12:03 J-Son89

Hi, Do you know when the enzyme-adapter-react-17 will be available? My company is waiting for this to migrate to React 17 and I think we are not the only one :) Thank you

jRichardeau avatar Apr 27 '21 14:04 jRichardeau

@jRichardeau would your company care to invest some time/resources in getting the tests on this PR to pass? that's the best way to move things along.

ljharb avatar Apr 27 '21 18:04 ljharb

Ok it's fair :), my question was badly asked, I just wanted to know if you think this could be available in the next few weeks. I'd love to help, I'll see what I can do...

jRichardeau avatar Apr 29 '21 13:04 jRichardeau

Please be aware of the following issue that, due to unofficial adapter nature being tightly related to this PR, may be affecting this adapter.

https://github.com/wojtekmaj/enzyme-adapter-react-17/issues/12

wojtekmaj avatar Jun 04 '21 20:06 wojtekmaj

Am I reading this correctly? Tests are passing, but code coverage is a few percentage points lower than the target and that is holding this PR up?

createthis avatar Jul 27 '21 22:07 createthis

@createthis last time I ran it, tests weren't passing; you're right that they seem to be passing now - maybe React fixed some bugs in React 17. I'll rebase this PR shortly and see if that's still the case.

ljharb avatar Jul 27 '21 22:07 ljharb

I don't believe that's the case. I copied the entire test suite over to @wojtekmaj/enzyme-adapter-react-17, got

  1618 passing (3s)
  118 pending
  11 failing

on 17.0.0, 17.0.1, and now 17.0.2.

wojtekmaj avatar Jul 28 '21 12:07 wojtekmaj

@createthis last time I ran it, tests weren't passing; you're right that they seem to be passing now - maybe React fixed some bugs in React 17. I'll rebase this PR shortly and see if that's still the case.

@ljharb Sounds like a plan.

createthis avatar Aug 05 '21 18:08 createthis

@wojtekmaj @ljharb I'm trying to understand what is holding up this PR. I followed the CONTRIBUTING guide and performed the following:

git clone https://github.com/layershifter/enzyme.git
cd enzyme
git checkout feat/support-react17
npm install
npm install # running it twice reduces vulnerabilities sometimes
npm run react 17
npm run build
npm test

And received this result:

 1783 passing (3s)
  120 pending

What am I missing?

createthis avatar Aug 10 '21 14:08 createthis

What am I missing?

The maintainers probably want to enable and pass all the skipped tests from TODO_17(true).

eps1lon avatar Aug 10 '21 15:08 eps1lon