Idea to improve performance of unit tests #249
Replies: 6 comments 5 replies
-
This is a great idea. We do some caching now but it only serves to prevent duplicating a module during a test so the caching doesn't last beyond an individual test. I don't know of any first-class hooks from Angular that would allow for this but it might be possible to do the kind of caching you're describing using Proxy wrappers around modules and/or providers so once we traverse a module one time, we can re-use the mock for the next test to save ourselves another traversal. I may give this a look next week or so. If you have any ideas about where to add a hook so we can do on-demand mocking let me know. |
Beta Was this translation helpful? Give feedback.
-
@getsaf not to be a pain :), but did you ever get a chance to look into this? really hopeful this can be done! |
Beta Was this translation helpful? Give feedback.
-
so did a little more testing. i have a spec file whose module imports 30 other modules. i have 20 tests in the spec file. when i run it, i see that each of the 20 tests takes the same amount of time, 500ms. if i remove 15 of the imports (they're not needed for this component), each of the 20 tests takes the same time, 300ms. this suggests to me that the mocking of all the classes in the module is what making the tests slower. i'm not surprised by that - it's what i would expect, almost. the question i have around caching is, could the mocks be cached before the first test such that time to first test is slower, but then, if the mocks could be reused for the other 19 tests, those other 19 tests would be faster? or do you have to create new mocks every time for each of the 20 tests because they can be changed during the execution of a given test, and therefore you have no choice? |
Beta Was this translation helpful? Give feedback.
-
and also thinking, do you create a tree of what a component is using before mocking? if so, could you only mock what the component is using? lots of ifs and i'm sure there are complexities, just thinking aloud really. |
Beta Was this translation helpful? Give feedback.
-
so here is a test run with extra modules that aren't needed by the particular component... note that all runs take approximately the same time. |
Beta Was this translation helpful? Give feedback.
-
and with my suggestions above, in the scenario above with extra modules, i would hope to see the first test take longer, but then each test after take more around the 8-11ms time that we see when those extra modules aren't mocked. just some way to cache mocked modules or eliminate the time needed to mock if the module isn't needed.... that all make sense? |
Beta Was this translation helpful? Give feedback.
-
Brandon, right now, you mock everything the module depends on before running a test, correct? Do you think it would be possible to only mock dependencies as angular attempts to inject them? Is there some hook that would enable this (or hack :))? I bet we're not alone in having modules that have lots of imports for all the different components, while each component might only use a fraction of them? Or, in the case where a ui module is imported, it might be that only a few of all the possible ui components are used?
Having this would significantly increase the speed of unit tests. Thoughts/
Beta Was this translation helpful? Give feedback.
All reactions