Unit testing possible?

Is there a way to unit test plugins ?
I tried Jest, but I get errors like these:

  ● Test suite failed to run

    Cannot find module 'viewport' from 'main.js'

    However, Jest was able to find:
        './main.js'
        './main.test.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

      4 | 
    > 6 | const viewport = require("viewport");

Is there a way to import xd modules like viewport, scenegraph, application etc ?

1 Like

Hi @aldobsom,

per se, nothing should stop you from writing unit tests for your plugins. You will, however, have to write mocks for the modules you’re using (or at least the parts of the module you’re using). Without mocks, it would – after all – also be more of an E2E test :wink:

For an example of how this can be achieved (with Jest), you could take a look at my open-source libraries (which have unit tests in place), paticularly the xd-storage-helper test which mocks some storage aspects of the uxp module, and the xd-localization-helper test (with an a bit more sophisticated mock, again, of the uxp module).

I hope this helps,
Happy coding (or – in this case – testing :slightly_smiling_face:),
Pablo

2 Likes

Thank you very much Pablo!

1 Like