DEV Community

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

Collapse
 
mareru profile image
Marijana Rukavina

Thanks Dan for writing this article. It sounds like a direction I would like to go to. I have one question though. How would you solve for Typescript the fact that cypress ui tests need to run on different environments? Meaning you would have to create/load test data objects with different values for different environments. I would appreciate if you could share your thoughts about it. Thanks 😊

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