Home » Javascript » TypeError: “listener” argument must be a function. Using npm pixelmatch in node JS

TypeError: “listener” argument must be a function. Using npm pixelmatch in node JS

Posted by: admin October 29, 2018 Leave a comment

Questions:

I am using nodeJS with mocha and chai and importantly pixelmatch.

I have taken screenshots and backed them up.
I want to compare them and produce a diff folder with differences using pixelmatch.
I keep getting an error.

"TypeError: "listener" argument must be a function"

The error refers to .66:

 img1 = fs.createReadStream(screenshot_path).pipe(new PNG()).on('parsed', doneReading())

Here is my compare screenshot code:

const compareScreenshots = async (path) => {
     const diff_path = "./build/diff/" + path + ".diff.png"
     const screenshot_path = "./build/" + path + ".png"
     const backup_path = "./backup/" + path +".png"
    let img1, img2, filesRead = 0;

let doneReading = async function(){
    if(++filesRead < 2) return;

    var diff = new PNG({width: img1.width, height: img2.height});

    var numDiffPixels = pixelmatch(img1.data, img2.data, diff.data, img1.width, img1.height, {threshold: 0.1})

    diff.pack().pipe(fs.createWriteStream(diff_path));

    expect(numDiffPixels).to.equal(0) }

 img1 = fs.createReadStream(screenshot_path).pipe(new PNG()).on('parsed', doneReading())
 img2 = fs.createReadStream(backup_path).pipe(new PNG()).on('parsed', doneReading())
}

the input ‘path’ is:

let page_title = await page.$eval('title', page => page.textContent.replace(/ /g,''))
Answers: