EzDevInfo.com

testem

Test'em 'Scripts! A test runner that makes Javascript unit testing fun.

Single test fails in PhantomJS but works in Chrome and Firefox

I have a single acceptance test in Ember.js 1.10, Ember CLI 0.1.12 which fails on PhantomJS but runs fine in both Chrome and Firefox. I've tried to debug this for 2 days but I'm running out of ideas. Name of the test is user can view logged-only pages when he is logged. Basically when you are not logged and you try to access classic route, for example /about you are redirected to start.login in beforeModel hook of classic route:

beforeModel: ->
  if not @controllerFor('application').get 'logged'
    @transitionTo 'start.login'

When you are on start.login and you will give correct name and username, logIn action in StartLoginController will be called:

logIn: ->
  if not @get('logged')
    @get('controllers.application').send 'logIn', @get('username'), @get('password'), @get('rememberMe')

Which calls following action in ApplicationController:

  actions:
    logIn: (username, password, remember) ->
      if not @get 'logged'
        $.ajax
          url: @get('apiURL') + '/auth/login'
          type: 'POST'
          data:
            name: username
            password: password
            remember: remember
          xhrFields:
            withCredentials: true #ensure CORS
        .then (response) =>
          if response.success

            expires = if remember then new Date(response.cookieExpiration) else null

            $.cookie 'auth_user_id', response.user.id,
              expires: expires
              path: '/'

            $.cookie 'auth_expiration', response.cookieExpiration,
              expires: expires
              path: '/'

            @setProperties
              logged: true
              'controllers.auth.userId': response.user.id

            @transitionToRoute 'classic.index'
        , =>
          @send 'openModal', 'modal/wrong-credentials'

      false

And this works fine even in PhantomJS. Other tests pass. Actions are called correctly, properties are set correctly, cookies are set correctly. Even beforeModel hook correctly calls(or not) transitionTo method. I've thought that issue with PhantomJS is with some async order of calling things but I've tried wrapping code in Ember.run and andThen in many places. No luck at all.

testem.json:

{
  "framework": "qunit",
  "test_page": "tests/index.html?hidepassed",
  "launch_in_ci": [
    "PhantomJS"
  ],
  "launch_in_dev": [
    "PhantomJS",
    "Chrome",
    "Firefox"
  ]
}

Acceptance test login-test.coffee(failing test is the last one):

`import Ember from 'ember'`
`import startApp from '../helpers/start-app'`

application = null

run = Ember.run

login = ->
  visit '/'

  fillIn '[placeholder=Login]', 'test'
  fillIn '[placeholder=Hasło]', 'test'

  click '.button-login'

clearCookies = ->
  cookies = $.cookie()

  for cookie of cookies
    $.removeCookie cookie,
      path: '/'

  cookies = document.cookie.split ';'
  for i in [0...cookies.length] by 1
    equals = cookies[i].indexOf '='
    name = if equals > -1 then cookies[i].substr(0, equals) else cookies[i]
    document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT"

module 'Acceptance: Login',
  setup: ->
    application = startApp()

    clearCookies()

    time = new Date()

    $.mockjaxSettings.logging = false

    $.mockjax
      type: 'POST'
      url: 'api/v1/auth/login'
      dataType: 'json'
      responseText:
        success: true
        user:
          id: 1
        cookieExpiration: time.setDate time.getDate() + 14

    $.mockjax
      type: 'GET'
      url: '/api/v1/users/1'
      dataType: 'json'
      responseText:
        user:
          id: 1

    $.mockjax
      type: 'GET'
      url: '/api/v1/statuses' # ?limitOld=10&friends=true&comments[limit]=5
      data:
        comments:
          limit: 5
        friends: true
        limitOld: 10
      responseText:
        statuses: {}

    $.mockjax
      type: 'GET'
      url: '/api/v1/getRandomQuote'

    $.mockjax
      type: 'GET'
      url: '/api/v1/statuses/?friends=true&count=true'
      responseText:
        count: 0

    $.mockjax
      type: 'GET'
      url: '/api/v1/meals'

    $.mockjax
      type: 'GET'
      url: '/api/v1/notifications'

    $.mockjax
      type: 'GET'
      url: '/api/v1/notifications'
      data:
        read: false
      responseText: {}

    return

  teardown: ->
    $.mockjax.clear()

    clearCookies()

    run application, 'destroy'

test 'user lands on default page when he is not logged', ->
  expect 1

  visit '/'

  andThen ->
    equal currentPath(), 'start.login'

test 'login page is displayed when you are trying to access logged-only page', ->
  expect 1

  visit '/kitchen'

  andThen ->
    equal currentPath(), 'start.login'

test 'user can login', ->
  expect 2

  appController = application.__container__.lookup 'controller:application'

  equal appController.get('logged'), false, 'user is not logged before login'

  login()

  andThen ->
    ok appController.get 'logged', 'user is logged when response is success'

test 'user can view logged-only pages when he is logged', ->
  expect 2
  console.log '-- LOGGED-ONLY TEST START --'

  visit '/about'

  andThen ->
    equal currentPath(), 'start.login'

  login()

  visit '/about'

  andThen ->
    equal currentPath(), 'classic.about'

And finally output from tests:

TEST'EM 'SCRIPTS!                                                          
Open the URL below in a browser to connect.                                
http://localhost:7357/                                                     
━━━━━━━━━━━━━━┓                                                            
 PhantomJS 1.9┃  Firefox 37.0                                              
  198/199 ✘   ┃  199/199 ✔                                                 
              ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Acceptance: Login: user can view logged-only pages when he is logged
    ✘ failed
         expected classic.about
         actual start.login

-- LOGGED-ONLY TEST START --

I don't think classic.about is source of error, because replacing it with other child routes of classic resource result in same PhantomJS failing test.

Okay so it seems that problem lies in model for ClassicRoute(commenting that makes test pass):

  model: ->
    new Promise (resolve, reject) =>
      @store.find 'user', @controllerFor('auth').get('userId')
        .then (user) =>
          @controllerFor('auth').set 'user', user
          resolve
            profile: user.get 'profile'

Source: (StackOverflow)

Ember CLI tests on Sauce Labs

How can I get ember-cli tests running on Sauce Labs? Testem has an example configuration, but I don't know how to translate that into the ember-cli compiled tests since the testem.json gets packed into the build when tests run.

I tried doing an ember build --env=test and then putting "test_page": "dist/tests/index.html" in my testem.js and just running testem ci --port=8080 as it is in the example, but that gives me 0 tests run.


Source: (StackOverflow)

Advertisements

Testing elements with jQuery-Chai

I have a couple of functions that use jQuery. And I'm having trouble making sense of the proper way to test them with jQuery-Chai in Mocha+Chai.

I see the list of assertions in the jQuery-Chai plugin. However, I don't understand where we get the DOM data to run those assertions?

Ideally I want to insert a line of html. Run the function on it. And use the jQuery-Chai assertion to validate.

Can someone help clear up where I would include the fixture to test these functions?

Thanks in advance.

Using: Testem with Mocha+Chai.


Source: (StackOverflow)

Exclude files while using Ember-CLI testem

I am trying to run my tests using Ember-CLI - Testem. Ember-CLI uses tests/index.html and not the usual testem.json config for testing.

Is there any way I can exclude certain files from being built into app.js?

Usecase : I have some js files where I inject some dependencies. These dependencies are different during the testing environment. I would like to ignore these files and inject the dependencies from my test-injectors.


Source: (StackOverflow)

Putting ember-cli tests in pods

I want to try to place tests in pods, so instead of

tests
  unit
    routes
      resource-test.js
app
  pods
    resource
      route.js

I want to have

app
  pods
    resource
      route.js
      route-test.js

This seems to be taking pods to their logical conclusion--namely, to include all pod-related "things" in the pod, instead of scattered somewhere else in the directory structure.

I am able to get the tests picked up by setting the src_files property in testem.json to include app/pods/**/*-test.js.

However, I am running into a problem with jshint. The .jshintrc in tests has predefs for things like moduleFor. So tests located inside tests pass jshint fine. However, app has its own .jshintrc, which doesn't define the test-related globals. So jshint fails on the test files under app/pods. I would prefer to keep a separate .jshintrc for testing-related files, but how can I have one .jshintrc applied to the test files and another one applied to non-test files?

More generally, are there any best practices or tricks for putting test files into the pod structure?


Source: (StackOverflow)

How can I get source maps to work when running tests using ember-qunit for an ember app built on ember-cli

I have an Ember app built using ember-cli and I'm writing my tests using the ember-qunit testing adapter and running them in the browser using testem as instructed in the ember-cli documentation. Although debugging in Google Chrome works fine when I'm interesting with my app, I am unable to use many debugging features such as breakpoints when running my tests.

I often run into a problem that my tests fail despite my actual app seeming to work properly, and to investigate the problem I would like to step through code while my tests are running.

But when I step into code that appears in vendor.js I just just see the following contents in my vendor.js:

// Please wait a bit.
// Compiled script is not shown while source map is being loaded!

These two lines are lines 6 & 7 of the file. The lines before this are blank, and these two lines are the last lines in the file. The debugger has the first line of the file highlighted as if that's the current location in the source, but it cannot show the source for some reason.

I can proceed to step through the code, but I can't see anything.

However, if I find vendor.js in the list of sources in the developer tools sources file list then it opens as a separate source tab and I can see all my code. At this point I have two tabs labeled vendor.js, one with all my vendor assets and one with just those line quoted above.

I am guessing that there is something different between how my tests are served and how my app is served in the development environment that is confusing Chrome.

I am using the following versions of things:

ember 1.9.1
ember-data 1.0.0-beta.14.1
ember-cli 0.1.9
qunit 1.17.1
ember-qunit 0.2.0
testem 0.6.33

Although I've poked around a bunch I don't really have any leads on where the problem is stemming from. Perhaps it's related to how testem is running the tests? Or could it be something that gets included in my tests has a messed up source map?

I appreciate any help or ideas.


Source: (StackOverflow)

tests fail or hang randomly on travis with PhantomJS

I run my tests with testem and PhantomJS, they all pass on my local machine, but when i run them on travis, they fail randomly, or hang, It's not like some specific tests fail, some pass, randomsome fail, I have no idea what's going on. My project is available on github https://github.com/eguneys/marbles-site

Edit:

I've optimized it a little, it fixed the hang issue, now the problem is it fails the first integration test. I added an App.reset() before the first integration test. And it passes now. I had trouble with App.reset() before,

Erik Bryn suggested that you can use App.reset() in the setup method rather than teardown so you can see the application's last state. But that causes tests to fail, i can't solve the mystery of App.reset() and fear the tests will fail anytime now. Yet they pass at the moment.


Source: (StackOverflow)

Slow static file serving with Node on Ubuntu

We're using Testem to serve a bunch of HTML files (templates). Under the covers Testem uses the "res.sendfile" method of Express to send the static file back to the client. On Mac machines this is very fast - 1-2 ms per file according to the Chrome network trace. On an Ubuntu machine, however, it takes 39ms.

This is on the latest stable Node - 0.10.29. Testem is using Express 3.1.

Any suggestions on what might cause this or how I can diagnose it further?


Source: (StackOverflow)

Connecting to a PHP website with Node.js and keep session

I'm making a testbench with Test'em and Mocha (that run on node.js) in order to test a PHP website.

What I want is to request some URL (e.g http://www.my-website/test.php) and get the http status code as well as the content returned.

I'm doing it with the node.js Request module.

The problem is:

I need to be authenticated to access this page, otherwise I'm redirected to the login page.

So, does it exist a way to log in my application through Node.js and keep the session open to be able to chain tests on any pages I want?

I was thinking on get the PHPSESSID on login request if it is possible. Do you thing it is a good direction ?

Any help would be much appreciated.

Thank you, have a nice day :)

Michaël


Source: (StackOverflow)

not ok PhantomJS exited unexpectedly

$ testem ci

not ok 1 PhantomJS - Browser "phantomjs /home/ubuntu/.nvm/v0.10.12/lib/node_modules/testem/assets/phantom.js http://localhost:7357/6092" exited unexpectedly.

1..1
# tests 1
# pass  0
# fail  1

$ phantomjs --version

2014-07-28T00:24:22 [WARNING] Unable to load library icui18n "Cannot load library icui18n: (libicui18n.so.48: cannot open shared object file: No such file or directory)"
2014-07-28T00:24:22 [WARNING] phantomjs: cannot connect to X server

What's wrong?

testem command works fine and all the tests pass in Chrome


Source: (StackOverflow)

Trying to spy (Jasmine) on Array.prototype methods causes stack overflow

This is pretty odd. Using the testem runner with jasmine2 and the following spec executes (though it correctly flags that there are no expectations):

describe('Spying on array.prototype methods', function(){
  it('should work this way', function(){
    spyOn( Array.prototype, 'push' ).and.callThrough();
    // expect(1).toBe(1);
  });
});

However, add an expect (any expect!) and it causes the stack to overflow with the following message in the testem console: RangeError: Maximum call stack size exceeded. at http://localhost:7357/testem/jasmine2.js, line 980 The html report page gets up to the spec and then hangs without showing any actual results.

Ultimately I'd like to do something like this:

describe('Some structure.method', function(){
  it('does not use native push', function(){
    spyOn( Array.prototype, 'push' ).and.callThrough();
    [].push(1); // actually more like `myStructure.myMethod('test')`
    expect( Array.prototype.push ).not.toHaveBeenCalled();
  });
});

Thanks in advance to anyone who can shed light on this oddity. Can I not spy on native prototypical methods?


Source: (StackOverflow)

When I run ember test and visit /tests the results are inconsistent, how can I troubleshoot why these are different?

I have been working with ember for a little over a month now and I have yet to find a solution to some testing inconsistencies I have been experiencing.

The problem is that when I run ember test from the command line and visit /tests in the browser sometimes I see a different total number of tests. It seems like ember test with phantomjs as the test runner is skipping some tests. On top of that the results seem to be inconsistent as well.

For instance, I have a simple acceptance test:

import Ember from 'ember';
import startApp from '../helpers/start-app';

var App;

module('Acceptance: Login', {
  setup: function() {
    App = startApp();
  },
  teardown: function() {
    Ember.run(App, 'destroy');
  }
});

test('Page contents', function() {
  visit('/login');

  andThen(function() {
    equal(find('form.login').length, 1);
  });
});

When I visit /tests, all of my tests pass, however when I run Ember test I get one failure:

not ok 1 PhantomJS 1.9 - Acceptance: Login: Page contents
---
    actual: >
        0
    expected: >
        1
    Log: >
...

Thanks in advance for any help.


Source: (StackOverflow)

How can I set up mocha tests to run Ember initializers so injected objects are defined?

My Ember app injects an "i18n" object into the container via an initializer, which is later looked up with "this.container.lookup('i18n:main')" in a controller 'preferredLanguage' computed property.

A mocha unit test which tests the controller 'preferredLanguage' property, fails due to "i18n is not defined". How can I set up the mocha tests to run Ember application initializers so injected objects are defined when looked up from the container during unit testing?


Source: (StackOverflow)

How can I fix tests in Ember testem with errors such as 'could not load', 'failed', 'could not find module' or 'died'?

I managed to get a couple of EAK/grunt based Ember apps upgraded to 1.11 with HTMLBars, and then got them migrated to Ember CLI/Brocolli. The unit tests were setup for karma test runner so I'm looking at how to get those running in the CLI projects now, but I didn't write the tests and really have no experience with unit testing javascript modules.

Searching around the iNet, I can see that others have also used karma becasue of its coverage output and are trying to get it to work with Ember CLI, but that Ember Core isn't supporting it, though they say anyone should be able to get it set up with a custom addon. I'm also trying to use the 'testem' runner to see what sticks with that.

