0

I am using grunt connect ("grunt-contrib-connect": "0.11.2") in conjunction with grunt jasmine ("grunt-contrib-jasmine": "^1.1.0"). The connect config was previously hardcoding the port number and the jasmine config was also configured to use the same port number in the host config option.

I now want to enable the useAvailablePort option in connect in order to resolve an issue with concurrent builds using the same port. However I am struggling to understand how to correctly communicate the port number from connect to jasmine?

Here are the relevant sections of my grunt init config block:

connect: {
  test: {
    options: {
      keepalive: false,
      hostname: 'localhost',
      port: 1234,
      base: ['.']
    }
  }
}
jasmine: {
  js: {
    src: ...,
    options: {
      keepRunner: true,
      summary: true,
      host: 'http://localhost:1234/',
      specs: 'src/test/js/**/*Spec.js',
      vendor: ...
    }
  }
}

I have created a grunt test task using:

grunt.registerTask('test', ['connect', 'jasmine:js']);

So what would be the best practice way of capturing the port from connect and setting the host in jasmine:js?

I have been trying to change the test task as follows, but with no joy (also not sure if this would be the correct way to do it):

grunt.registerTask('test', 'Run jasmine unit tests', function () {
  grunt.event.once('connect.test.listening', function(host, port) {
    var jasmineHost = 'http://' + host + ':' + port;
    grunt.log.writeln('Running jasmine tests from: ' + jasmineHost );
    grunt.config.set('jasmine:js:options:host', jasmineHost );
    grunt.task.run('jasmine:js');
  });
  grunt.task.run('connect:test');
});
RobC
  • 22,977
  • 20
  • 73
  • 80
DaveJohnston
  • 10,031
  • 10
  • 54
  • 83

1 Answers1

0

The GruntJS framework includes lodash templates, which you can use in your configs & tasks to inject values at runtime. The documentation can be a little confusing; to solve your problem we'll add a lodash template tag within the string for jasmine.js.options.host & reference the other config value using json notation:

jasmine: {
  js: {
    src: ...,
    options: {
      keepRunner: true,
      summary: true,
      host: 'http://localhost:<%= connect.test.port %>/',
      specs: 'src/test/js/**/*Spec.js',
      vendor: ...
    }
  }
}

During runtime, grunt will process the lodash template tag before executing the task.

RobC
  • 22,977
  • 20
  • 73
  • 80
theaccordance
  • 889
  • 5
  • 13