typescript - 在/node_modules/angular2/src/testing/matchers.d.ts 中找不到命名空间 Jasmine

标签 typescript jasmine angular karma-jasmine

我想用 karma 测试我的 angular2 应用程序,但我收到一个 typescript 错误:

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

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

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

我的服务.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', included: false, 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"/>

关于typescript - 在/node_modules/angular2/src/testing/matchers.d.ts 中找不到命名空间 Jasmine ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35316982/

相关文章:

node.js - NodeJS Azure Functions - 从同一存储库的不同文件夹嵌套级别导出函数

javascript - HTML5 必需属性在使用 CLI 安装的 Angular 2 项目中不起作用

Angular Material 日期选择器限制范围选择

javascript - 如何扩展接口(interface)并覆盖属性的类型?

reactjs - this.state 在方法中未定义

angular - 在 TestBed 模块中创建时,NGRX 和 NGXS 会创建单独的 Store 实例吗?

angular 2 - 具有 injectionToken 依赖性的模拟组件

javascript - Protractor :如何获取当前的浏览器宽度?

node.js - 找不到 'node' 的类型定义文件

typescript - 在 typescript 类型中对对象键名称施加一些模式