mercredi 29 juin 2016

Sinon stub error with localstorage in karma tests (es6 + jspm)

I am trying to stub setItem and getItem methods of window.localStorage and I am experimenting the issues as it can be seen in the screenshot:

enter image description here

The point is I don't know what happen to the window.localStorage object which seems to be different depending on the property. When the time comes to stub the setItem method, I get

Testing storage services LocalStorage "before each" hook for "should return current name":
     TypeError: Attempted to wrap string property setItem as function
      at checkWrappedMethod (base/node_modules/sinon/pkg/sinon.js:1355:29)
      at Object.wrapMethod (base/node_modules/sinon/pkg/sinon.js:1398:21)
      at Object.stub (base/node_modules/sinon/pkg/sinon.js:3465:26)
      at Context.eval (client/js/common/services/storage/localstorage.spec.js!transpiled:30:14)
      at Object.invoke (base/client/jspm_packages/github/angular/bower-angular@1.5.1/angular.js:4628:19)
      at Context.workFn (base/client/jspm_packages/npm/angular-mocks@1.4.8/angular-mocks.js:2441:20)
      at window.inject.angular.mock.inject (base/client/jspm_packages/npm/angular-mocks@1.4.8/angular-mocks.js:2413:37)
      at Context.eval (client/js/common/services/storage/localstorage.spec.js!transpiled:20:7) Error: Declaration Location
      at window.inject.angular.mock.inject (base/client/jspm_packages/npm/angular-mocks@1.4.8/angular-mocks.js:2412:25)
      at Context.eval (client/js/common/services/storage/localstorage.spec.js!transpiled:20:7)

I am using jspm as a client package/module manager. So, the karma config file is:

basePath: './',

    // frameworks to use
    // available frameworks: http://ift.tt/1ft83uu
    frameworks: ['jspm', 'mocha', 'chai-as-promised', 'chai', 'sinon'],

    // start these browsers
    // available browser launchers: http://ift.tt/1ft83KU
    browsers: ['Chrome'],

    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: http://ift.tt/1ft83KQ
    reporters: ['mocha'],

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: true,

    // enable / disable colors in the output (reporters and logs)
    colors: true,

    // list of files / patterns to load in the browser
    files: [],

    jspm: {
      // Edit this to your needs
      config: 'jspm.config.js',
      packages: 'client/jspm_packages',
      loadFiles: [
        'client/js/common/services/**/*.spec.js'
      ], 
      serveFiles: [
        'client/js/**/*.js',
        'client/js/**/*.html',
        'client/js/**/*.css'
      ],
      paths: {
        'github:*': 'base/client/jspm_packages/github/*',
        'npm:*': 'base/client/jspm_packages/npm/*',
        'js/*': 'base/client/js/*'
      },
      urlRoot: './'
    },

    proxies: {
      '/client': '/base/client'
    },

    // list of files to exclude
    exclude: [],

    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,
    client: {
      captureConsole: true,
      mocha: {
        bail: false,
        // require: 'should'
        reporter: 'spec',
        ui: 'bdd'
      }
    }

The only thing I can say is if someone have had the same issue and/or can provide some hint to come up with a solution :-/

Thanks!!!

Aucun commentaire:

Enregistrer un commentaire