This is a bit of a weird issue that'll require some explaining. I use grunt-testem-mincer
for testing code in a browser. As the name implies, grunt-testem-mincer
uses grunt
, testem
, and mincer
and mincer
uses this library. I have a test
grunt task that runs my tests in node and then in a browser using testem
. I discovered that the tests in the browser all fail if I run them like this, but pass correctly if I run only the browser tests. Weird, right? So I used grunt-testem-mincer
s --debug
flag (or maybe it's part of grunt proper . . . not relevant here) to see what files it's trying to serve. It's super handy and leaves the mincer
server running so you can try to fetch assets. In my case the file, node_modules/should/should.js
was not returning the actual file, but instead the following text:
console.error("pattern.test is not a function")
After grepping my entire codebase (including modules) for pattern.test
and digging through them. I've identified the problem. The function match
in lib/hike/index.js
calls pattern.test
on what is supposed to be a regex returned by pattern_for
in the same file. pattern_for
checks for self.__patterns__[basename]
where basename
is the name of the file to server. In my failing case, that basename is simply should
. This is not a problem when I run the browser-based tests directly. The reason it fails when I run it in conjunction with node-based tests is that I'm requiring should
as part of my test setup and because mincer then starts in the same node instance, should
is already in the require cache. The side effects of requiring should
(forgive me if you know all this . . . I'm just being thorough) is that a should
property is added to the Object prototype to make testing easier. So when my list of files to serve gets to should
, pattern_for
encounters this check:
if (!self.__patterns__[basename]) {
which in this case is the same as
if (!self.__patterns__.should) {
and self.__patterns__.should
does exist. So then it returns self.__patterns__[basename]
or in my case the getter
added to Object by should
. Which makes the file not serve correctly and all my tests fail.
So I have a couple thoughts. 1) It's pretty well agreed upon that adding methods, properties, etc. to built-in prototypes is bad (obviously this is why). So you'd probably be within your rights to ignore it. However, this would also happen if someone wanted to serve a file called toString.js
or constructor.js
or anything that's already built-in. So it could cause issues even without should
's inclusion. 2) There are ways for me to work around it (like removing should
after my tests complete or copying the should.js
script to another name), but it seems like it shouldn't be that hard to address, as you could just expand your current check to something like:
if (!self.__patterns__[basename] || !(self.__patterns__[basename] instanceof RegExp)) {
Or however you prefer to check that it it's not a regex (lodash, for instance). It's a little more than this I guess because you'd also need to be sure not to overwrite the existing property, so if self.__patterns__[basename]
existed but wasn't a regex, maybe you need to preface it with, like __
or something. I don't know. That's getting into the weeds of your project a bit, and I don't know nearly enough about it to postulate.