Skip to content

INT-3427: make fixtureHook use beforeEach instead of before#464

Closed
michalnieruchalski-tiugo wants to merge 1 commit intomainfrom
INT-3427
Closed

INT-3427: make fixtureHook use beforeEach instead of before#464
michalnieruchalski-tiugo wants to merge 1 commit intomainfrom
INT-3427

Conversation

@michalnieruchalski-tiugo
Copy link
Copy Markdown
Contributor

@michalnieruchalski-tiugo michalnieruchalski-tiugo commented May 7, 2026

Problem: fixtureHook calls TestBed.configureTestingModule inside a before hook, which runs once per suite. However, InitTestEnvironment.ts initializes TestBed with destroyAfterEach: true, which resets the module after every test.

This means the first test in any suite works fine, but every subsequent test fails because:

  1. The first test completes and destroyAfterEach tears down the configured module
  2. The second test calls TestBed.createComponent(), but the module is no longer configured providers, imports, and compiled components are gone
  3. before doesn't re-run, so nothing reconfigures the module

Impact: Only the first test in suite can run successfully. The rest fail regardless of their content. ( Assuming that succesfull module definition is required for the test to run )

Fix: Change before to beforeEach in fixtureHook so that configureTestingModule runs before every test, matching the teardown lifecycle.

Example. In this example only the first test will succeed, and the second one will always fail, regardless of content of these tests. If we swap the order, only the first one will succeed. It happens because providers array is always cleaned afterEach test.

@Injectable()
class CounterService {
  public value = 0;
  public increment(): void {
    this.value++;
  }
}

@Component({
  imports: [ EditorComponent ],
  template: `<editor /> {{ counterService.value }}`,
})
class CounterComponent {
  public counterService = inject(CounterService);
}

describe('ExampleTest', () => {
  eachVersionContext([ '8' ], () => {
    const createFixture = editorHook(CounterComponent, { providers: [ CounterService ] });

    it('should initialize counter at zero', async () => {
      const fixture = await createFixture();
      const { counterService } = fixture.componentInstance;
      Assertions.assertEq('Counter should start at 0', 0, counterService.value);
    });

    it('should increment and decrement the counter', async () => {
      const fixture = await createFixture();
      const { counterService } = fixture.componentInstance;

      counterService.increment();
      counterService.increment();
      Assertions.assertEq('Counter should be 2 after two increments', 2, counterService.value);
    });
  });
});

@michalnieruchalski-tiugo michalnieruchalski-tiugo marked this pull request as ready for review May 7, 2026 10:40
@michalnieruchalski-tiugo michalnieruchalski-tiugo requested a review from a team as a code owner May 7, 2026 10:40
@michalnieruchalski-tiugo michalnieruchalski-tiugo requested review from TheSpyder, spocke and tiny-ben-tran and removed request for a team May 7, 2026 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant