DEV Community

Discussion on: Don't use fixtures in Cypress and unit tests- use factories

Collapse
 
dgreene1 profile image
Dan Greene

My apologies for not seeing this question sooner. So I would first ask thy you would need different data for mocked tests. Generally mocked tests are designed to test the "shape" of your data, not necessarily real data. As where end-to-end (e2e) tests would rely on both the shape and the content of the data and would therefore not utilize mock data (or factories) at all.

But if you still choose to mock different data per environment, I would consider simply clarifying that in your tests.

let user: IUser | null = null;
if(process.env.NODE_ENV === 'production'){
  user = makeFakeUser({userName: 'John'});
} else if(process.env.NODE_ENV === 'staging'){
  user = makeFakeUser({userName: 'Susan'});
}
// Assert so that the user variable can no longer be seen as null
if(!user){
  throw new Error(`unable to initialize mock user since NODE_ENV was invalid`);
}
// the rest of your test
Enter fullscreen mode Exit fullscreen mode