Testing: Mocks 2
It's nice to have tools that lets you do what you want. In this case, I read about how Jest create mocks and was wondering if Mocha had something similar. Nope, doesn’t have. So sad.
BUT! Good thing is good, webpack keeps surprising me and there is a plugin (inject-loader, https://www.npmjs.com/package/inject-loader) that lets you inject mocks into your components. This way you don’t have to modify your codebase (you should never modify your code for the only purpose of testing).
Here is an example:
This is the dependent class
This is the test class:
As you can see, I needed to import the Inyector class, tell him what class needs to wrap and --as an option-- it can be specified what dependencies will be mocked. In this case, I just wanted mock ‘components/Monster’.
Last thing, I had to access the Inyector’s `default` prop (who is the mocked class). IDK why you need to do that, but is how the Inyector function returns the data.
Once I ran my tests, it was printed `hello ^o^/` in the console! Wohoo! Now I have mocked classes inyected into your unit tests.
See you next time!











