Skip to content

Testcafe filter function

I have a filter function for Selector as following which accepts two parameters { subject: subject, from: from } as a dependencies object.

But after running this function, I am getting an error

ReferenceError: subject is not defined

async function getMessage(subject, from) {
  return await Selector('[data-test=messageListItem]').filter(( message ) => {
    return message.querySelector('[data-test=subject]').textContent.includes(subject) &&
  }, { dependencies: { subject: subject,  from: from } });

Can TestCafe team please help me with this?


In the case of the .filter method you need to rewrite your dependencies parameter ({ dependencies: { subject: subject, from: from } }) as follows:

{ subject: subject,  from: from }

I have prepared a sample test to illustrate it:

import { Selector } from 'testcafe';

fixture `New Fixture`
    .page ``;

test('New Test', async t => {
    await t
        .typeText(Selector('#tsf').find('[name="q"]'), 'testcafe')

    await t.expect(Selector('.LC20lb').count).eql(10);

    function fn (title) {
        return Selector('.LC20lb').filter((node, idx) => {
            return node.textContent.includes(title);
        }, { title }); // dependencies parameter

    await t.expect(fn('TestCafe').count).gt(1);