How can I require core NodeBB modules inside my test files, supposed to be executed using mocha/jest binaries?
-
I have to write unit tests for our custom plugins, and the tests files will be run with jest/mocha binaries. The problem is that I cannot
require
NodeBB core modules inside my test files to mock/stub them. I have tried bothrequire.main.require
andmodule.parent.require
, and get the "Cannot find module" error. Is there a way to do so? -
-
@Muhammad-Saqib not currently possible to run tests using NodeBB modules. The way this has to be done is having the NodeBB test run the tests of your plugin.
It's currently not supported, though. Please open an issue on GitHub requesting this feature.
-
@PitaJ said in How can I require core NodeBB modules inside my test files, supposed to be executed using mocha/jest binaries?:
The way this has to be done is having the NodeBB test run the tests of your plugin.
This doesn't work either if your plugin/theme is having
module.parent.require
calls inside them. Any other solution? -
@Muhammad-Saqib it will work if you change them to use
require.main.require
. NodeBB should warn if your plugin usesmodule.parent.require
because that method is deprecated.Your plugin will need to be activated for the routes and sockets and stuff to work, though.
-
@PitaJ said in How can I require core NodeBB modules inside my test files, supposed to be executed using mocha/jest binaries?:
Your plugin will need to be activated for the routes and sockets and stuff to work, though.
@PitaJ Actually I have a custom theme, which has extended the core router and there are a few endpoints which I want to write tests for. What I am not being able to figure out is that how I can load my custom theme when I run
npm run test
in NodeBB directory so that I could run tests for my custom endpoints. Please note that my theme is child ofnodebb-theme-persona
, can you please help me with this? Thanks -
@Muhammad-Saqib said in How can I require core NodeBB modules inside my test files, supposed to be executed using mocha/jest binaries?:
@PitaJ Actually I have a custom theme, which has extended the core router and there are a few endpoints which I want to write tests for. What I am not being able to figure out is that how I can load my custom theme when I run npm run test in NodeBB directory so that I could run tests for my custom endpoints. Please note that my theme is child of nodebb-theme-persona, can you please help me with this? Thanks
Anybody who can respond? @julian
-
@Muhammad-Saqib it's usually customary to wait more than a day before bumping a thread.
The process is too difficult to really describe here, it might be easier to just implement a feature where tests can be run than explain how to do it yourself.
There's an issue for this as well:
-
@PitaJ said in How can I require core NodeBB modules inside my test files, supposed to be executed using mocha/jest binaries?:
The process is too difficult to really describe here, it might be easier to just implement a feature where tests can be run than explain how to do it yourself.
So I will have to create a PR to NodeBB core for this feature? There seems not to be another way?
-
@Muhammad-Saqib I wasn't suggesting that you specifically implement it, just that you should wait until someone does. If you can implement it, then please do!