In my testcafe tests, I need to get the constructor function for a library I am using in order to call a static method on it.
However, I am unable to do this using the given ClientFunction and Eval methods. How can I go about getting the constructor function?
I have tried the following:
// Does not work, because the docs say it only allows using ClientFunction for obtaining "serializable" values
let getSortable = new ClientFunction(() => window.Sortable);
test('test', async t => {
let Sortable = await getSortable();
console.log(Sortable); // Logs undefined
});
test('test', async t => {
let Sortable = await t.eval(() => window.Sortable);
console.log(Sortable); // Logs undefined (not sure why)
});
I need to get the constructor function for a library I am using in order to call a static method on it.
It's impossible to do that. You cannot call function from the browser's JavaScript environment in the Node.js environment in which tests are executed. To accomplish your scenario, call the target static method in the ClientFunction and return the result from it.
cosnt getStaticData = ClientFunction(() => {
const data = window.Sortable.staticMethod();
return JSON.serializable(data);
});