How to implement the Music Metadata Or Music MetaData Browser npm plugin in Cypress V10?

297 Views Asked by At

I tried to implement the Node plugin in Cypress Version 10, But I couldn't done it.

https://www.npmjs.com/package/music-metadata-browser#fetchurl-function

  1. Installation is done: npm install --save-dev music-metadata-browser npm install --save-dev util

  2. Added the below lines in plugin/index.js

const musicMetadata = require('music-metadata-browser');
const util = require('util');

module.exports = (on, config) => {
  require('@cypress/code-coverage/task')(on, config);
  on('task', {
    validateAudioFormat(audioTrackUrl) {
      return new Promise((resolve, reject) => {
        musicMetadata.parseFile(audioTrackUrl, (err, data) => {
          if (err) {
            return reject(err);
          }
          return resolve(data);
        });
      });
    },

  });
};
  1. Added the below code in e2e/validateFile.cy.js
describe('Parsing File', () => {
  it('Validating Audio File', () => {
    const audioURL = 'cypress/fixtures/media/Patrikios.mp3';
    console.log('url: ' +  audioURL);
    cy.task('validateAudioFormat', audioURL).then(data => {
        const allData = Object.values(data);
        console.log('All data: ' + allData);
    });

/******
    cy.on('validateAudioFormat', (url) => {
      async () => {
        const metadata = await mm.fetchFromUrl(url);
        console.log('url: ' + url);
        console.log(util.inspect(metadata, { showHidden: false, depth: null }));
      };
    });
*****/
  });
});

Error:

CypressError: `cy.task('validateAudioFormat')` failed with the following error: 

The task 'validateAudioFormat' was not handled in the setupNodeEvents method. The following tasks are registered: resetCoverage, combineCoverage, coverageReport

Fix this in your setupNodeEvents method here: /opt/lampp/htdocs/project/cypress.config.js

Commented block error:

taskreadAudioFiles, cypress/fixtures/media/audios/valid/Beyond_Patrick_Patrikios.mp3
CypressError
cy.task('readAudioFiles') timed out after waiting 60000ms

Anyone can help on this scenario?

Thanks!

1

There are 1 best solutions below

6
On

Your project is a mixture of Cypress v9 config and Cypress v10 tests.

I presume you are on 10+ so the plugins now go in cypress.config.js

const { defineConfig } = require('cypress')
const musicMetadata = require('music-metadata-browser');
const util = require('util');

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      
      require('@cypress/code-coverage/task')(on, config);

      on('task', {
        validateAudioFormat(audioTrackUrl) {
          return new Promise((resolve, reject) => {
            musicMetadata.parseFile(audioTrackUrl, (err, data) => {
              if (err) {
                return reject(err);
              }
              return resolve(data);
            });
          });
        },
      });

    },
  }
})