1

I am curious if there is a best practice (if any at all) with trying to unit test a feature test. For example, if I have a function called isStyleSupported that detects if a CSS property is supported in the current browser, how can I reliably unit test this function knowing the results would be different in different browsers.

The only thing I can think of is sniffing out the specific browser and then comparing the function's return value with what is expected for that browser. However, it seems like a bad approach given the fact that browser detection is bad practice.

I've briefly looked at Modernizr to see what they do without any definitive answer as it's a lot to look through.

Any ideas? Thanks.

Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
user141350
  • 95
  • 3
  • 5
  • Easy peasy, you generally just do `if ( style in element )` to check if a style is supported, if it's false, the browser doesn't support that style. There's no need to unit test that ? – adeneo Jan 16 '15 at 21:19
  • Ok fine, but that was just an example. How about a function that feature tests CSS at-rule support, such as: https://github.com/Modernizr/Modernizr/blob/master/src/atRule.js? Or a function that feature tests event support such as: https://github.com/Modernizr/Modernizr/blob/master/src/isEventSupported.js. Looking for more of a general concept to unit testing a feature testing function. – user141350 Jan 16 '15 at 21:24
  • i would argue that if the test is not deterministic, it's of little to no utility. it's like when people want to unit test picking a file, as though that's something that's going to break unexpectedly. refer to the serenity prayer for guidance on writing practical tests. – dandavis Jan 16 '15 at 21:24
  • What you likely want to do is manually return false for the feature in one test, and return true in another test, thus testing the implications of both possibilities downstream, which is what matters. – dandavis Jan 16 '15 at 21:28
  • Ok I see what your saying, that makes sense. Thanks – user141350 Jan 16 '15 at 21:31
  • So to be clear, I should create a test that calls the function using parameters I know should fail in every browser and then create another test that calls the function using parameters I know should pass in every browser? – user141350 Jan 16 '15 at 21:37

1 Answers1

0

Sounds to me like you are comparing unit testing to acceptance testing. One way to achieve this would be to use something like BrowserStack.

JonnyReeves
  • 6,119
  • 2
  • 26
  • 28