Skip to content

Mixing custom render with all other exports gives error #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
robertvansteen opened this issue Sep 7, 2018 · 10 comments
Closed

Mixing custom render with all other exports gives error #169

robertvansteen opened this issue Sep 7, 2018 · 10 comments

Comments

@robertvansteen
Copy link

Using the custom utils file as described in the README where you re-export everything from react-testing-library and override the render method gives the following error:

TypeError: Cannot set property render of [object Object] which has only a getter

  • react-testing-library version: 5.0.1
  • react version: 16.5.0
  • node version: 10.0.0
  • npm (or yarn) version: 1.6.0

Relevant code or config:

// @flow
import * as React from 'react';
import { render } from 'react-testing-library';

const customRender = (node, ...options) => {
  return render(
    <div>{node}</div>,
    ...options,
  );
};

export * from 'react-testing-library';

export { customRender as render };

This is on create-react-app by the way (react-scripts@1.1.5)

@kentcdodds
Copy link
Member

@gnapse
Copy link
Member

gnapse commented Sep 7, 2018

Hmmm... It seems to be working fine for me here: https://github.com/kentcdodds/jest-cypress-react-babel-webpack/blob/master/test/calculator-test-utils.js

Hmm, but you're not doing exactly the same, so maybe your way is the answer.

You're renaming the lib render as rtlRender at the moment of importing. @rovansteen OTOH is attempting to rename customRender as render at the moment of exporting. Maybe that has something to do with the issue?

@alexkrolick
Copy link
Collaborator

Does this work instead? #170

@chancesmith
Copy link

chancesmith commented Sep 17, 2018

As a temporary fix, similar to the README example, instead of overwriting render I'm exporting customRender() from my test-utils.js and using it like so:

import {customRender} from '../test-utils';

test('<Button/> opens modal', () => {
  const {queryByText} = customRender(
    <Button/>
  )
  // rest of the test...
}

@kentcdodds
Copy link
Member

I'm guessing the issue is something to do with versions of babel or something. In any case I don't see anything actionable here. If someone discovers what's going on feel free to open a PR to improve the docs.

@alexkrolick
Copy link
Collaborator

I think it has to do with Babel 7 vs. 6. https://github.com/kentcdodds/jest-cypress-react-babel-webpack/blob/master/package.json is using v7. I'm getting the error on v6.

@alexkrolick
Copy link
Collaborator

https://github.com/babel/babel-upgrade try running this to upgrade to 7. Not quite working on my project but on a simpler config it should confirm whether or not this is the issue.

alexkrolick added a commit to alexkrolick/react-testing-library that referenced this issue Sep 18, 2018
kentcdodds pushed a commit that referenced this issue Sep 18, 2018
<!--
Thanks for your interest in the project. Bugs filed and PRs submitted are appreciated!

Please make sure that you are familiar with and follow the Code of Conduct for
this project (found in the CODE_OF_CONDUCT.md file).

Also, please make sure you're familiar with and follow the instructions in the
contributing guidelines (found in the CONTRIBUTING.md file).

If you're new to contributing to open source projects, you might find this free
video course helpful: http://kcd.im/pull-request

Please fill out the information below to expedite the review and (hopefully)
merge of your pull request!
-->

<!-- What changes are being made? (What feature/bug is being fixed here?) -->

**What**:

<!-- Why are these changes necessary? -->

**Why**:

<!-- How were these changes implemented? -->

**How**:

<!-- Have you done all of these things?  -->

**Checklist**:

<!-- add "N/A" to the end of each line that's irrelevant to your changes -->

<!-- to check an item, place an "x" in the box like so: "- [x] Documentation" -->

- [ ] Documentation
- [ ] Tests
- [ ] Ready to be merged
      <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
- [ ] Added myself to contributors table
      <!-- this is optional, see the contributing guidelines for instructions -->

<!-- feel free to add additional comments -->
julienw pushed a commit to julienw/react-testing-library that referenced this issue Dec 20, 2018
…brary#169)

* feat(queryByCurrentValue): add get/query by current value

* feat(queryByCurrentValue): exports (query|get)AllByCurrentValue

* chore: change the order of exports

* fix: add implementation for `select` element at `queryAllByCurrentValue`

* chore: add tests for get/queryByCurrentValue

* chore: add test for getAllByCurrentValue to throw

* chore: rename xxxByCurrentValue -> xxxByDisplayValue

* docs: add `getByDisplayValue`

* docs: add `eunjae-lee` as a contributor

* docs: remove `getByAltText`, `getByTitle` and `getByValue`

* docs: update `getByDisplayValue`

* chore: add test for 100% coverage

* docs: remove unnecessary `console.log(...)`

* Revert "docs: remove `getByAltText`, `getByTitle` and `getByValue`"

This reverts commit c960865af18f2ecc81a6770806eef95cc7dd89f7.

* docs: remove `getBySelectText` and `getByValue`

* docs: update description on `getByDisplayName`
@radihuq
Copy link

radihuq commented Jun 4, 2020

For anyone using TypeScript and running into the error Error: SyntaxError: Cannot use import statement outside a module, I fixed the issue by changing the file test-utils.js to test-utils.tsx:

import React, { ReactElement } from 'react';
import { render, RenderOptions, RenderResult } from '@testing-library/react';

import { MockedProvider } from '@apollo/react-testing'; // Mock Apollo Client
import { ThemeContextProvider } from '../Theme'; // Themeing

type AllTheProvidersProps = {
    children?: ReactElement;
};

const AllTheProviders: React.ComponentType<AllTheProvidersProps> = ({ children }) => {
    return (
        <ThemeContextProvider>
            <MockedProvider>{children}</MockedProvider>
        </ThemeContextProvider>
    );
};

const customRender = (ui: ReactElement, options?: RenderOptions): RenderResult =>
    render(ui, { wrapper: AllTheProviders, ...options });

// re-export everything
export * from '@testing-library/react';

// override render method
export { customRender as render };

Ref: https://testing-library.com/docs/react-testing-library/setup

@nvh95
Copy link

nvh95 commented Jun 16, 2020

I just paste the workaround solution from https://github.com/testing-library/react-testing-library/pull/179/files here in case you are googling by the error message. The root cause is believed caused by Babel version < 7

// test-utils.js
const rtl = require('react-testing-library')
const customRender = (node, ...options) => {
  return rtl.render(<Something>{node}</Something>)
}
module.exports = {
   ...rtl,
   render: customRender,
}

@Abdelalim-dev
Copy link

@radihuq Thanks for pointing that out.
It ended up being the issue I was facing all along!

For future reference, the error in my case was the following:
render method has not been called

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants