Spec test failing for no reason?


#1

Spec tests have always confused the hell out of me. Here’s a test that used to work fine, but is now failing. Probably since an upgrade.

EventWatch = require '../lib/event-watch'

describe 'EventWatch', ->
  [element] = []

  beforeEach ->
    workspaceElement = atom.views.getView(atom.workspace)
    jasmine.attachToDOM(workspaceElement)

    waitsForPromise -> atom.packages.activatePackage('status-bar')
    waitsForPromise -> atom.packages.activatePackage('event-watch')

    runs ->
      element = EventWatch.element

  describe 'after initialization', ->
    it 'element is in the status bar', ->
      expect(element).toBeDefined()

    it 'element has expected data', ->
      element.getDatetime = ->
        new Date(1982, 4, 24, 5, 30)
[...]

The first test passes fine, the second test fails because element is null. This used to work. I changed nothing but my package README and now the test is failing.

Package: https://github.com/lexicalunit/event-watch


#2

Are you sure the consumeStatusBar method is being called each time? If consumeStatusBar is only called on the first activation then element won’t be defined for the second test.


#3

Also, you should probably retrieve EventWatch with the promise returned by activatePackage rather than with a require at the beginning of the file (if the test environment clear the module cache after each run, your instance and the instance activated in the test will differ).

waitsForPromise -> 
  atom.packages.activatePackage('event-watch').then (pkg) ->
    EventWatch = pkg.mainModule

#4

Fabulous! Thanks y’all. For future readers, here’s the complete working code:

describe 'EventWatch', ->
  [EventWatch, statusBar, statusBarService, workspaceElement] = []

  beforeEach ->
    workspaceElement = atom.views.getView atom.workspace
    jasmine.attachToDOM(workspaceElement)
    waitsForPromise ->
      atom.packages.activatePackage('status-bar').then (pack) ->
        statusBar = workspaceElement.querySelector 'status-bar'
        statusBarService = pack.mainModule.provideStatusBar()
    waitsForPromise ->
      atom.packages.activatePackage('event-watch').then (pack) ->
        EventWatch = pack.mainModule
        EventWatch.consumeStatusBar statusBar
    waitsForPromise ->
      atom.workspace.open()

  describe 'after initialization', ->
    it 'element is in the status bar', ->
      expect(EventWatch.element).toBeDefined()

#5

I’m trying to convert this package to ES6 just to learn the language. However I seem to be missing something fundamental because, in the following code, pack.mainModule is now null instead of what it should be:

describe('EventWatch', function () {
  let EventWatch

  beforeEach(function () {
    let workspaceElement = atom.views.getView(atom.workspace)
    jasmine.attachToDOM(workspaceElement)

    let statusBar
    waitsForPromise(() => atom.packages.activatePackage('status-bar').then(function (pack) {
      statusBar = workspaceElement.querySelector('status-bar')
    }))

    waitsForPromise(() => atom.packages.activatePackage('event-watch').then(function (pack) {
      EventWatch = pack.mainModule
      EventWatch.consumeStatusBar(statusBar)
    }))

    waitsForPromise(() => atom.workspace.open())
  })

  describe('after initialization', function () {
    it('has the element in the status bar', () =>
      expect(EventWatch.element).toBeDefined())
  })
})

#6

Bah, figured it out. I need to add 'use babel' at the top of my source files :-/