DEBUG_PRINT_LIMIT
环境变量的值(默认值为7000)。DEBUG_PRINT_LIMIT=100000 yarn test
来源:https://github.com/testing-library/dom-testing-library/blob/2653fd934f33ce19377c98029efe3e983a1c602b/src/pretty-dom.js#L50
我正在使用这个版本:"@testing-library/react": "^11.0.4"
我们可以像下面这样做,将300000更改为输出的最大长度。
debug(undefined, 300000)
debug(undefined, 300000)
- J. Munsonundefined
? - byxorundefined
。 - J. MunsonAnother option
screen.debug(undefined, Infinity);
debug()
函数的第二个参数可用于设置maxLengthToPrint
。screen
方法打印myElement
中的所有内容:import {render, screen} from '@testing-library/react'
render(<MyComponent />);
const myElement = await screen.findByText(/someText/i);
const maxLengthToPrint = 100000
screen.debug(myElement, maxLengthToPrint);
查看:
你可以使用 Jest Preview (https://github.com/nvh95/jest-preview) 来在浏览器中查看应用程序的用户界面,而不是在终端中查看 HTML,只需两行代码即可:
import { debug } from 'jest-preview';
describe('App', () => {
it('should work as expected', () => {
render(<App />);
debug();
});
});
它与 jest
和 react-testing-library
配合得非常好。
jest-preview
服务器才能使其正常工作。 - undefined由于DOM的大小可能非常大,您可以通过环境变量DEBUG_PRINT_LIMIT设置要打印的DOM内容限制。默认值为7000。当DOM内容被剥离(因为您设置的长度或由于默认大小限制)时,您将在控制台上看到...。以下是在运行测试时如何增加此限制的方法:
DEBUG_PRINT_LIMIT=10000 npm test
文档中有更多关于调试的信息。
DEBUG_PRINT_LIMIT=10000 pnpm test "文件路径"
。 - Purple_Kitten这对我有用:
debug(undefined, 300000);
import {render, screen} from '@testing-library/react'
render(<MyComponent />);
您可以阅读更多有关协助您打印结果的方法,包括美化生成的标记,详见:
screen
以使其按照我的需要工作:screen.debug(undefined, 300000);
- Purple_Kitten这对我有用
const history = createMemoryHistory()
const { debug } = renderWithRedux(
<Router history={history}>
<SideBar />
</Router>
, state);
screen.debug(debug(), 20000);
默认情况下,RTL不显示注释、<script />
和<style />
标签。在我的情况下,我需要测试DOM中的已注释节点。
如果您希望测试包括所有节点,可以像这样使用prettyDOM:
// render DOM with a commented node
const html = {__html: '<!--this is a commented DOM element-->'};
const element = <div dangerouslySetInnerHTML={html} />;
const { container } = render(element);
// This is what tells RLT to print all nodes!
const prettyfiedDOM = prettyDOM(container, undefined, { filterNode: () => true}) || '';
expect(prettyfiedDOM.includes('<!--this is a commented DOM element-->')).toBeTruthy();
filterNode
函数始终返回true
,这告诉RTL打印所有DOM节点,因此您可以测试注释、样式、标签等。您可以在prettyDOM源代码上阅读更多信息,并配置所需的DOM过滤行为。
希望能有所帮助!
DEBUG_PRINT_LIMIT
吗? - uday