1

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!

Fody
  • 23,754
  • 3
  • 20
  • 37

1 Answers1

0

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);
            });
          });
        },
      });

    },
  }
})
Fody
  • 23,754
  • 3
  • 20
  • 37
  • Yes @Fody. I include the below on the cypress.config.js file. Your code is also tried. `return require('./cypress/plugins/index.js')(on, config)` Still I am getting an Error: `musicMetadata.parseFile is not a function` – Ramesh Moorthy Aug 17 '22 at 06:24
  • Well at least the error message has changed! I will see if it works here. By the way, I don't see any actual testing going on - just logging. What would you expect from the data? – Fody Aug 17 '22 at 06:29
  • Yes. you are right! I have to parse the mp3/mp4 file which has valid or not. `Ex: user can use info.txt file has renamed as info.mp3 and tried to upload.` In this case, I have to validate. Now, I am getting an error when I called the `musicMetadata.parseFile` function. How should I include the file, I tried with import that is also showing error "Import" is not acceped. Is there anyother way for the fix. – Ramesh Moorthy Aug 17 '22 at 07:25
  • I've just been trying it - so far it's crash and burn. There is definitely no `.parseFile()` but it looks like `.parseBlob()` should work (according to examples) - except another error comes up. Will try again shortly. – Fody Aug 17 '22 at 07:28
  • Thank you for the support! But I am getting an error "musicMetadata" is not a function when I called the `.parseFile()` or `.parseBlob()` – Ramesh Moorthy Aug 17 '22 at 07:49
  • I'm getting `File is undefined` with `.parseBlob()`, and looking at the package it is trying to handle the file path. Can't quite figure out the issues with that yet. – Fody Aug 17 '22 at 07:52