angularjs - 未捕获的错误:[$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')
部件有问题,但这也是从不会导致任何问题的现有文件中粘贴的。
解决方案
我不完全清楚为什么,但我能够通过更改使其成为新模块来解决问题:
angular.module('app')
到:
angular.module('JSONServerInterceptor', [])
我不完全清楚为什么这解决了我的问题的原因是我的项目有多个其他文件angular.module('app')
与我上面的代码中使用的相同,我什至尝试将我的代码粘贴到其中一个文件中 - 这仍然导致测试错误。
推荐阅读
- rust - 为具有反序列化特征绑定的泛型派生反序列化时无法推断类型参数的类型
- postgresql - 从 DatabaseMetaData.getIndexInfo() 中为 Postgres 发现索引“nulls first|last”
- nlp - CUDA 错误:调用 `cublasCreate(handle)` 时出现 CUBLAS_STATUS_ALLOC_FAILED
- google-cloud-platform - 与常规部署相比,使用 Bitnami 部署有什么好处?
- java - Openshift中的Spring Boot / VertX调用超时
- .net - 如果未安装 ODAC,为什么我们需要向所有调用项目添加对 Oracle.ManagedDataAccess 的引用?
- html - 提交后重置角度表单
- javascript - WebRTC - Offer-Answer 发生两次
- hibernate-search - Hibernate Search 仅添加来自 IndexedEmbedded 类的 DocumentId
- iis - 在 windows server 2012 R2 的 iis 中创建自签名证书