ReferenceError: Can't find variable: Map

This error is thrown because there's no Map in browsers. PhantomJS is used as Karma driver, and it doesn't support ES6 features.

If polyfills (e.g. core-js) aren't loaded in files that were included in tests, they should be loaded separately, for example via karma-es6-shim plugin:

    ...
    frameworks: ['es6-shim', 'jasmine'],
    ...

This is what has worked for me: I was using PhantonJs with karma and nightwatch for my testing . I was getting this error during JhantomJs initialization and as test were getting started because because there's no Map in browsers. PhantomJS is used as Karma driver, and it doesn't support ES6 features.

I tried to load polyfills but that did not help as well. Then I saw that support for PhantonJs has long gone and felt it wont work. So I took an effort to replace PhantomJs with Chrome headless browser and surprisingly it was pretty easy to replace.

Here are the thing I did to make the change from PhantomJs to Chrome headless:

  1. Package.json - puppeteer, chromedriver, karma-chrome-launcher as dependency
  2. Add the following in karma.conf.js
const puppeteer = require('puppeteer');
process.env.CHROME_BIN = puppeteer.executablePath();

module.exports = function(config) {
  config.set({ 
    .
    browsers: ['ChromeHeadless'],
    .
   });

  1. Add/Edit the following in nightwatch.json
    "chrome": {
      "desiredCapabilities": {
        "browserName": "chrome",
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "chromeOptions": {
          "args": ["--no-sandbox", "headless"]
        }
      }

This is something which has worked for me; may or may not work for you depending on the kind of side things you are using with JhantomJs for testing. Nevertheless should helpful for anyone who is trying to move away PhantomJs.