ngrx / platform

Reactive State for Angular
https://ngrx.io
Other
8.04k stars 1.98k forks source link

provideMockStore initialState causes tests to randomly fail #4573

Open krechtern opened 5 days ago

krechtern commented 5 days ago

Which @ngrx/* package(s) are the source of the bug?

effects

Minimal reproduction of the bug/regression with instructions

Hey, we are currently facing the problem that in one of our effect test files multiple tests are randomly failing because a selector returns undefined. We have mocked the selector value with the initialState property in the provideMockStore config. We have triple checked if the mocked data is fine and it is. Still we are receiving randomly an undefined value.

In a different effect spec file, we have basically the same tests. The setup for both effects is nearly identical, listen on actions, get some data from the state (the same as above mostly) and fetch some data. But these tests never randomly failed, even though it uses the same selectors. Here we have used a mocking solution with provideMockStore selectors where we directly mock the used selectors.

Example of failing spec file:

provideMockActions(() => actions),
provideMockStore({
  initialState: {
    machines: { selectedMachine: createMockMachine({ machineId: '123' }) },
    },
  },
}),

Example of always succeeding spec file:

provideMockActions(() => actions),
provideMockStore({
  selectors: [
    { selector: selectSelectedMachineId, value: '123' },
  ],
}),

Relevant selectors

const selectMachinesFeature = createFeatureSelector<State>('machines');

export const selectSelectedMachine = createSelector(
  selectMachinesFeature,
  (machinesState): Machine | undefined => machinesState.selectedMachine
);

export const selectSelectedMachineId = createSelector(
  selectSelectedMachine,
  (selectedMachine): string | undefined => selectedMachine?.machineId
);

We don't use any overrideSelector on the selector causing the problems in any of the tests.

Expected behavior

We expect that if we use the initialState property and correctly mock the state, that the selectors will emit the expected values inside the tests.

Or is there any reason that initialState shouldn't be use in this case?

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)

Ngrx: 18.1.0 Angular: 18.1.4 Node: 20.15.1 Browser: Chrome OS: macOS 15.0.1

Other information

No response

I would be willing to submit a PR to fix this issue

timdeschryver commented 4 days ago

Please provide a reproduction in order for us to investigate this issue.