无法运行测试(茉莉,业力,网络包)

时间:2018-04-24 00:14:27

标签: unit-testing webpack jasmine karma-jasmine

我已经尝试了几天才能进行一些测试。

我有两个环境,一个非常“空”,另一个环境是完整项目。 'empty'确实运行了一个非常简单的测试(但不包含无法在整个项目中构造的Person对象)。

我将整个项目迁移到一个非常相似的环境,包括已安装的开发包,业力,茉莉花版本等。我还复制了tsconfig.json,karma.conf文件。那些在下面。

对于我的生活,我无法弄清楚为什么业力测试不会运行。 使用以前版本的Karma,我会得到一个错误,就像' scheduler_person .b'不是构造函数。

深入研究一下,在这行test.store.ts上失败了:

let neil: Person = new Person("Neil Clayton");

该对象的构造函数是:

constructor(name: string = "put name here") {
    super();
    this.name = name;
    this.primary_roles = new Map<Role, number>();
    this.specific_roles = new Map<string, Array<Role>>();
    this.secondary_action_list = [];
    this.condition_rules = [];
    this.unavailable = [];
    this.prefs = new SchedulePrefs();
}

因此,Person类有一个构造函数,这个代码确实在app本身内运行。

我已经升级了所有业力/茉莉花,现在错误变为: VM105 karma-test-shim.js:629 Uncaught TypeError:$ f.b不是构造函数

查看生成的代码,它实际上与调用构造函数完全相同。

我觉得我必须做一些非常简单,非常愚蠢的事情,但我看不到它。 有什么想法吗?

