# How to create new unit test

**URL:** <https://discourse.processing.org/t/how-to-create-new-unit-test/44219>\
**Category:** Development\
**Tags:** contributions\
**Created:** [April 2, 2024, 5:32pm UTC](https://discourse.processing.org/t/how-to-create-new-unit-test/44219 "2024-04-02T17:32:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Renatolo](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@Renatolo](https://discourse.processing.org/u/Renatolo)\
**Post date:** [April 2, 2024, 5:32pm UTC](https://discourse.processing.org/t/how-to-create-new-unit-test/44219/1 "2024-04-02T17:32:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [April 3, 2024, 6:08am UTC](https://discourse.processing.org/t/how-to-create-new-unit-test/44219/2 "2024-04-03T06:08:25Z")

</div>

Hi @Renatolo,

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

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![Renatolo](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@Renatolo](https://discourse.processing.org/u/Renatolo)\
**Post date:** [April 3, 2024, 3:10pm UTC](https://discourse.processing.org/t/how-to-create-new-unit-test/44219/3 "2024-04-03T15:10:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [April 4, 2024, 6:11am UTC](https://discourse.processing.org/t/how-to-create-new-unit-test/44219/4 "2024-04-04T06:11:52Z")

</div>

Hi @Renatolo,

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

Did you follow the description here:

> <https://github.com/processing/p5.js/blob/main/contributor_docs/unit_testing.md>

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

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![Renatolo](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@Renatolo](https://discourse.processing.org/u/Renatolo)\
**Post date:** [April 4, 2024, 8:54am UTC](https://discourse.processing.org/t/how-to-create-new-unit-test/44219/5 "2024-04-04T08:54:45Z")

</div>

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() {

```auto
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
