Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work with RequireJS #5

Open
aik099 opened this issue Dec 30, 2013 · 35 comments
Open

Doesn't work with RequireJS #5

aik099 opened this issue Dec 30, 2013 · 35 comments

Comments

@aik099
Copy link

aik099 commented Dec 30, 2013

Because of chai.js detects RequireJS presense on a page (by checking define & define.amd) it doesn't create window.chai variable used by this extension.

As a result I'm getting following errors, when running karma:

TypeError: window.chai is undefined
at http://localhost:9876/absolute/usr/local/lib/node_modules/karma-chai/adapter.js?1376684533000:4

Error: Mismatched anonymous define() module: function (){ return require("chai"); }
http://requirejs.org/docs/errors.html#mismatch
at http://localhost:9876/absolute/usr/local/lib/node_modules/requirejs/require.js?1381784385000:163

This is part of karma.conf.js, that might be relevant:

frameworks: ['mocha', 'chai', 'requirejs'],
@aik099
Copy link
Author

aik099 commented Dec 30, 2013

I've found some article, where it was advised to include requirejs before chai in Karma framework list. This helped.

@nafaabout
Copy link

Oh! Thank you, you saved me, I spent more than 4 hours figuring out how this error can be solved, I never though that the order of the frameworks in Karma.conf.js is significant.
Thank you, problem solved

@aik099
Copy link
Author

aik099 commented Jan 12, 2014

I'm afraid, that it's not Karma fault, but fact, that if a library supports AMD (and Chai does), then it in no case should be include before RequireJS, because RequireJS will detect it and complain about incorrect module loading. Same happens on regular web page too.

@bbialas
Copy link

bbialas commented Jan 28, 2014

Any other solutions? My errors...

INFO [PhantomJS 1.9.7 (Mac OS X)]: Connected on socket zVVCILKMDo_2zsghSf-e
DEBUG [karma]: All browsers are ready, executing
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/karma/static/context.html
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/requirejs/require.js
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/karma-requirejs/lib/adapter.js
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/chai/chai.js
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/karma-chai/adapter.js
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/karma-mocha/lib/adapter.js
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/mocha/mocha.js
PhantomJS 1.9.7 (Mac OS X) ERROR
    TypeError: 'undefined' is not an object (evaluating 'window.chai.should')
    at /Users/Ril/Work/xxx/node_modules/karma-chai/adapter.js:4
PhantomJS 1.9.7 (Mac OS X) ERROR
    Error: Mismatched anonymous define() module: function () { return require("chai"); }
    http://requirejs.org/docs/errors.html#mismatch
    at /Users/Ril/Work/xxx/node_modules/requirejs/require.js:141

@aik099
Copy link
Author

aik099 commented Jan 28, 2014

TypeError: 'undefined' is not an object (evaluating 'window.chai.should')

This tells, that karma-chai node module is trying to use globally (or through requirejs) loaded chai library, when the library itself is absent.

Error: Mismatched anonymous define() module:

This tells, that you're trying to include chai.js as a JS file directly (maybe in Karma configuration) and RequireJS doesn't like that.

@bbialas
Copy link

bbialas commented Jan 28, 2014

Yes, but configured everything with official karma runner description... :/

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['mocha', 'chai', 'requirejs'],
    files: [
      {pattern: 'project/xxx/public/scripts/libs/*.js', included: false},
      {pattern: 'project/xxx/public/scripts/modules/**/*.js', included: false},
      {pattern: 'project/xxx/public/scripts/modules/**/tests/*Spec.js', included: false},
      'project/xxx/public/scripts/modules/test-main.js'
    ],
    exclude: [

    ],
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_DEBUG,
    autoWatch: false,
    browsers: ['PhantomJS'],
    captureTimeout: 60000,
    singleRun: true

  });
};

@aik099
Copy link
Author

aik099 commented Jan 28, 2014

Here is my configuration (not from Official docs, but works):

// Karma configuration
// Generated on Sat Dec 28 2013 12:21:03 GMT+0200 (EET)

