我正在尝试从quickstart编写Angular 2应用程序的测试用例。如何更改spec文件的路径
每当我运行代码时,spec文件总是生成在同一个文件夹中,但我想移动到自定义测试文件夹。我正在使用SystemJS。例如,app.component.spec.js
应移动到testing
文件夹。
我正在尝试从quickstart编写Angular 2应用程序的测试用例。如何更改spec文件的路径
每当我运行代码时,spec文件总是生成在同一个文件夹中,但我想移动到自定义测试文件夹。我正在使用SystemJS。例如,app.component.spec.js
应移动到testing
文件夹。
好的,所以我有我的测试文件在/test
,以及karma.conf.js
和karma-test-shim.js
。
我的目录结构是这样的:
我的项目
| --app
| --node_modules
| --test
.. | --app.component.spec.ts
.. | --karma.conf.js
.. | --karma试验shim.js
这里是我的karma.conf.js文件看起来像:
module.exports = function(config) {
var appBase = 'app/'; // transpiled app JS and map files
var appSrcBase = 'app/'; // app source TS files
var appAssets = '/base/app/'; // component assets fetched by Angular's compiler
var testBase = 'test/'; // transpiled test JS and map files
var testSrcBase = 'test/'; // test source TS files
config.set({
basePath: '../',
frameworks: ['jasmine'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'), // click "Debug" in browser to see it
require('karma-htmlfile-reporter') // crashing w/ strange socket error
],
customLaunchers: {
// From the CLI. Not used here but interesting
// chrome setup for travis CI using chromium
Chrome_travis_ci: {
base: 'Chrome',
flags: ['--no-sandbox']
}
},
files: [
// System.js for module loading
'node_modules/systemjs/dist/system.src.js',
// Polyfills
'node_modules/core-js/client/shim.js',
'node_modules/reflect-metadata/Reflect.js',
// zone.js
'node_modules/zone.js/dist/zone.js',
'node_modules/zone.js/dist/long-stack-trace-zone.js',
'node_modules/zone.js/dist/proxy.js',
'node_modules/zone.js/dist/sync-test.js',
'node_modules/zone.js/dist/jasmine-patch.js',
'node_modules/zone.js/dist/async-test.js',
'node_modules/zone.js/dist/fake-async-test.js',
// RxJs
{ pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false },
{ pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false },
// Paths loaded via module imports:
// Angular itself
{pattern: 'node_modules/@angular/**/*.js', included: false, watched: false},
{pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false},
{pattern: 'systemjs.config.js', included: false, watched: false},
{pattern: 'systemjs.config.extras.js', included: false, watched: false},
'test/karma-test-shim.js',
// transpiled application & spec code paths loaded via module imports
{pattern: appBase + '**/*.js', included: false, watched: true},
{pattern: testBase + '**/*.js', included: false, watched: true},
// Asset (HTML & CSS) paths loaded via Angular's component compiler
// (these paths need to be rewritten, see proxies section)
{pattern: appBase + '**/*.html', included: false, watched: true},
{pattern: appBase + '**/*.css', included: false, watched: true},
// Paths for debugging with source maps in dev tools
{pattern: appSrcBase + '**/*.ts', included: false, watched: false},
{pattern: appBase + '**/*.js.map', included: false, watched: false},
{pattern: testSrcBase + '**/*.ts', included: false, watched: false},
{pattern: testBase + '**/*.js.map', included: false, watched: false}
],
// Proxied base paths for loading assets
proxies: {
// required for component assets fetched by Angular's compiler
"/app/": appAssets
},
exclude: [],
preprocessors: {},
// disabled HtmlReporter; suddenly crashing w/ strange socket error
reporters: ['progress', 'kjhtml'],//'html'],
// HtmlReporter configuration
htmlReporter: {
// Open this file to see results in browser
outputFile: '_test-output/tests.html',
// Optional
pageTitle: 'Unit Tests',
subPageTitle: __dirname
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
})
}
里面karma-test-shim.js
,我做了一个线上的变化:
var builtPath = '/base/test/';
编辑: 按照docs,谷歌建议把规范的测试文件在同一文件夹中您的组件,有以下原因:
- 这种测试很容易找到
- 您一看便知,如果我们的应用程序的一部分,缺乏测试。
- 附近的测试可以揭示一个部分如何在上下文中工作。
- 当你移动源码(不可避免)时,你记得移动
的测试。- 当您重命名源文件(不可避免)时,您还记得
以重命名测试文件。
这是工作jon,感谢所需的指针。 – fruitjs
你看过你的'karma.conf.js'吗?你为什么不想使用这个约定? – jonrsharpe
我看过这个 var testBase ='testing /'; //测试JS和映射文件 var testSrcBase ='testing /'; //测试源TS文件 如何在测试文件夹中移动这些specs.js文件? 它给了我警告 app/**/* .css“与任何文件都不匹配 testing/**/*。js.map”与任何文件都不匹配。 – fruitjs
这个警告只是因为你没有.js.map文件和.css文件,这就是全部;) – Supamiu