title: How to get nightwatch test_workers work with browserstack-local
body: <p>I am trying to get Nightwatch's inbuilt parallel test_workers working with browserstack-local for local url testing. </p>
  
  <p>When running tests without browserstack local, Nightwatch test_workers seem to work just fine. The same is true for running local tests without test_workers enabled. </p>
  
  <p>I have tried the examples found here <a href="https://github.com/browserstack/nightwatch-browserstack" rel="nofollow noreferrer">https://github.com/browserstack/nightwatch-browserstack</a> but none of these uses browserstack-local in combination with Nightwatch test_workers. </p>
  
  <p>When running locally with test_workers I get the following output.</p>
  
  <pre><code>Connecting local
  Connected. Now testing...
  Process terminated with code 0.
  </code></pre>
  
  <p>Has anyone else encountered similar issues?</p>
  
  <p>EDIT: I have since resolved this issue and have posted an answer below</p>
  
  <p>I have dumped the relevant configuration files below.</p>
  
  <p>My local.conf.js</p>
  
  <pre><code>nightwatch_config = {
      globals_path: 'globals.js',
      output_folder: false,
      src_folders: ['tests'],
      selenium: {
          'start_process': false,
          'host': 'hub-cloud.browserstack.com',
          'port': 80
      },
      test_workers: {
          "enabled": true,
          "workers":2
      },
      test_settings: {
          default: {
              desiredCapabilities: {
                  'browserstack.user': process.env.BROWSERSTACK_USER,
                  'browserstack.key': process.env.BROWSERSTACK_ACCESS_KEY,
                  'browserstack.local': true,
                  'browserstack.debug': false,
              }
          }
      }
  };
  
  // Code to copy seleniumhost/port into test settings
  for (let i in nightwatch_config.test_settings) {
      if (nightwatch_config.test_settings.hasOwnProperty(i)) {
          let config = nightwatch_config.test_settings[i];
          config['selenium_host'] = nightwatch_config.selenium.host;
          config['selenium_port'] = nightwatch_config.selenium.port;
      }
  }
  
  module.exports = nightwatch_config;
  </code></pre>
  
  <p>local.runner.js</p>
  
  <pre><code>    #!/usr/bin/env node
  
  var Nightwatch = require('nightwatch');
  var browserstack = require('browserstack-local');
  var bs_local;
  
  process.env.BROWSERSTACK_ID = new Date().getTime();
  
  try {
      process.mainModule.filename = "./node_modules/.bin/nightwatch";
  
      // Code to start browserstack local before start of test
      console.log("Connecting local");
      Nightwatch.bs_local = bs_local = new browserstack.Local();
      bs_local.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY }, function (error) {
          if (error) throw error;
  
          console.log('Connected. Now testing...');
          Nightwatch.cli(function (argv) {
              Nightwatch.CliRunner(argv)
                  .setup(null, function () {
                      // Code to stop browserstack local after end of parallel test
                      bs_local.stop(function () { });
                  })
                  .runTests(function () {
                      // Code to stop browserstack local after end of single test
                      bs_local.stop(function () { });
                  });
          });
      });
  } catch (ex) {
      console.log('There was an error while starting the test runner:\n\n');
      process.stderr.write(ex.stack + '\n');
      process.exit(2);
  }
  </code></pre>
  
  <p>and my package.json script </p>
  
  <pre><code>node ./local.runner.js -c ./local.conf.js
  </code></pre>