1
私はmomentJSを使用するAngular(angular2)サービスをテストしようとしていますが、ブラウザに瞬間があるので、私は参照エラーを取得し続けます。ユニットテストでカルマ/ジャスミンに瞬間を使う方法を教えてもらえますか?ここで私が得るエラーは次のとおりです。momentを使用するangular2サービスをテストするにはどうすればよいですか?
ここReferenceError: Can't find variable: moment in karma-test-shim.js (line 33814)
は私のテストです:ここでは
describe('isDateBeforeToday',() => {
it('should return true if date is before today',() => {
let past = new Date(2016, 1, 1);
expect(UnitService.isDateBeforeToday(past)).toEqual(true);
past = new Date();
past.setDate(past.getDate() - 1);
past.setHours(23);
expect(UnitService.isDateBeforeToday(past)).toEqual(true);
});
it('should return false if date is today',() => {
let today = new Date();
expect(UnitService.isDateBeforeToday(today)).toEqual(false);
});
it('should return false if date is in future',() => {
let future = new Date();
future.setDate(future.getDate() + 1);
expect(UnitService.isDateBeforeToday(future)).toEqual(false);
});
});
は、私がテストしようとしている機能である:
static isDateBeforeToday(date: Date) {
let momentDate = moment(date);
let now = moment();
return (momentDate.diff(now, 'days') < 0);
}
カルマ・テスト・シム。 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');
require('moment');
var appContext = require.context('../__tests__', 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());
karma.conf.js :
var webpackConfig = require('./webpack.test');
module.exports = function (config) {
var _config = {
basePath: '',
browserNoActivityTimeout: 20000, // had to increase this for component unit tests to work
frameworks: ['jasmine'],
files: [
{pattern: './karma-test-shim.js', watched: false}
],
preprocessors: {
'./karma-test-shim.js': ['webpack', 'sourcemap']
},
webpack: webpackConfig,
webpackMiddleware: {
stats: 'errors-only'
},
webpackServer: {
noInfo: true
},
reporters: ['progress','kjhtml'], //leave coverage reporter in for now, even though coverage not working
coverageReporter: {
type: 'html',
dir: '../coverage/'
},
browsers: ['phantomJS_without_security'],
customLaunchers: {
chrome_without_security: {
base: 'Chrome',
flags: ['--disable-web-security']
},
phantomJS_without_security: {
base:'PhantomJS',
options: {
windowName: 'Portal-tests',
settings: {
webSecurityEnabled: false
}
}
}
},
phantomjsLauncher: {
// Have phantomjs exit if a ResourceError is encountered (useful if karma exits without killing phantom)
exitOnResourceError: true
},
client: {
captureConsole: true
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
singleRun: true
};
config.set(_config);
};