Jasmine Spec Helpers 未加载
Jasmine Spec Helpers not loaded
我正在尝试在 Typescript 环境中使用 Jasmine 和 Karma 编写我的单元测试。
我已经安装了 karma、karma-typescript、karma-jasmine、jasmine 和 jasmine-ts。
我已将自定义 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/"
}
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/**/*"
]
}
tsconfig.json
在 spec 文件夹中:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"typeRoots": [
"../node_modules/@types",
"typings"
]
},
"include": [
"./**/*",
"../src/**/*"
]
}
spec/helpers/chai.ts
是 karma 未执行的规范助手。
该文件的内容是:
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 以获取独立示例。
目前唯一可行的选择是将助手存储在单独的 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"
]
}
},
所以这里的问题是 Karma 配置有一个很容易被忽略的小错误配置。
Karma 配置的文件节点完全有效,匹配您希望 Karma 加载的所有测试规范。
karma 配置的 karmaTypescriptConfig 节点对 Karma 加载的文件应用了额外的过滤器,在您的情况下,您只包括 karma 加载的与 .spec.ts 文件匹配的文件。
捆绑器无法找到帮助规范,因为它们与正则表达式模式不匹配:.spec.ts,即使它们是在文件节点下指定的。因此,辅助脚本在加载后被排除在测试之外。
解决这个问题的方法是删除 karmaTypescriptConfig 节点,重新配置它以显式匹配助手,或者重命名助手以匹配给定的匹配器。
我已将其从以下示例中删除。默认情况下,打包器会打包 Karma 加载的所有文件。
官方文档说明
karmaTypescriptConfig.bundlerOptions.entrypoints - A regex filtering which files loaded by Karma should be executed in a test run, for example only filenames ending with ".spec.ts": /.spec.ts$/.
This setting can be used to make sure the specs have finished setting up the test environment before other code starts requiring modules, which otherwise could lead to subtle bugs caused by race conditions.
Defaults to all files, /.*/.
希望对您有所帮助!
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
})
}
我正在尝试在 Typescript 环境中使用 Jasmine 和 Karma 编写我的单元测试。
我已经安装了 karma、karma-typescript、karma-jasmine、jasmine 和 jasmine-ts。
我已将自定义 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/"
}
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/**/*"
]
}
tsconfig.json
在 spec 文件夹中:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"typeRoots": [
"../node_modules/@types",
"typings"
]
},
"include": [
"./**/*",
"../src/**/*"
]
}
spec/helpers/chai.ts
是 karma 未执行的规范助手。
该文件的内容是:
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 以获取独立示例。
目前唯一可行的选择是将助手存储在单独的 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"
]
}
},
所以这里的问题是 Karma 配置有一个很容易被忽略的小错误配置。
Karma 配置的文件节点完全有效,匹配您希望 Karma 加载的所有测试规范。
karma 配置的 karmaTypescriptConfig 节点对 Karma 加载的文件应用了额外的过滤器,在您的情况下,您只包括 karma 加载的与 .spec.ts 文件匹配的文件。
捆绑器无法找到帮助规范,因为它们与正则表达式模式不匹配:.spec.ts,即使它们是在文件节点下指定的。因此,辅助脚本在加载后被排除在测试之外。
解决这个问题的方法是删除 karmaTypescriptConfig 节点,重新配置它以显式匹配助手,或者重命名助手以匹配给定的匹配器。
我已将其从以下示例中删除。默认情况下,打包器会打包 Karma 加载的所有文件。
官方文档说明
karmaTypescriptConfig.bundlerOptions.entrypoints - A regex filtering which files loaded by Karma should be executed in a test run, for example only filenames ending with ".spec.ts": /.spec.ts$/. This setting can be used to make sure the specs have finished setting up the test environment before other code starts requiring modules, which otherwise could lead to subtle bugs caused by race conditions. Defaults to all files, /.*/.
希望对您有所帮助!
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
})
}