{"id":15415868,"url":"https://github.com/davidje13/react-mock-element","last_synced_at":"2026-05-01T15:38:24.134Z","repository":{"id":57334713,"uuid":"187918654","full_name":"davidje13/react-mock-element","owner":"davidje13","description":"mock element for capturing props in tests","archived":false,"fork":false,"pushed_at":"2020-11-22T16:09:13.000Z","size":76,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-08T14:02:52.337Z","etag":null,"topics":["mock","react","react-testing-library"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davidje13.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-21T21:52:37.000Z","updated_at":"2020-12-18T11:10:31.000Z","dependencies_parsed_at":"2022-09-11T01:51:23.312Z","dependency_job_id":null,"html_url":"https://github.com/davidje13/react-mock-element","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidje13%2Freact-mock-element","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidje13%2Freact-mock-element/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidje13%2Freact-mock-element/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidje13%2Freact-mock-element/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidje13","download_url":"https://codeload.github.com/davidje13/react-mock-element/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245965566,"owners_count":20701695,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mock","react","react-testing-library"],"created_at":"2024-10-01T17:10:03.465Z","updated_at":"2026-05-01T15:38:24.094Z","avatar_url":"https://github.com/davidje13.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# React mockElement\n\nMock element for capturing props in tests. Provides a middle-ground\ntowards enzyme's `shallow` unit tests when using\n[react-testing-library](https://github.com/testing-library/react-testing-library)\n(but can be used with any testing library which renders to a DOM).\n\nThe intended use is to mock out specific children of a component under\ntest which have important properties or are otherwise difficult to\nmock. This should not be used to mock every child component.\n\n## Install dependency\n\n```bash\nnpm install --save-dev react-mock-element\n```\n\n## Usage with react-testing-library + Jest\n\n```javascript\n// code under test (MyComponent.jsx)\n\nimport React from 'react';\n\nexport default ({Child}) =\u003e {\n  return (\n    \u003cdiv\u003e\n      foobar\n      \u003cChild p1=\"a\" /\u003e\n    \u003c/div\u003e\n  );\n};\n\n\n// test (MyComponent.test.jsx)\n\nimport React from 'react';\nimport {render, cleanup} from 'react-testing-library';\nimport mockElement from 'react-mock-element';\nimport MyComponent from './MyComponent';\n\ndescribe('MyComponent', () =\u003e {\n  it('creates a child element with a property', () =\u003e {\n    const MyMockElement = mockElement('some-tag-name');\n\n    const {container} = render(\u003cMyComponent Child={MyMockElement} /\u003e);\n\n    const child = container.querySelector('some-tag-name');\n    expect(child.mockProps.p1).toEqual('a');\n  });\n});\n\nafterEach(cleanup);\n```\n\nThis is also compatible with Jest module mocking:\n\n```javascript\n// code under test (MyComponent.jsx)\n\nimport React from 'react';\nimport ChildComponent from './ChildComponent';\n\nexport default () =\u003e {\n  return (\n    \u003cdiv\u003e\n      foobar\n      \u003cChildComponent p1=\"a\" /\u003e\n    \u003c/div\u003e\n  );\n};\n\n\n// test (MyComponent.test.jsx)\n\nimport React from 'react';\nimport {render, cleanup} from 'react-testing-library';\nimport mockElement from 'react-mock-element';\nimport MyComponent from './MyComponent';\n\njest.mock('./ChildComponent', () =\u003e mockElement('mock-child'));\n\ndescribe('MyComponent', () =\u003e {\n  it('creates a ChildComponent element with a property', () =\u003e {\n    const {container} = render(\u003cMyComponent /\u003e);\n\n    const child = container.querySelector('mock-child');\n    expect(child.mockProps.p1).toEqual('a');\n  });\n});\n\nafterEach(cleanup);\n```\n\n## API\n\n`mockElement(tagName)`: returns a React component which renders a tag\nwith the given tag name (a naming convention such as `mock-something`\nis recommended but not required). Note that as this function name\nbegins with `mock`, it can be used by hoisted `jest.mock` calls in\ntests (see second demo above).\n\n`element.mockProps`: added to the `HTMLElement` prototype. This\nprovides access to the props passed to an element. This is read-only.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidje13%2Freact-mock-element","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidje13%2Freact-mock-element","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidje13%2Freact-mock-element/lists"}