The Ember site does have an 'automating tests with runners' page for v1.10, with sections on 'testem' and 'karma', but it doesn't appear for v1.11 so I can't tell from that site what is or isn't relevant. But it seems like I should be able to work out a solution for the karma test runner, so I added the old devDependencies to the project package.json:

"karma": "^0.12.31",
"karma-chai": "~0.1.0",
"karma-chrome-launcher": "~0.1.2",
"karma-coverage": "~0.2.1",
"karma-firefox-launcher": "~0.1.3",
"karma-junit-reporter": "~0.2.1",
"karma-mocha": "~0.1.3",
"karma-phantomjs-launcher": "~0.1.2",
"karma-sinon-chai": "~0.1.5"

I also dropped the old 'karma.conf.js' (along with a few other karma confs) in the project and updated the paths inside (from 'vendor' to 'bower_components'). I did find a 'ember-cli-karma' node mode and installed it, but it seems to just have a 'package.json'. It has no docs and seems like just a stubbed out starter project with no implementation. I also installed 'karma', 'karma-cli' and 'testem' node modules.

The testem docs say to add you src and test files to 'testem.json', but with out examples I don't know what that means; a list of every src and test file? With what path; relative, absolute? Forward slashes, backslashes? preceded with / or ./ or ../? I just left them out because I think the system just finds the src and tests by convention.

When I run 'karma init' I get:

readline.js:529
this.line = this.line.slice(this.cursor);
                   ^
  TypeError: Cannot read property 'slice' of undefined

When I run 'testem' I get:

TEST'EM 'SCRIPTS!
Open the URL below in a browser to connect.
http://localhost:7357/aN;0faN;NaNf

...then the project's '../tests/index.html' loads in a browser, but is not able to 'find' any of the asset files (css, js) so nothing executes or renders correctly. I just see template expressions ({{content-for 'head'}}, etc).

When I run 'ember test' I get:

Building...BuildingBuilding.Building..Building...Built project successfully.

1..0
# tests 0
# pass  0
# fail  0

# ok
No tests were run, please check whether any errors occurred in the page (ember test --server) and ensure that you have a test launcher (e.g. PhantomJS) enabled.

When I run 'ember test --server' I get:

The test index.html loaded in a browser with a test report. When I uncheck 'hide passed tests' the report indicates '29 passed, 28 failed'. It has 11 sections where a particular test may have 3 problems such as 'could not load', 'failed', 'could not find module', 'attempting to register an unknown factory' or 'died'.

With this, I'm obviously running testem and not karma, so may as well work on getting testem working and figure out karma later. If there were more examples and migration troubleshooting docs I might have a systematic way to work through some of these problems.


Source: (StackOverflow)

exec 'node app' hangs inside gulp task

This gulp task hangs on exec('node config/app') line. first exec works fine but the second just hangs.

gulp.task('test', function(cb) {
    var exec = require('child_process').exec;

    exec('echo 3', function(err, stdout) {
        console.log(stdout);
    });

    exec('node config/app', function(err, stdout, stderr) {

        console.log(stdout);

        var testemOptions = {
            file: 'testem.json'
        };

        var t = new testem();

        return t.startCI(testemOptions, function() {
            cb();
        });
    });

});

I can see the output 3 but no output is shown for the second console.log.

I am trying to run my server before running the tests with testem.

I've tried this similar solution but it doesn't work: Exec not returning anything when trying to run git shortlog with nodejs.

Also I've recently asked a hanging testem gulp task question: Testem gulp task hangs after finished.

Edit:

My current solution is:

gulp.task('test', /*['build'],*/ function(cb) {
    var spawn = require('child_process').spawn;

    var proc = spawn('node', ['config/app']);

    proc.stdout.on('readable', function() {
        var output = proc.stdout.read();

        if (output && output.toString().match('express listening')) {

            var testemOptions = {
                file: 'testem.json'
            };

            var t = new testem();

            t.startCI(testemOptions, function() {
                proc.kill();
                cb();
            });
        }

    });

});

Source: (StackOverflow)