Using isTest in Production code


I’m trying to unit test some Meteor Methods that check

if (!this.userId)

I’ve been able to stub Meteor.userId successfully, but same fix doesn’t work for this.userId and I’m also running into issues with getting this to work for multiple unit tests.

I’ve spent a lot of time on this and realized I can simply just do

if (!Meteor.isTest && !this.userId)

Seems simple enough. However, I don’t know if it’s good practice to check environments in production code. What does the community think about this. I don’t think it’s something I’d introduce frequently. Thanks!

Personally, I wouldn’t put anything critical in that block. Why? Although the guide says here that Meteor.isTest will be set by the meteor test command, there is no guarantee that some other package (gone rogue, or by error) wouldn’t do the same.

I would check if isTest is true at most for logging verbosity or something like that, but I wouldn’t trust it with anything security related.

1 Like