admin管理员组文章数量:1192918
I am learning React Testing Library (many years of TDD experience in other languages)
This documentation on the React Testing Library says that when getByText
fails, it "however it prints the state of your DOM under test":
/
However, this does not happen for me on the current RTL.
Instead, I get this:
● loads and displays greeting
TestingLibraryElementError: Unable to find an element with the text: the current weather is: overcast clouds. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
Ignored nodes: comments, <script />, <style />
<h1
data-testid="currentWeatherOutput"
/>
27 | const currentWeatherOutput = screen.getByTestId('currentWeatherOutput')
28 |
> 29 | getByText(currentWeatherOutput, "the current weather is: overcast clouds")
my package dependencies are
"dependencies": {
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"fetch": "^1.1.0",
"msw": "^0.38.1",
"node-fetch": "^3.2.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"web-vitals": "^2.1.2"
},
I have two more questions:
Can I drop into a debugger directly inside my test code? / mentions nothing about accessing a native JS debugger
Can I take a screenshot of the DOM (I guess no because this isn't actually headless?) and look at it?
I am learning React Testing Library (many years of TDD experience in other languages)
This documentation on the React Testing Library says that when getByText
fails, it "however it prints the state of your DOM under test":
https://testing-library.com/docs/dom-testing-library/api-debugging/
However, this does not happen for me on the current RTL.
Instead, I get this:
● loads and displays greeting
TestingLibraryElementError: Unable to find an element with the text: the current weather is: overcast clouds. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
Ignored nodes: comments, <script />, <style />
<h1
data-testid="currentWeatherOutput"
/>
27 | const currentWeatherOutput = screen.getByTestId('currentWeatherOutput')
28 |
> 29 | getByText(currentWeatherOutput, "the current weather is: overcast clouds")
my package dependencies are
"dependencies": {
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"fetch": "^1.1.0",
"msw": "^0.38.1",
"node-fetch": "^3.2.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"web-vitals": "^2.1.2"
},
I have two more questions:
Can I drop into a debugger directly inside my test code? https://testing-library.com/docs/dom-testing-library/api-debugging/ mentions nothing about accessing a native JS debugger
Can I take a screenshot of the DOM (I guess no because this isn't actually headless?) and look at it?
2 Answers
Reset to default 20Can I take a screenshot of the DOM (I guess no because this isn't actually headless?) and look at it?
Yes. You absolutely can. Check out Jest Preview (https://github.com/nvh95/jest-preview)
Jest Preview gives you the ability to "see" your app UI in a browser, instead of HTML in the terminal, just by 2 lines of code:
import { debug } from 'jest-preview';
describe('App', () => {
it('should work as expected', () => {
render(<App />);
debug();
});
});
- Introduction: https://www.jest-preview.com/docs/getting-started/intro
- Installation guide: https://www.jest-preview.com/docs/getting-started/installation
You can use screen.debug() to debug your document or elements https://testing-library.com/docs/queries/about/#screendebug
本文标签: javascriptReact Testing LibraryHow to see current state of the DOM when testingStack Overflow
版权声明:本文标题:javascript - React Testing Library - How to see current state of the DOM when testing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738384019a2084093.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<h1 data-testid="currentWeatherOutput" />
. By Screenshot, do you mean literally print screen?. Or are you looking more for snapshots? Also would help us if you show your test case file. – Tony Commented Feb 22, 2022 at 15:28screen
from the base testing library functions but don't see how you're rending any components. – Tony Commented Feb 24, 2022 at 15:52