module.exports = function (config) {
    config.set({

        // base path, that will be used to resolve files and exclude
        basePath: '.',

        // frameworks to use
        frameworks: ['mocha', 'requirejs', 'chai'],

        // list of files / patterns to load in the browser
        files: [
            'test/bootstrap.js',
            'lib/jquery-1.9.1.js',
            'script.js',
            {pattern: 'lib/**/*.js', included: false, watched: false},
            {pattern: 'modules/**/*.js', included: false},
            {pattern: 'test/**/*Test.js', included: false}
        ],

        // list of files to exclude
        exclude: [

        ],

        // test results reporter to use
        // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
        reporters: ['dots', 'coverage'/*, 'osx'*/],

        preprocessors: {
            // source files, that you wanna generate coverage for
            // do not include tests or libraries
            // (these files will be instrumented by Istanbul)
            'modules/**/*.js': ['coverage']
        },

        coverageReporter: {
            reporters: [
//              {type: 'html', dir: 'coverage/'},
                {type: 'text', dir: 'coverage/', file : 'coverage.txt'},
                {type: 'cobertura', dir: 'coverage/', file : 'cobertura.xml'}
            ]
        },

        // web server port
        port: 9876,

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

        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        logLevel: config.LOG_INFO,

        // enable / disable watching file and executing tests whenever any file changes
        autoWatch: true,

        // Start these browsers, currently available:
        // - Chrome
        // - ChromeCanary
        // - Firefox
        // - Opera (has to be installed with `npm install karma-opera-launcher`)
        // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
        // - PhantomJS
        // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
        browsers: ['Firefox'],

        // If browser does not capture in given timeout [ms], kill it
        captureTimeout: 60000,

        // Continuous Integration mode
        // if true, it capture browsers, run tests and exit
        singleRun: false
    });
};

@aik099
Copy link
Author

aik099 commented Jan 28, 2014

Notice, that requirejs and chai order in configuration. This was the source of the problem for me. I see you have wrong order there as well.

@bbialas
Copy link

bbialas commented Jan 28, 2014

I spent already couple of hours on this problem... And the DEBUG for my configuration:

DEBUG [plugin]: Loading karma-* from /Users/Ril/Work/xxx/node_modules
DEBUG [plugin]: Loading plugin /Users/Ril/Work/xxx/node_modules/karma-chai.
DEBUG [plugin]: Loading plugin /Users/Ril/Work/xxx/node_modules/karma-chrome-launcher.
DEBUG [plugin]: Loading plugin /Users/Ril/Work/xxx/node_modules/karma-coffee-preprocessor.
DEBUG [plugin]: Loading plugin /Users/Ril/Work/xxx/node_modules/karma-firefox-launcher.
DEBUG [plugin]: Loading plugin /Users/Ril/Work/xxx/node_modules/karma-html2js-preprocessor.
DEBUG [plugin]: Loading plugin /Users/Ril/Work/xxx/node_modules/karma-jasmine.
DEBUG [plugin]: Loading plugin /Users/Ril/Work/xxx/node_modules/karma-mocha.
DEBUG [plugin]: Loading plugin /Users/Ril/Work/xxx/node_modules/karma-phantomjs-launcher.
DEBUG [plugin]: Loading plugin /Users/Ril/Work/xxx/node_modules/karma-requirejs.
DEBUG [plugin]: Loading plugin /Users/Ril/Work/xxx/node_modules/karma-script-launcher.
INFO [karma]: Karma v0.10.9 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
DEBUG [launcher]: Creating temp dir at /var/folders/9s/b9ckf62n7hz5fj8b3ytkbk400000gp/T/karma-94950220
DEBUG [launcher]: /Users/Ril/Work/xxx/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/lib/phantom/bin/phantomjs /var/folders/9s/b9ckf62n7hz5fj8b3ytkbk400000gp/T/karma-94950220/capture.js
DEBUG [watcher]: Excluded file "/Users/Ril/Work/xxx/project/xxx/public/scripts/modules/script.js"
DEBUG [watcher]: Resolved files:
    /Users/Ril/Work/xxx/node_modules/requirejs/require.js
    /Users/Ril/Work/xxx/node_modules/karma-requirejs/lib/adapter.js
    /Users/Ril/Work/xxx/node_modules/karma-chai/node_modules/chai/chai.js
    /Users/Ril/Work/xxx/node_modules/karma-chai/adapter.js
    /Users/Ril/Work/xxx/node_modules/mocha/mocha.js
    /Users/Ril/Work/xxx/node_modules/karma-mocha/lib/adapter.js
    /Users/Ril/Work/xxx/project/xxx/public/scripts/modules/liveSearch/main.js
    /Users/Ril/Work/xxx/project/xxx/public/scripts/modules/liveSearch/tests/mainSpec.js
    /Users/Ril/Work/xxx/project/xxx/public/scripts/modules/test-main.js
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/karma/static/client.html
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/karma/static/karma.js
DEBUG [karma]: A browser has connected on socket J1p_jNwxXOTwayg105QY
INFO [PhantomJS 1.9.7 (Mac OS X)]: Connected on socket J1p_jNwxXOTwayg105QY
DEBUG [karma]: All browsers are ready, executing
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/karma/static/context.html
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/requirejs/require.js
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/karma-requirejs/lib/adapter.js
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/karma-chai/adapter.js
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/karma-chai/node_modules/chai/chai.js
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/karma-mocha/lib/adapter.js
DEBUG [web-server]: serving: /Users/Ril/Work/xxx/node_modules/mocha/mocha.js
PhantomJS 1.9.7 (Mac OS X) ERROR
    TypeError: 'undefined' is not an object (evaluating 'window.chai.should')
    at /Users/Ril/Work/xxx/node_modules/karma-chai/adapter.js:4
