This repository was archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
This repository was archived by the owner on Mar 5, 2022. It is now read-only.
Styled Components - Fails to mount #40
Copy link
Copy link
Closed
Labels
Description
Hey, first of all: great module! It's very elevating to combine unit tests into the E2E workflow.
Anyway; we use styled-components
heavily and I would like to unit test them in cypress. Unfortunately, mount
throws a fit when hit with a styled component.
After
yarn add -D styled-components
- (
yarn add -D cypress-react-unit-test
)
If you throw this test into cypress...
// type definitions for Cypress object "cy"
/// <reference types="cypress" />
// check this file using TypeScript if available
// @ts-check
// =============================================================================
// Debugging Styled Component in Cypress for Unit Testing
// yarn add -D cypress-react-unit-test
// yarn add -D styled-components
// =============================================================================
import React from "react";
import { mount } from "cypress-react-unit-test";
import styled from "styled-components";
/**
* Create normal react component
* */
const TestComponent = props => {
return <div data-test="TestComponent">This is a test!</div>;
};
/**
* 💄 Now we style with styled-components
* */
const StyledComponent = styled(TestComponent)`
background-color: green;
`;
/**
* Running Test
*/
describe("Can Render Component", () => {
it("Plain Component", () => {
// ✅ Works
mount(<TestComponent />);
cy.get('[data-test="TestComponent"]').should("contain", "test");
});
it("Styled Component as JSX", () => {
// 🔥 Fails
// Error: Element type is invalid: expected a string
// (for built-in components) or a class/function (for composite components)
// but got: object.
mount(<StyledComponent />);
cy.get('[data-test="TestComponent"]').should("contain", "test");
});
it("Styled Component as object reference", () => {
// 🔥 Fails
// Error: TypeError: Cannot read property 'name' of undefined
mount(StyledComponent);
cy.get('[data-test="TestComponent"]').should("contain", "test");
});
});
// =============================================================================
// END Debugging Component
// =============================================================================
I was trying to solve this with some style extraction etc. to see what's going on but the last error TypeError: Cannot read property 'name' of undefined
is really odd to me.
Someone have an idea?
Thanks in advance
feedm3 and ysdexlic