ag-grid Jest 测试 - this.btFirst.insertAdjacentElement 不是函数

Ome*_*r72 5 testing reactjs jestjs ag-grid

我尝试使用 Jest 测试 ag-grid 组件:

describe('DataGridInternal Component Tests', () => {
  it('includes class names on DataGrid', () => {
    const component = mount(<DataGridInternal {...SAMPLE_PROPS} />);
    expect(component.find('[data-test="ccfk-datagrid"]')).toHaveClassName('csf-material-grid');

  });
});
Run Code Online (Sandbox Code Playgroud)

我得到了例外:

TypeError: this.btFirst.insertAdjacentElement is not a function

来自(哪里

node_modules/ag-grid-community/dist/lib/rowModels/pagination/paginationComp.js:49

一旦我评论这一行,测试就通过了

知道什么是不正确的吗?

我正在使用:

"ag-grid-community": "21.2.2",       
"ag-grid-react": "21.2.2",
"enzyme": "3.10.0",
"enzyme-adapter-react-16": "1.14.0",
"enzyme-to-json": "3.3.5"
Run Code Online (Sandbox Code Playgroud)

小智 5

解决方案:

如果您使用 jest 作为测试运行程序,则需要通过安装 jest-environment-jsdom-fourteen 并将以下内容添加到 jest.config 来进行升级

testEnvironment: 'jest-environment-jsdom-fourteen'

如果您使用的是 create-react-app,

npm i -D jest-environment-jsdom-fourteen

然后在你的脚本中:

"test": "react-scripts test --env=jsdom-fourteen",

来源:1 2


小智 0

您能展示您如何准确地要求/导入 ag-grid 并定义您的 gridOptions 结构吗?我假设您正在测试现有的表或您自己在 HTML 中定义的表?