PhantomJS 1.9.7 (Mac OS X) ERROR
    Error: Mismatched anonymous define() module: function () { return require("chai"); }
    http://requirejs.org/docs/errors.html#mismatch
    at /Users/Ril/Work/xxx/node_modules/requirejs/require.js:141
PhantomJS 1.9.7 (Mac OS X): Executed 0 of 0 ERROR (0.041 secs / 0 secs)
DEBUG [launcher]: Disconnecting all browsers
DEBUG [launcher]: Killing PhantomJS
DEBUG [launcher]: Process PhantomJS exitted with code 0
DEBUG [launcher]: Cleaning temp dir /var/folders/9s/b9ckf62n7hz5fj8b3ytkbk400000gp/T/karma-94950220

@aik099
Copy link
Author

aik099 commented Jan 28, 2014

So just changing chai and requirejs order in karma.conf.js doesn't help in your case?

Maybe you're missing chai node module on your system.

@bbialas
Copy link

bbialas commented Jan 28, 2014

When I change frameworks order, then karma freeze on the node_modules/karma-requirejs/lib/adapter.js... :/

@bbialas
Copy link

bbialas commented Jan 28, 2014

This is my package.json:

{
  "name": "xxx",
  "preferGlobal": false,
  "version": "0.0.1",
  "author": "Bartosz Bialas <[email protected]>",
  "description": "xxx",
  "devDependencies": {
    "karma-script-launcher": "~0.1.0",
    "karma-chrome-launcher": "~0.1.2",
    "karma-firefox-launcher": "~0.1.3",
    "karma-html2js-preprocessor": "~0.1.0",
    "karma-jasmine": "~0.1.5",
    "requirejs": "~2.1.10",
    "karma-requirejs": "~0.2.1",
    "karma-coffee-preprocessor": "~0.1.2",
    "karma-phantomjs-launcher": "~0.1.1",
    "karma": "~0.10.9",
    "mocha": "~1.17.1",
    "karma-mocha": "~0.1.1",
    "karma-chai": "0.0.2",
    "chai": "~1.8.1"
  },
  "engines": {
    "node": "~0.10"
  }
}

@bbialas
Copy link

bbialas commented Jan 28, 2014

What is you Node.js version? And Karma, and others components?

@bbialas
Copy link

bbialas commented Jan 28, 2014

Ok, I found a solution... I don't know why, but it works when I put requirejs before chai and put test-main.js at the first line of files array...

My current karma.conf.js:

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['mocha', 'requirejs', 'chai'],
    files: [
      'project/xxx/public/scripts/modules/test-main.js',
      {pattern: 'project/xxx/public/scripts/libs/*.js', included: false},
      {pattern: 'project/xxx/public/scripts/modules/**/*.js', included: false},
      {pattern: 'project/xxx/public/scripts/modules/**/tests/*Spec.js', included: false},
    ],
    exclude: [
        'project/xxx/public/scripts/modules/script.js'
    ],
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_DEBUG,
    autoWatch: false,
    browsers: ['PhantomJS'],
    captureTimeout: 60000,
    singleRun: true
  });
};

Strange, but it work 👍

Thanks for help! :)

@aik099
Copy link
Author

aik099 commented Jan 28, 2014

It's not strange, since when you put test-main.js first, then it bootstraps your application and configures your RequireJS. Then Karma uses it to load chai over RequireJS. This is exactly the same in config I've posted.

@bbialas
Copy link

bbialas commented Jan 28, 2014

It is strange, because official documentation tells us, to put test-main.js at the end... :) You are right, it is logical, why we should put it in the first line :) 👍

@thom4parisot
Copy link
Contributor

Indeed, having requirejs before mocha solved the problem.

Thanks!

@alexgorbatchev
Copy link
Collaborator

I think this deserves a note in README. Would love to get a PR.

@aik099
Copy link
Author

aik099 commented Feb 13, 2014

I think this deserves a note in README.

What text exactly? We've talked about many things. While figuring out what text you can easily put it there with 1 click I guess.

@jonwyatt-mas
Copy link

@aik099 - thanks for posting the solution, fixed it for me!

@percyhanna
Copy link

TL;DR:

I think part of the confusion from above is that the ordering of the frameworks array is backwards: it loads the last framework first, and the first one last. The solution is to make requirejs load after chai, which would mean placing it before chai in the frameworks array.

@NoopDog
Copy link

NoopDog commented Jun 26, 2014

frameworks: ['mocha', 'requirejs','chai', 'sinon' ],

