1

I need some help with the following code:

const ffmpeg = require('fluent-ffmpeg'),
{PassThrough} = require('stream');

ffmpeg.setFfmpegPath('/opt/bin/ffmpeg');
ffmpeg.setFfprobePath('/opt/bin/ffprobe');

exports.wavCompilation = async (wavObjList, s3OutputFullPath) => {
    let command = ffmpeg();
    return new Promise(async (resolve, reject) => {

        const pt = new PassThrough()
        command
            .complexFilter(complexFilterList)
            .audioFrequency(44100)
            .audioChannels(2)
            .outputOptions('-map [audio]')
            .toFormat("wav")
            .output(pt, {end: true})
            .on('error', function (err, stdout, stderr) {
                console.log("stdout:\n" + stdout);
                console.log("stderr:\n" + stderr);
                if (err) {
                    console.log(err.message);
                    return reject("Error");
                }
            })
            .on('start', (commandLog) => printLog(commandLog))
            .on(`end`, () => console.info("end ffmpeg"))
            .run()

        resolve(***);
    });
}

Does anyone know how can I stub/mock/spy/fake "ffmpeg" ?

I use Sinon for unit tests and I'm trying to use proxyquire to mock "fluent-ffmpeg"

const sinon = require('sinon'),
proxyquire = require('proxyquire').noCallThru();

let ffmpegStub = {
    setFfmpegPath: () => {},
    setFfprobePath: () => {}
}

const ffmpegService = proxyquire('../services/ffmpeg-service/index', {
    'fluent-ffmpeg': ffmpegStub
});

for the methods setFfmpegPath and setFfprobePath it works but for the line:

let command = ffmpeg();

it says "ffmpeg is not a function"

I tried to add a default function such as:

let ffmpegStub = {
    default: () => {},
    setFfmpegPath: () => {},
    setFfprobePath: () => {}
}

but it also doesn't work and I have no idea now what I could do.

Please I need some fresh ideas, my goal is to catch the argument for "complexFilter" and assert it.

1 Answers1

0

No need of proxyquire.

I've got what I wanted with the following:

filterStub = sandbox.stub(ffmpeg.prototype, "complexFilter").returnsThis();
sandbox.stub(ffmpeg.prototype, "run").returnsThis();
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 22 '22 at 00:20