0

Is there a way to run tests with components that use singletons like https://stackoverflow.com/a/59646297/8425354 isolated? I've noticed that some tests , that contains in same file, use the same context, so that tests could fail because one test affects other. E.g. I run one test , it changed something in singleton and other test expected to have something different in singleton return value. I don't understand deeply where this object(singleton)is contained, but in order to avoid such problems, I would like to run some tests isolated, so that each singleton should be unique for those tests. I would add comment, if it wasn't clear

fake364
  • 99
  • 8
  • 1
    "Each singleton should be unique" makes it less of a singleton. Depending on its implementation you might be able to use rewire, create a subclass that un-singletons it (very dependent on how its constructed), set the internal instance directly, etc. It it doesn't have a built-in `reset` mechanism or it disallows access to its single instance, your options are limited. – Dave Newton Apr 08 '22 at 19:42
  • Do you mean to set a new link to the singleton instance? Unique singleton - like when you close the browser and open it again, all global variables that had been set before would be cleared – fake364 Apr 08 '22 at 19:53
  • 1
    Right, but as you noted, when you load/import a module in a JS file, that's the equivalent of loading the browser page--and the "browser" doesn't close until that file is done. (And it's actually worse than that; once a module is loaded by node, it's generally never reloaded.) You can play *games* with `import`, e.g., https://stackoverflow.com/q/56492656/438992 or https://github.com/nodejs/help/issues/2751, but personally, I'd be more inclined to instrument the object w/ back-door reinitialization. Not sure how I'd feel about it, though. – Dave Newton Apr 08 '22 at 21:23

0 Answers0