How to pause the Karma function before running tests?

I am faced with what, in my opinion, is a condition of the race between Vim save files and Karma re-running the Jasmine tests. Here's a sequence of four test runs that shows a symptom (I truncated extremely long paths in the error log):

$ karma start karma.conf.js --auto-watch [... snip a lot of coding and test running ...] PhantomJS 1.6 (Linux) LOG: 'Running tests at 2013-08-14T08:19:57.252Z' PhantomJS 1.6 (Linux): Executed 4 of 4 SUCCESS (0.307 secs / 0.013 secs) PhantomJS 1.6 (Linux) LOG: 'Running tests at 2013-08-14T08:20:09.866Z' PhantomJS 1.6 (Linux): Executed 4 of 4 SUCCESS (0.288 secs / 0.012 secs) PhantomJS 1.6 (Linux) LOG: 'Running tests at 2013-08-14T08:20:14.366Z' PhantomJS 1.6 (Linux) controller should have a breadcrumb after $routeChangeSuccess FAILED Error: No module: myapp.question at /home/rmunn/.../angular.js:1124 at ensure (/home/rmunn/.../angular.js:1065) at module (/home/rmunn/.../angular.js:1296) at /home/rmunn/.../angular.js:2806 TypeError: 'undefined' is not an object (evaluating 'rootScope.$broadcast') at /home/rmunn/.../unit/controllersSpec.js:35 PhantomJS 1.6 (Linux): Executed 4 of 4 (1 FAILED) (0.312 secs / 0.014 secs) PhantomJS 1.6 (Linux) LOG: 'Running tests at 2013-08-14T08:20:28.588Z' PhantomJS 1.6 (Linux): Executed 4 of 4 SUCCESS (0.287 secs / 0.013 secs) 

The only change I made to run these four runs was to add and remove spaces from the question.js file; There were no significant code changes, but launch # 3 failed when runs 1, 2, and 4 were performed.

My Vim is configured to store backup files, and by default, Vim saves backup files by renaming the original file and writing a new one. (Except for certain conditions that are not applicable here). I think I'm running a race condition: Vim renames question.js (or any file I just edited), Karma detects this change and runs tests, and since I have so few tests now, all tests run before as the Linux process manager will return to Vim. After Vim is scheduled again, he writes the new contents of question.js , but by now it is too late for the Jasmine test, which depends on the existing and non-empty question.js .

I could fix this by setting Vim not to store backup files, but I really would not risk it. In the end, I use backup files. So is there a way to tell Karma "when you detect a file change, pause for N milliseconds, and then do unit tests"? Although this decision would be brute force, it would solve this particular race condition. Or, if someone has other smart ideas, I would also like to hear them.

+7
javascript angularjs vim karma-runner jasmine
source share
2 answers

This Question GitHub also discusses this issue. Vim can be configured to back up by copying and then overwriting the original instead of moving the original and writing a new file. Although this is not technically the answer to your question, this should be enough to solve your problem:

 set backupcopy=yes 

Now Vim will edit the file in place, and Karma will detect it as โ€œmodifiedโ€ instead of โ€œdeletedโ€.

See :help 'backupcopy' more details:

  *'backupcopy'* *'bkc'* 'backupcopy' 'bkc' string (Vi default for Unix: "yes", otherwise: "auto") global {not in Vi} When writing a file and a backup is made, this option tells how it's done. This is a comma separated list of words. The main values are: "yes" make a copy of the file and overwrite the original one "no" rename the file and write a new one "auto" one of the previous, what works best 
+3
source share

From the karma documentation , there seems to be an โ€œexcludeโ€ option to delete some file template. If you change your option to have a different name for the backup and ignore the Vim backup format, it should work correctly.

But it will somehow change your workflow, you will probably have to ignore the backup in your .gitignore .

Just a comment aside, it seems to me that with vim persistent undo (version> 7.3) it seems that the backup file is a bit redundant. So maybe use

 set nobackup set undodir=~/.vim/undodir if (v:version >= 703) set undofile set undolevels=1000 "maximum number of changes that can be undone set undoreload=10000 "maximum number lines to save for undo on a buffer reload endif 
+1
source share

All Articles