DataTables 3.x.x CommonJS import fails with jest/jsdom
DataTables 3.x.x CommonJS import fails with jest/jsdom
sebastianbarth
Posts: 74Questions: 16Answers: 0
Hi Allan,
I’m testing a component that uses DataTables with Jest and jsdom. Loading datatables.net-dt fails before creating a table.
I could reproduce this isolated with DataTables 3.1.4.
In an empty directory:
npm init -ynpm install --save-dev jest@30 jest-environment-jsdom@30 datatables.net-dt@3.1.4- Create dt.test.js:
test('loads DataTables', () => {
const DataTable = require('datatables.net-dt');
expect(DataTable).toBeDefined();
});
npx jest --env=jsdom --runInBand
This fails with:
TypeError: Cannot set properties of undefined (setting 'api')
The styling wrapper appears to call:
require('datatables.net')(window);
However, when a global window exists, the core already exports the DataTable constructor. Calling it this way reaches this.api with this undefined.
Could the CommonJS wrapper use the exported constructor directly when window exists, and invoke the factory only when no global window is available, .. or similar?
Resolving the ESM entry points works around the issue, but it would be helpful for the CommonJS import to work too.
Thanks for checking!
Best Regards,
Sebastian
Answers
jest.config.js - How we (hopefully only temporarily) work around this issue:
Have an awesome weekend!
Sebastian
Hi Sebastian,
Many thanks for flagging this up and the extra details. I'll look into it next week and get back to you.
Allan
I've committed this change to my package builder to address this issue. It will propagate to the extensions as well, although they will each need a release in order to get that update. I'm not currently planning to do an immediate release on a number of the extensions (just those which have other patches also in the queue), but they will get updated as I roll through them.
The nightly now has the change (and the style file) for the core software if you want to try it out.
Allan