How to create new unit test

Hey, I’m new to contributing for P5, I was trying to add a simple unit test for an event. But it seems that when i run the command npm test its not running that specific test. Any help?

Hi @Renatolo,

Maybe you forgot to provide your unit test to:
test/unit/spec.js
?

Cheers
— mnse

Thank you for your answer! The test i’m creating is in the folder test/unit/events/mouse.js so i thought i didn’t need to add it to spec.js
I also tried running the command npm run build before npm test but i think it still didn’t run the test…
Any ideas? Also, is there a way tu run only that test?

Hi @Renatolo,

Hard to say what’s wrong without seeing what you’ve done…

Did you follow the description here:

you can run this and check on the console what tests are running…
npm test -- --verbose

Cheers
— mnse

Yes, i think i followed every step in the guide.
After running the command npm test -- --verbose i saw that the test file i wanted to run was indeed being tested “test/unit/events/mouse.js” as this path was under the eslint:test task
However in the output of the tests i see the following:

mouseReleased documentation

  ✅ example #1 works

  ✅ example #2 works

  ✅ example #3 works

I assumed example #2 would be my test (because of the order in the file), but after deleting it from the file and re running npm test i still saw the same output in the terminal… I also tried changing the values in other tests so that i could make them fail, but i would also see no difference in the output

To further detail my problem this is the test i have created:
suite(‘mouseReleased’, function() {

test('mouseReleased function must run when mouse is released', async function() {
      let count = 0;

      myp5.mouseReleased = function() {
        count += 1;
      };

      window.dispatchEvent(new MouseEvent('mouseup'));
      assert.deepEqual(count, 1);
    });

    // test('mouseButton should be "left" on left mouse button release', async function() {
    //   // both mouse buttons pressed
    //   window.dispatchEvent(new MouseEvent('mousedown', { button: 0 }));
    //   window.dispatchEvent(new MouseEvent('mousedown', { button: 2 }));
    //   window.dispatchEvent(new MouseEvent('mouseup'));
    //   assert.strictEqual(myp5.mouseButton, 'left');
    // });

    test('mouseReleased functions on multiple instances must run once', async function() {
      let sketchFn = function(sketch, resolve, reject) {
        let count = 0;

        sketch.mouseReleased = function() {
          count += 1;
        };

        sketch.finish = function() {
          resolve(count);
        };
      };
      let sketches = parallelSketches([sketchFn, sketchFn]); //create two sketches
      await sketches.setup; //wait for all sketches to setup
      window.dispatchEvent(new MouseEvent('mouseup'));
      sketches.end(); //resolve all sketches by calling their finish functions
      let counts = await sketches.result; //get array holding number of times mouseReleased was called. Rejected sketches also thrown here
      assert.deepEqual(counts, [1, 1]);
    });
  });

*mine is commented because i was trying to see if it would show one less line in the output