首页 > 解决方案 > 未捕获的错误:[$injector:nomod] in karma test afterAll

问题描述

我在它自己的文件中有以下代码:

angular.module('app').config(['$httpProvider', function($httpProvider) {
    if (window.localStorage.getItem('cmx.use-json-server') === 'true') {
        const blacklist = window.localStorage.getItem('cmx.json-server-blacklist');
        const JSONServerBlacklist = blacklist ? JSON.parse(blacklist) : [];
        const JSON_SERVER_HOST = 'http://localhost:3000';
        const extraMessage = JSONServerBlacklist.length > 0 ? `except for the following: ${JSONServerBlacklist.join(', ')}` : '';
        console.warn(
            `WARNING: you are using JSON server. All requests are being routed to ${JSON_SERVER_HOST} \n${extraMessage}`
        );
        $httpProvider.interceptors.push(['$q', function($q) {
            return {
                request: function (config) {
                    const url = config.url;
                    // ignore template requests
                    if (url.includes('.html') || JSONServerBlacklist.includes(url)) {
                        return config || $q.when(config);
                    }
                    config.url = JSON_SERVER_HOST + config.url;
                    return config || $q.when(config);
                }
            };
        }]);
    }
}]);

此代码工作正常,并且未在测试中,但是当我在 Karma 中运行 Jasmine 测试时,文件的存在会导致以下错误:

An error was thrown in afterAll
  Uncaught Error: [$injector:nomod] 
http://errors.angularjs.org/1.7.5/$injector/nomod?p0=app

当我将未修改的块剪切并越过.config()另一个 js 文件时,这个问题就消失了。是什么导致了这里的问题?从错误消息中可以看出该angular.module('app')部件有问题,但这也是从不会导致任何问题的现有文件中粘贴的。

标签: angularjskarma-jasmine

解决方案


我不完全清楚为什么,但我能够通过更改使其成为新模块来解决问题:

angular.module('app')

到:

angular.module('JSONServerInterceptor', [])

我不完全清楚为什么这解决了我的问题的原因是我的项目有多个其他文件angular.module('app')与我上面的代码中使用的相同,我什至尝试将我的代码粘贴到其中一个文件中 - 这仍然导致测试错误。


推荐阅读