How do I activate a package in specs?


The basic version is pretty simple. This is for packages that do not have activationCommands defined in their package.json:

describe 'Package Activation', ->
  beforeEach ->
    waitsForPromise ->

If your package does have activationCommands, it is only slightly more complex:

describe 'Package Activation', ->
  [activationPromise, workspaceElement] = []
  executeCommand = (callback) ->
    atom.commands.dispatch(workspaceElement, 'package-name:some-command')
    waitsForPromise -> activationPromise

  beforeEach ->
    workspaceElement = atom.views.getView(atom.workspace)
    activationPromise = atom.packages.activatePackage('package-name')

  it 'executes a command', ->
    executeCommand ->
      expect(something).toEqual somethingElse

The way this works is that it tells Atom to activate the package but does not wait on it to finish activating. Then the test executes the command, whereupon Atom actually activates the package, so when you wait on the promise it is resolved and the test can continue.

(You can find sample code for this in my right-case package.)

Spec "timed out after 5000 msec waiting for something to happen"
Waiting for Promise after activation in Spec
How to activate my package via the console?