Can't activate package in specs


#1

I’m trying to write some specs for a package and I’m running:

beforeEach ->
  ...

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

and its failing every test with timeout: timed out after 5000 msec waiting for something to happen.

I added another test to check if the package was loaded

it 'should be in the packages list', ->
  expect(atom.packages.loadedPackages["jekyll"]).toBeDefined()

Which fails with Expected undefined to be defined.

I’ve had a look at a few other packages and can’t workout how they are making thier package avliable in the specs

The complete spec file is at https://gist.github.com/Arcath/cb6d9a699abb6e59c3a4


#2

I think the problem doesn’t come from your test setup, but from your actual activation routine:

https://github.com/Arcath/jekyll-atom/blob/master/lib/jekyll.coffee#L23-L30

In your tests you retrieve a WorkspaceElement and in your package you access atom.workspaceView, which doesn’t exist if don’t create it in your specs.

Here’s how I do it when I need both (as with tree-view-breadcrumb as it relies on tree-view which is stil using atom.workspaceView):

BTW, the proper way to define commands is now done by using atom.commands.add(elementOrSelector, commandsObject).


#3

@abe I’ve updated the activate event to contains:

    atom.commands.add 'atom-workspace', "jekyll:open-layout", => @openLayout()
    atom.commands.add 'atom-workspace', "jekyll:open-config", => @openConfig()
    atom.commands.add 'atom-workspace', "jekyll:open-include", => @openInclude()
    atom.commands.add 'atom-workspace', "jekyll:open-data", => @openData()
    atom.commands.add 'atom-workspace', "jekyll:manage", => @manage()
    atom.commands.add 'atom-workspace', "jekyll:toolbar", => @toolbar()
    atom.commands.add 'atom-workspace', "jekyll:toggle-server", => @toggleServer()

and I’m still getting the same errors.

I’m finding it very weird, atom.packages.loadedPackages["jekyll"] is defined in every test other than the first one run… and there is nothing coming up as an error (even when I introduce something like a coffee script compile error in the package).


#4

The reason for the lack of errors is that package activation is handled inside a promise, so if you don’t register a failure handler the error will be trapped by the promise and never reported.
You can try to use the following snippet to debug your package activation in tests:

waitsForPromise ->
  atom.packages.activatePackage('jekyll').fail (reason) -> console.log reason

Also, I do get compilation errors when my coffee is malformed:

/Users/cedric/github/table-edit/lib/table-edit.coffee:3:7: error: unexpected ->
'foo' ->
      ^^ index.js:73window.onload

#5

That snippet doesn’t change the output at all. I’d assume that a timeout killing the promise means that the failure never gets triggered.


#6

Oh, true, I forgot the issue was a timeout. I’ll try to reproduce the same test setup when I get some time.


#7

I’ve even tried commenting everything out so that all activating the package does is return true and it still takes more than 5 seconds and timesout.


#8

I made my activate function start with throw 'this should show up right?' and still got timeouts…

I just pushed my recent changes to github and more importantly got Travis to run the specs just to rule out my environment as the cause.

Travis has the same issue, the build is at https://travis-ci.org/Arcath/jekyll-atom/builds/43577725

To make things more confusing this test:

it 'should activate', ->
  expect(atom.packages.isPackageLoaded('jekyll')).toBe false
  atom.packages.activatePackage('jekyll').then ->
    expect(atom.packages.isPackageActive('jekyll')).toBe true
  expect(atom.packages.isPackageLoaded('jekyll')).toBe true

passes so it can activate the package no problem and atom sees it as active after that.


#9

Ok, I solved your problem.

The reason the package never activate is due to the activationCommands you defined in package.json:

So, You have to trigger one of these commands in your tests to activate the package before testing the promise resolution.


#10

Thanks!

Thats sorted it.


#11

Thanks. My tests were working locally, but I was getting an error on Travis: “TypeError: Cannot read property ‘attachToDom’ of undefined”. This was the fix:

workspaceElement = atom.views.getView(atom.workspace)
jasmine.attachToDOM(workspaceElement)

Not sure why tests have stopped passing