worked for me.

@jdhuntington
Copy link

The ordering of frameworks didn't have any effect for me, but overriding requirejs's path to chai did the trick:

require.config({
  // Karma serves files under /base, which is the basePath from your config file
    baseUrl: '/base',
    paths: {
        "chai": "/base/node_modules/chai/chai"
    },

  // dynamically load all test files
  deps: allTestFiles,

  // we have to kickoff jasmine, as it is asynchronous
  callback: window.__karma__.start
});

@attila226
Copy link

@jdhuntington - Thanks! the ordering didn't work for me either but your solution worked.

@pr0r0k131
Copy link

Good day to all.

When I use karma frameworks in order "frameworks: ['mocha', 'chai', 'requirejs']", in logs I see

TypeError: 'undefined' is not an object (evaluating 'window.chai.should')

But when i've changed "requirejs" with "chai", it make an error

Error: Mismatched anonymous define() module: function () {}

What have I do? Thanks!

@aik099
Copy link
Author

aik099 commented Mar 16, 2015

So mocha, requirejs, chai order doesn't work for you?

@pr0r0k131
Copy link

Chai is working. But files with anonymous defines

define([], function () {})

having error

Error: Mismatched anonymous define() module: function () {}

@aik099
Copy link
Author

aik099 commented Mar 16, 2015

That's strange. Maybe there is a bug in last RequireJS package, that wraps browser version of requirejs.

@pr0r0k131
Copy link

Requirejs version is 2.1.16.

Maybe I forgot some module? My modules list looks like this

  • chai
  • gulp
  • gulp-html-replace
  • gulp-jshint
  • jasmine-core
  • jshint-stylish
  • karma
  • karma-chai
  • karma-jasmine
  • karma-mocha
  • karma-phantomjs-launcher
  • karma-requirejs
  • mocha
  • requirejs
  • run-sequence

@aik099
Copy link
Author

aik099 commented Mar 16, 2015

Can you try exactly my config from: #5 (comment) ?

@pr0r0k131
Copy link

Not working (((((((((((((

Error: Mismatched anonymous define() module: function () {}

Ok. Thanks! I will try some else versions of requirejs.

@pr0r0k131
Copy link

Else versions didn't help. A feeling that requirejs clears "window.chai" or "window.chai" don't started without requirejs.
I don't understand...

@pwdonald
Copy link

pwdonald commented Jun 9, 2015

I was having this problem, it was related to having karma installed both locally and globally.

@arcanis
Copy link

arcanis commented Aug 26, 2015

I don't use requirejs, but I get the same error. It probably comes from the fact that I preprocess my tests with webpack.

@raghanag
Copy link

raghanag commented Sep 17, 2019

Hi All,

I am getting the below error when i try to include more libraries required for my test files.

image

Here is my karma.conf.js

module.exports = function(config) {
  config.set({
    basePath: '.',
    plugins: [
      'karma-*'
    ],
    frameworks: ['requirejs', 'mocha', 'chai'],
    files:[
      'test-main.js',
      {pattern: 'src/ts/tests/**/*.ts', included:false}
    ],
    preprocessors: {
      'test-main.js': 'typescript',
      'src/ts/tests/**/*.ts': 'typescript'
    },
    typescriptPreprocessor: {
      options: {
        module: 'amd',
        target: 'es5'
      }
    },
    reporters: ['dots', 'karma-typescript'],
    autoWatch: true,
    singleRun: false,
    concurrency: 2,
    browsers: ['Chrome']
  })
}

and test-main.js

(function() {
    var specFiles = null;
    var baseUrl = '';
    var allTestFiles = [];
    var requirejsCallback = null;
    var TEST_REGEXP = /^\/src\/ts\/tests\/.*(spec|test)\.js$/i

    if (typeof window !== 'undefined' && window.__karma__ !== undefined) {
      baseUrl = '/base/src';
      var start = window.__karma__.start;
      requirejsCallback = function() {
        require(['mocha','chai','knockout','jquery','ojs'], start);
      };
  
      specFiles = [];
      for (var file in window.__karma__.files) {
        if ((/spec.js$/i).test(file)) {
          specFiles.push(file);
        }
      }
    }
    requirejs.config({
        baseUrl: baseUrl,
  
        paths: {
          'mocha': '../node_modules/mocha/mocha',
          'chai': '../node_modules/chai/chai',
          'knockout': '../node_modules/knockout/build/output/knockout-latest',
          'jquery': '../node_modules/jquery/dist/jquery.min',
          'ojs': '../node_modules/@oracle/oraclejet/dist/js/libs/oj/min/oj'
        },
          deps: allTestFiles,
          callback: window.__karma__.start
    });
  })();

Thanks in advance. Tagging @alexgorbatchev @oncletom @xdissent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests