没有装载Jasmine Spec Helpers

时间:2017-09-15 18:01:48

标签: typescript jasmine karma-runner karma-jasmine

我正在尝试在Typescript环境中使用Jasmine和Karma编写单元测试。

我已经安装了业力,业力 - 打字稿,业力 - 茉莉,茉莉和茉莉花。

我已将自定义tsconfig.json添加到spec目录,并在karma-typescript设置中使用它。

通常,我的测试正在运行,但是,它不执行我的规范助手。

我是否缺少执行规格助手的事项?

供您参考,这是我的配置:

karma.conf.js

    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', "karma-typescript"],
    
    
            // list of files / patterns to load in the browser
            files: [
                "spec/helpers/chai.ts",
                {pattern: "src/**/*.ts"},
                {pattern: "spec/**/*.ts"}
            ],
    
    
            // 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: {
                "src/**/*.ts": ["karma-typescript"],
                "spec/**/*.ts": ["karma-typescript"]
            },
    
            karmaTypescriptConfig: {
                bundlerOptions: {
                    entrypoints: /\.spec\.ts$/
                },
                tsconfig: "./spec/tsconfig.json",
                coverageOptions: {
                    exclude: [/\.(d|spec|test)\.tsx?/, /\/spec\//]
                }
            },
            specReporter: {
                maxLogLines: 3,             // limit number of lines logged per test
                suppressErrorSummary: true, // do not print error summary
                suppressFailed: false,      // do not print information about failed tests
                suppressPassed: false,      // do not print information about passed tests
                suppressSkipped: true,      // do not print information about skipped tests
                showSpecTiming: false,      // print the time elapsed for each spec
                failFast: false              // test would finish with error when a first fail occurs.
            },
            // test results reporter to use
            // possible values: 'dots', 'progress'
            // available reporters: https://npmjs.org/browse/keyword/karma-reporter
            reporters: ['spec', "karma-typescript", "kjhtml"],
    
    
            // 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: true,
    
    
            // 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
        })
    }

jasmine.json(虽然我觉得它没有被使用):

    {
      "spec_dir": "spec",
      "spec_files": [
        "**/*[sS]pec.ts"
      ],
      "helpers": [
        "helpers/**/*.ts"
      ],
      "stopSpecOnExpectationFailure": false,
      "random": false,
      "reporters": [
        {
          "name": "jasmine-spec-reporter#SpecReporter"
        }
      ],
      "project": "./spec/"
    }

root中的

tsconfig.json

    {
      "compilerOptions": {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": [
          "es6",
          "dom",
          "es2015.promise"
        ],
        "module": "commonjs",
        "target": "es5",
        "sourceMap": true,
        "outDir": "./dist/",
        "noImplicitAny": false,
        "allowJs": true,
        "baseUrl": "src",
        "typeRoots": [
          "node_modules/@types",
          "typings"
        ]
      },
      "include": [
        "src/**/*"
      ]
    }

spec文件夹中的

tsconfig.json

    {
      "extends": "../tsconfig.json",
      "compilerOptions": {
        "typeRoots": [
          "../node_modules/@types",
          "typings"
        ]
      },
      "include": [
        "./**/*",
        "../src/**/*"
      ]
    }

spec/helpers/chai.ts是业力未执行的规范助手。

该文件的内容为:

import * as chai from "chai";
import chaiThings = require("chai-things");
import chaiInterface = require("chai-interface");

chai.should();
chai.use(chaiThings);
chai.use(chaiInterface);

请参阅https://github.com/dhilgarth/mjt了解自包含的示例。

2 个答案:

答案 0 :(得分:3)

所以这里的问题是Karma配置有一个轻微错误配置,很容易错过。

Karma配置的文件节点完全有效,并且匹配您希望Karma加载的所有测试规范。

karma配置的karmaTypescriptConfig节点对由Karma加载的文件应用额外的过滤器,在您的情况下,您只包括由karma加载的匹配.spec.ts文件的文件。

捆绑器无法找到辅助规范,因为它们与正则表达式模式不匹配:.spec.ts,即使它们已在files节点下指定。因此,帮助脚本在加载后被排除在测试之外。

对此的修复是删除karmaTypescriptConfig节点,重新配置它以显式匹配帮助程序,或者重命名帮助程序以匹配给定的匹配器。

我已从以下示例中删除了它。捆绑器默认捆绑由Karma加载的所有文件。

官方文件说明

  

karmaTypescriptConfig.bundlerOptions.entrypoints - 由Karma加载的文件的正则表达式过滤应该在测试运行中执行,例如只有以" .spec.ts":/.spec.ts$/结尾的文件名。   此设置可用于确保规范在其他代码开始需要模块之前已完成设置测试环境,否则可能导致由竞争条件引起的细微错误。   默认为所有文件,/。* /。

希望这有帮助!

karma.conf.js



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', "karma-typescript"],

		// list of files / patterns to load in the browser
		files: [
			"spec/helpers/helper.ts", {
				pattern: "src/**/*.ts"
			}, {
				pattern: "spec/**/*.ts"
			}
		],

		client: {
			clearContext: false // leave Jasmine Spec Runner output visible in browser
		},

		// 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: {
			"spec/helpers/helper.ts": ["karma-typescript"],
			"src/**/*.ts": ["karma-typescript"],
			"spec/**/*.ts": ["karma-typescript"]
		},

		// test results reporter to use
		// possible values: 'dots', 'progress'
		// available reporters: https://npmjs.org/browse/keyword/karma-reporter
		reporters: ['progress', "karma-typescript"],

		// 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: true,

		// 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
	})
}




答案 1 :(得分:1)

目前唯一可行的选择是将助手存储在单独的forlder中,使用tsc编译它们并将.js文件添加到karma.config

检查此PR以了解有效的步骤:https://github.com/dhilgarth/mjt/pull/1/files

    files: [
        {pattern: "spec/helpers/*.js"},
        {pattern: "src/**/*.ts"},
        {pattern: "spec/**/*.ts"}
    ],

    client:{
      jasmine: {
        helpers: [
          "spec/helpers/*.js"
        ]
      }
    },