加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

typescript – 在/node_modules/angular2/src/testing/matchers.

发布时间:2020-12-17 17:41:34 所属栏目:安全 来源:网络整理
导读:我想用业力来测试我的angular2应用程序,但是我得到了一个打字稿错误: /my/path/node_modules/angular2/src/testing/matchers.d.ts Error:(4,37) TS2503: Cannot find namespace ‘jasmine’. 安装了NPM模块,typescript编译器运行良好. 怎么了? myservice.s
我想用业力来测试我的angular2应用程序,但是我得到了一个打字稿错误:

/my/path/node_modules/angular2/src/testing/matchers.d.ts

Error:(4,37) TS2503: Cannot find namespace ‘jasmine’.

安装了NPM模块,typescript编译器运行良好.
怎么了?

myservice.serviceSpec.ts:

import {it,describe,expect,beforeEach,inject} from 
'angular2/testing';
import {myService} from "../app/myservice.service";

describe('Tests',() => {
    it('should be true',() => {
        expect(true).toBe(true);
    });
});

的package.json:

{
  "name": "myapp","version": "0.1.0","dependencies": {
    "angular2": "^2.0.0-beta.2","es6-promise": "^3.0.2","es6-shim": "^0.33.13","reflect-metadata": "^0.1.2","rxjs": "^5.0.0-beta.0","systemjs": "^0.19.17","zone.js": "^0.5.10"
  },"devDependencies": {
    "jasmine-core": "^2.4.1","karma": "^0.13.19","karma-chrome-launcher": "^0.2.2","karma-coverage": "^0.5.3","karma-jasmine": "^0.3.7","remap-istanbul": "^0.5.1"
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "ES5","module": "system","moduleResolution": "node","sourceMap": true,"emitDecoratorMetadata": true,"experimentalDecorators": true,"removeComments": false,"noImplicitAny": true
  },"exclude": [
    "node_modules"
  ]
}

karma.conf.js:

// Karma configuration
// Generated on Wed Feb 10 2016 09:56:19 GMT+0100 (CET)

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

        // base path that will be used to resolve all patterns (eg. files,exclude)
        basePath: '',// frameworks to use
        // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
        frameworks: ['jasmine'],// list of files / patterns to load in the browser
        files: [
            'node_modules/systemjs/dist/system.src.js','node_modules/angular2/bundles/http.dev.js','test/karma_test_shim.js','test/myservice.serviceSpec.js',{pattern: 'app/**/*.js',included: false,watched: true},{pattern: 'test/**/*Spec.js',watched: true}
        ],// list of files to exclude
        exclude: [],// preprocess matching files before serving them to the browser
        // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
        preprocessors: {},// test results reporter to use
        // possible values: 'dots','progress'
        // available reporters: https://npmjs.org/browse/keyword/karma-reporter
        reporters: ['progress'],// 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: false,// start these browsers
        // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
        browsers: ['Chrome'],// Continuous Integration mode
        // if true,Karma captures browsers,runs the tests and exits
        singleRun: false,// Concurrency level
        // how many browser should be started simultaneous
        concurrency: Infinity
    })
}

karma_test-shim.js:

// Cancel Karma's synchronous start,// we will call `__karma__.start()` later,once all the specs are loaded.
__karma__.loaded = function() {};
System.config({
  packages: {
    'base/app': {
      defaultExtension: false,format: 'register',map: Object.keys(window.__karma__.files).
            filter(onlyAppFiles).
            reduce(function createPathRecords(pathsMapping,appPath) {
              // creates local module name mapping to global path with karma's fingerprint in path,e.g.:
              // './hero.service': '/base/src/app/hero.service.js?f4523daf879cfb7310ef6242682ccf10b2041b3e'
              var moduleName = appPath.replace(/^/base/app//,'./').replace(/.js$/,'');
              pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath]
              return pathsMapping;
            },{})
    }
  }
});
System.import('angular2/testing').then(function(testing) {
  return System.import('angular2/platform/testing/browser').then(function(testing_platform_browser) {
    testing.setBaseTestProviders(testing_platform_browser.TEST_BROWSER_PLATFORM_PROVIDERS,testing_platform_browser.TEST_BROWSER_APPLICATION_PROVIDERS);
  });
}).then(function() {
  return Promise.all(
    Object.keys(window.__karma__.files) // All files served by Karma.
    .filter(onlySpecFiles)
    .map(function(moduleName) {
      // loads all spec files via their global module names
      return System.import(moduleName);
  }));
}).then(function() {
  __karma__.start();
},function(error) {
  __karma__.error(error.stack || error);
});
function onlyAppFiles(filePath) {
  return /^/base/app/.*.js$/.test(filePath)
}
function onlySpecFiles(path) {
  return /Spec.js$/.test(path);
}

解决方法

编译器需要Jasmine声明文件jasmine.d.ts来声明Jasmine的函数.您可以从 here下载并在TypeScript中引用它:

/// <reference path="jasmine.d.ts"/>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读