Skip to content

Stateless functional components have "this" defined #1683

@halkeye

Description

@halkeye

Describe the bug
Looks like stateless functional components work differently in browser vs enzyme.
I'm not sure if this is a enzyme, react, react-test-utils or babel issue.

To Reproduce

Run this jest test

const React = require('react');
const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');
const { shallow } = require('enzyme');

// React 16 Enzyme adapter
Enzyme.configure({ adapter: new Adapter() });

const ArrowComponent = () => {
  console.log(this.props);
  return <div />;
};

// shouldn't work either
function FuncComponent () {
  console.log(this.props);
  return <div />;
}

test('FuncComponent', () => {
  shallow(<FuncComponent abc="123" />);
});

test('ArrowComponent', () => {
  shallow(<ArrowComponent abc="123" />);
});

Expected behavior
Both tests should fail (not sure how to properly write that, maybe expect throws?) but only the arrow function has this as undefined.

Module Versions:

├── enzyme@3.3.0
└── enzyme-adapter-react-16@1.1.1
└── react@16.4.1

Node Verison: v8.11.3

LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.5.1804 (Core)
Release: 7.5.1804
Codename: Core

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions