How to test package commands?


#1

How code testing packages-commands is supposed to work?

The problem I’m facing is that the specs dealing with a package command shows seemingly random behaviour. In particular I’ve four tests that do similar work, and the last of them often (not always) fails. I wonder if the problem can be ascribed to the activation of the package being a promise.

Here is what I do in my beforeEach method:

    beforeEach ->
      atom.workspaceView = new WorkspaceView();
      atom.project.setPath path.join(__dirname, 'fixtures')
      atom.workspaceView.openSync('sample.html')
      atom.workspaceView.attachToDom();
      @editorView = atom.workspaceView.getActiveView();
      @editor = @editorView.getEditor()
      atom.packages.activatePackage("language-html")

and here is what I do in one of the specs:

    it 'closes the first non closed tag', ->
      @editor.setCursorBufferPosition(new Point(5,14))
      @editorView.trigger('language-html:close-tag')

      cursorPos = @editor.getCursorBufferPosition()
      insertedText = @editor.getTextInRange( new Range([5,14], [5,18]) )

      expect( cursorPos ).toEqual( new Point(5, 18) )
      expect( insertedText ).toEqual('</a>')

Any best practice to follow? Is there any documentation about this topic?


#2

I’d recommend checking out the Whitespace package and its specs. Specifically, the waitsForPromise construct:

waitsForPromise ->
  atom.packages.activatePackage('whitespace')

#3

Thanks @leedohm.

For the record, the pattern in the Whitespace package would not work for me: waitsForPromise would hang waiting for the activation to happen.

In my understanding the reason is that the package is activated only when the first command is executed (which happens inside the “it” method which is never called since the beforeEach method hangs waiting for the activation).

I’ve solved the problem by following the pattern in the specs of the rspec package.