(顺便说一句:这个项目在https://github.com/scornflake/scheduler

tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "noImplicitThis": true,
    "lib": [
      "dom",
      "es2015",
      "esnext.asynciterable"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "src/**/*.spec.ts"
  ],
  "typeAcquisition": {
    "enable": true,
    "include": [
      "./src/common/fix.broken.gapi.types.d.ts"
    ]
  },
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

karma.config.json

const webpackConfig = require('./webpack.test.js');

module.exports = function (config) {
    const _config = {
        basePath: '',

        frameworks: [
            'jasmine'
            // 'jasmine-matchers'
        ],

        files: [
            {
                pattern: './karma-test-shim.js',
                watched: true
            }
        ],

        preprocessors: {
            './karma-test-shim.js': ['webpack', 'sourcemap']
        },

        webpack: webpackConfig,

        webpackMiddleware: {
            stats: 'errors-only'
        },

        webpackServer: {
            noInfo: true
        },

        browserConsoleLogOptions: {
            level: 'log',
            format: '%b %T: %m',
            terminal: true
        },

        // reporters: ['kjhtml', 'dots'],
        reporters: ['dots'],
        // reporters: ['kjhtml'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['Chrome'],
        singleRun: false
    };

    config.set(_config);
};

果报测试shim.js

Error.stackTraceLimit = Infinity;

require('core-js/es6');
require('core-js/es7/reflect');

require('zone.js/dist/zone');
require('zone.js/dist/long-stack-trace-zone');
require('zone.js/dist/proxy');
require('zone.js/dist/sync-test');
require('zone.js/dist/jasmine-patch');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');

var appContext = require.context('../src', true, /\.spec\.ts/);
appContext.keys().forEach(appContext);

var testing = require('@angular/core/testing');
var browser = require('@angular/platform-browser-dynamic/testing');

testing.TestBed.initTestEnvironment(browser.BrowserDynamicTestingModule, browser.platformBrowserDynamicTesting());

的package.json

{
  "name": "scheduler",
  "version": "0.0.1",
  "author": "Neil Clayton",
  "homepage": "http://ionicframework.com/",
  "private": true,
  "scripts": {
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build --copy ./config/copy.config.js",
    "lint": "ionic-app-scripts lint",
    "ionic:build": "ionic-app-scripts build --copy ./config/copy.config.js",
    "ionic:serve": "ionic-app-scripts serve --copy ./config/copy.config.js",
    "test-coverage": "ng test --code-coverage",
    "test": "ng test"
  },
  "dependencies": {
    "@angular/common": "5.0.3",
    "@angular/compiler": "5.0.3",
    "@angular/compiler-cli": "5.0.3",
    "@angular/core": "5.0.3",
    "@angular/forms": "5.0.3",
    "@angular/http": "5.0.3",
    "@angular/platform-browser": "5.0.3",
    "@angular/platform-browser-dynamic": "5.0.3",
    "@ionic-native/core": "4.3.2",
    "@ionic-native/splash-screen": "4.3.2",
    "@ionic-native/status-bar": "4.3.2",
    "@ionic/storage": "2.1.3",
    "@types/gapi": "^0.0.35",
    "@types/gapi.auth2": "^0.0.47",
    "@types/gapi.client": "^1.0.0",
    "@types/gapi.client.drive": "^3.0.1",
    "@types/gapi.client.sheets": "^4.0.0",
    "angular-pipes": "^6.5.3",
    "apollo-angular": "^1.0.1",
    "apollo-angular-link-http": "^1.0.2",
    "apollo-cache-inmemory": "^1.1.12",
    "apollo-client": "^2.2.8",
    "apollo-link": "^1.2.2",
    "cordova-browser": "5.0.1",
    "cordova-plugin-device": "^1.1.4",
    "cordova-plugin-ionic-webview": "^1.1.16",
    "cordova-plugin-splashscreen": "^4.0.3",
    "cordova-plugin-whitelist": "^1.3.1",
    "file-saver": "^1.3.8",
    "google-auth-library": "^1.4.0",
    "googleapis": "^28.1.0",
    "graphql": "^0.13.2",
    "graphql-tag": "^2.8.0",
    "ionic-angular": "3.9.2",
    "ionic-configuration-service": "^6.0.0",
    "ionic-logging-service": "^5.0.0",
    "ionic-plugin-keyboard": "^2.2.1",
    "ionicons": "3.0.0",
    "json2csv": "^3.11.5",
    "lodash": "^4.17.4",
    "mobx": "^4.1.1",
    "mobx-angular": "^3.0.1",
    "moment": "^2.20.1",
    "ng-circle-progress": "^0.9.9",
    "rxjs": "5.5.2",
    "short-unique-id": "^1.1.0",
    "sw-toolbox": "3.6.0",
    "to-case": "^2.0.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@ionic/app-scripts": "3.1.6",
    "@types/jasmine": "2.8.4",
    "@types/node": "8.5.8",
    "angular2-template-loader": "^0.6.2",
    "file-save": "^0.2.0",
    "html-loader": "^0.5.5",
    "jasmine-core": "3.1.0",
    "jasmine-expect": "^3.8.3",
    "jasmine-spec-reporter": "^4.1.0",
    "karma": "^2.0.2",
    "karma-chrome-launcher": "^2.2.0",
    "karma-jasmine": "^1.1.1",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-webpack": "^3.0.0",
    "null-loader": "^0.1.1",
    "protractor": "5.3.0",
    "ts-loader": "^4.2.0",
    "ts-node": "4.1.0",
    "typescript": "2.4.2",
    "webpack": "^4.6.0"
  },
  "description": "Scheduler",
  "cordova": {
    "plugins": {
      "ionic-plugin-keyboard": {},
      "cordova-plugin-whitelist": {},
      "cordova-plugin-device": {},
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-ionic-webview": {}
    },
    "platforms": [
      "browser"
    ]
  }
}

最后,浏览器错误的一些图片:

running from webstorm

karma stack trace

a bit more detail

1 个答案:

答案 0 :(得分:1)

src/providers/store/test.store.ts中,您需要将所有全局常量(以及后续方法调用)移动到SetupStore类的NPBCStoreConstruction方法中。当我这样做时,我可以正确运行测试套件。

问题的原因是,当Webpack / Typescript / something编译所有内容时,它会选择一个包含所有文件的任意顺序,如果查看生成的代码,您可以看到该人{{1}的全局调用构造函数在定义之前就已经发生了。