question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing singletons

See original GitHub issue

I’m writing tests against a class marked @singleton(). In my beforeEach I resolve the class, but since it’s a singleton I get the same instance for each test. Ideally each test would be independent of the others. I tried calling container.reset() but that wipes out all of the registered types. Is there room in this library for a “soft” reset, or is there another approach I could take?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

3reactions
fabiopignacommented, Apr 9, 2019

Hello, I’ve the same problem. There is a way to reset only the instances of registered singletons?

2reactions
skiptirengucommented, Nov 13, 2019

Just a heads up. You can do this cleanly with the new ContainerScoped lifecycle introduced on version 4. ContainerScoped behaves pretty much like a singleton, except the cached instance is reset for every new child container created.

import { Lifecycle, scoped, container as globalContainer, DependencyContainer } from 'tsyringe'

@scoped(Lifecycle.ContainerScoped)
class MyService {
  // ...
}

describe('My test', () => {
  let container: DependencyContainer

  beforeEach(function () => {
    container = globalContainer.createChildContainer()
  })

  it('New MyService instance', () => {
    const instance = container.resolve(MyService)
    // ...
  })
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit testing with singletons - Stack Overflow
To run code containing singletons in a test harness, we have to relax the singleton property. Here's how we do it. The first...
Read more >
Don´t use Singleton Pattern in your unit tests
Singleton classes do not allow for Test Driven Development (TDD) The laws of TDD. Unit testing depends on tests being independent of one...
Read more >
Unit Testing a Singleton in C++
The difficulty that we encounter when attempting to test a Singleton, is that quite often they are created as global variables at startup....
Read more >
Avoid Singletons to Write Testable Code - CodeAhoy
A look at the singleton design pattern in Java and why they make unit testing difficult with examples. Also explores alternate strategy.
Read more >
Singletons The Safe Way - Matt Carroll
That's the Singleton pattern. The Danger of Singletons. Can something as simple as a Singleton pose a threat to code health? Absolutely. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found