1
votes

Is there any way to stop Karma &/or Protractor from linting files in the stream passed to it? We are using gulp, but this could probably apply to other automation tools/environments. While I am sure they are just trying to be helpful, this clogs up our console and is redundant. Worse, it can be confusing to see the same error twice.

Here is an example task:

var karma = require('karma').server;
var spawn = require('child_process').spawn;

gulp.task('tests', function() {
  karma.start({
    files = 'my/unit/test/files/*.spec.js',
    singleRun: true
  }, function(error) {
    if(error) {
      console.log(error);
    }
  });

  spawn('protractor', ['--specs=my/e2e/test/files/*.e2e.js'],
    {stdio: 'inherit'}
  );
});

If the file "my/e2e/test/files/test.e2e.js" exists & has a JSHint error, Protractor will report the error, whether or not you also run gulp-jshint or something else.

As an example, if the file "my/e2e/test/files/test.e2e.js" looked like this:

describe('some test', function() {

);

The output of gulp tests might look like this:

... some setup...

Protractor:
[09:32:30] An error occurred during the end-to-end testing run
[09:32:30] my/e2e/test/files/test.e2e.js:3
    );
    ^

... some Protractor tests ...
... some Karma tests ...

If the file "my/unit/test/files/test.spec.js" exists & has a JSHint error, Karma will report this error, and it will not run. For example, if the file "my/unit/test/files/test.spec.js" looks the same as "my/e2e/test/files/test.e2e.js" above, the output of gulp tests may look like this:

...some setup...

Karma (I think):
PhantomJS 1.9.8 (Linux) ERROR
  SyntaxError: Parse error
  at my/unit/test/files/test.spec.js

SUMMARY:
✔ 0 tests completed

...some protractor tests...

It seems that Karma fails the task and does not run if there is an error in the spec files passed to it, and Protractor will report the error but still run, and will report the error in the tasks contained in the errored file. I would rather both streams run, both sets of tests run completely, and perhaps report syntax errors in the test output. However, they should not report the syntax errors before the tests run, or after, because we already have gulp-jshint for linting files. This is just redundant, and it is twice-redundant, in the case of Protractor, if you have a linting plugin already.

So, back to my question: Is there any way to prevent Karma &/or Protractor from linting in their respective streams, separately from the output of the tests? Is there any way to get access to their respective error reporters, without changing their source code? I could always kill them both on any errors before they run, but I may want to run the tests anyway, and I would definitely want to run the other testing framework (for example, run Karma if there are syntax errors in .e2e.js files).

2
karma and protractor are frameworks for unit and e2e testing, they are not related to linting in general, or specifically to jshint tool. Can you elaborate more on what is happening? Thanks. - alecxe
Well it seems like in general karma and protractor try to be more helpful than they need to be. They are both from the creators of Angular which suffers from the same problems sometimes. In general frameworks try to be helpful and some just go over the line. - trysis
Can you post an output of such behaviour? Unless it's the browser console reporting an error karma shouldn't really lint your files. - MarcoL
Technically it looks like Karma is just reporting any error in the stream, I just wasn't sure yesterday. I will update my question. - trysis
@MarcoCI, alecxe, I expanded the question & gave examples. Is that enough, or should I put more, or less, ...? - trysis

2 Answers

2
votes

The errors that both Protractor and Karma are Error that the Javascript Engines (the browser you're using for testing) are shouting to you. Karma and Protractor are not "linting" anything themselves.

To debug the issue in Karma follow these steps:

  • Add a regular browser to the karma configuration: instead of PhantomJS use either Chrome or Firefox

  • Start the karma server (not the runner): in your Gulpfile.js change singleRun from true to false.

  • Once karma and your browser have started you should have a similar window: Karma window

  • Now click on the DEBUG button, it will open a new tab, where you can open the Dev Tools (in case you're using Chrome): Debug window

  • In the Javascript console now you should be able to see that Syntax Error "Karma is reporting" (effectively karma is just repeating what Phantom is trying to say)

Solve those issues and then try to run again in the browser.
Once everything has been fixed switch back to PhantomJS and singleRun: true.

1
votes

It's not about linting, it's about having understandable/parsable code.

Your example file "my/e2e/test/files/test.e2e.js" is not well written, you're missing function close. Some errors can be "understood" by karma, protractor or your browser, like a missing semicolon.

But errors like not closing a function breaks the parsing of the file and parser won't understand what you mean. Having such code in a website would break the rendering and result in not running.