Skip to content

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

Merged
tiny-ben-tran merged 1 commit intomainfrom
feature/INT-3427
May 8, 2026
Merged

INT-3427: make fixtureHook use beforeEach instead of before#465
tiny-ben-tran merged 1 commit intomainfrom
feature/INT-3427

Conversation

@michalnieruchalski-tiugo
Copy link
Copy Markdown
Contributor

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 requested a review from a team as a code owner May 7, 2026 10:44
@michalnieruchalski-tiugo michalnieruchalski-tiugo requested review from ltrouton and spocke and removed request for a team May 7, 2026 10:44
@tiny-ben-tran tiny-ben-tran requested review from a team, kimwoodfield and metricjs and removed request for a team May 8, 2026 04:23
@tiny-ben-tran tiny-ben-tran merged commit 4448762 into main May 8, 2026
5 checks passed
@tiny-ben-tran tiny-ben-tran deleted the feature/INT-3427 branch May 8, 2026 04:59
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.

3 participants