RequireJS not loading modules properly using karma and typescript

倖福魔咒の 提交于 2019-12-05 13:00:27

It is not a TypeScript problem. We encountered the same problem. Turns out that karma "window.__karma__.files" array includes all files included in the test, including the .js extenstion.

Now requireJS does not work when supplying the .js extension. To fix it, in our main-test.js file, we created a variable "tests" by filtering all the *Spec.js files and then we removed the .js from the file name as requireJS needs it to be. More information here: http://karma-runner.github.io/0.8/plus/RequireJS.html

Below is how we did it (based on the info supplied in the link above):

main-test.js

console.log('===========================================')
console.log('=================TEST FILES================')


var tests = Object.keys(window.__karma__.files).filter(function (file) {
    return /\Spec\.js$/.test(file);
}).map(function (file) {
    console.log(file);
    return file.replace(/^\/base\/|\.js$/g, '');
});

console.log('===========================================')
console.log('===========================================')


require.config({
    baseUrl:'/base',
    paths: {
        jquery      :["http://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min", "lib/jquery"],
        angular     : ["https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.14/angular.min", "lib/angular"],
        angularMocks: 'app/vendors/bower_components/angular-mocks/angular-mocks',
    },
    shim: {
        'angularMocks': {
            deps: ['angular'],
            exports: 'angular.mock'
        }
    },
    deps: tests,
    callback: window.__karma__.start
});

Also make sure you have supplied the files to be tested in your karma.config.js file, more details here: http://karma-runner.github.io/0.8/plus/RequireJS.html same as the link above.

Hope it helps

It turns out it is trying to load the module without the .js postfix,

That is the perhaps not the actual source of the error. Actually it is looking at /base/src/app/domain/core/Collections and not app/domain/core/Collections (as in your manual type unsafe way). Notice base/src/ that shouldn't be there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!