While testing an Angular Application with Mocha in the PhantomJS Browser, I got the following Error Message:
TypeError: '[object BlobConstructor]' is not a constructor (evaluating 'new Blob([func],...
The problem seemed to be that PhantomJS could not find the Constructor for Blobs.
The main problem here is that Blobs are not supported in PhantomJS!
PhantomJS has a good reason to do this. The Implementation varies greatly from browser to browser. So, you could not be sure if your code works on all browsers, even if it tests correctly in PhantomJS.
But if you want to test code, where the Blob generation is not the main feature (but only some kind of by-product), it CAN make sense to add Blob support to PhantomJS. This way you can still test all the other features of this code block. Here is what you have to do:
- Get this Blob implementation from GitHub
- Add the blob.js file, to the imports of the file you want to test - If you are testing code for your index.html you are going to want to add the blob.js as a script tag in the head of the html.
- Run your tests again
You can actually leave the blob.js import in your production code, if you are lazy. The script performs a check if the current browser comes with an implementation of Blobs, before redefining it.