在Jasmine单元测试期间加载aurelia-validation插件-使用Webpack [英] Load aurelia-validation plugin during Jasmine unit tests - with webpack

查看:83
本文介绍了在Jasmine单元测试期间加载aurelia-validation插件-使用Webpack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Aurelia与Webpack一起使用.基于ESNext Skeleton Webpack.

I am using Aurelia with Webpack. Based on the ESNext Skeleton Webpack.

https://github.com/aurelia/skeleton-导航/树/主/骨架-esnext-webpack

我有一些普通的JS模型类,例如:

I have some plain JS model classes like:

import {ValidationRules} from 'aurelia-validation';

export class Address {
  street = '';  
}

ValidationRules
  .ensure('street').required()
  .on(Address);

运行茉莉花测试(通过Karma)并与Wallaby一起运行时,我会收到错误消息:

As soon as I run my Jasmine tests (via Karma) and also with Wallaby, I get the error:

'Message: Did you forget to add ".plugin('aurelia-validation)" to your main.js?'

好的-运行测试时我没有main.js,那么如何加载插件?

OK - I've not got a main.js when running tests, so how to load the plugin?

我已经尝试做过这样的事情-使用aurelia-testing:

I've tried doing something like this - using aurelia-testing:

import {StageComponent} from 'aurelia-testing';
import {bootstrap} from 'aurelia-bootstrapper-webpack';

... 

let component;

beforeEach(done => {
  component = StageComponent
    .withResources();

  component.bootstrap(aurelia => {
    aurelia.use.plugin('aurelia-validation')
  });
  done();
});

但这不适用于Webpack-aurelia-bootstrapper-webpack的未解决问题.也许我做错了.

But that does not work with Webpack - open issue with aurelia-bootstrapper-webpack. Or maybe I am doing it wrongly.

在测试过程中,还有其他方法可以加载验证插件吗?还是通过webpack进行aurelia测试?

Is there some other way to load the validation plugin during the tests? Or get aurelia-testing working with webpack?

目前,如果我具有验证插件或尝试使用aurelia-testing,则完全无法进行任何单元测试.

At the moment, I am completely blocked from doing any unit tests if I have the validation plugin, or attempt to use aurelia-testing.

推荐答案

我使用aurelia-cli和小袋鼠工作.你离得很近,我想这会让你更加沮丧.对我来说,秘密是,必须首先使用spec文件中的beforeAll方法引导验证插件,然后再使用beforeEach方法创建被测系统.以下规范文件对我有用,并解决了以下消息:您是否忘记向您的main.js中添加".plugin('aurelia-validation')"错误.

I have it working using the aurelia-cli and wallaby. You were very close which I guess makes it even more frustrating. The secret for me is that the validation plugin had to be bootstrapped first with the beforeAll method in the spec file and then the system under test created in the beforeEach method. The following spec file worked for me and resolved the Message: Did you forget to add ".plugin('aurelia-validation') to your main.js" error.

import { SourceSystemEntity } from '../../../src/entities/sourceSystemEntity';
import { StageComponent } from 'aurelia-testing';
import { bootstrap } from 'aurelia-bootstrapper';

describe('SourceSystem class', () => {
  let component;
  let sut: SourceSystemEntity;

  beforeAll(done => {
    component = StageComponent.withResources().inView('<div></div>').boundTo({});
    component.configure = (aurelia: Aurelia) => {
      aurelia.use
        .standardConfiguration()
        .plugin('aurelia-validation');
    };
    component.create(bootstrap).then(() => {
      done();
    });
  });

  afterAll(() => {
    component.dispose();
  });

  beforeEach(() => {
    sut = new SourceSystemEntity();
  });

  it('has Validation enabled', () => {
    expect(sut.hasValidation()).toBeTruthy();
  });

});

这篇关于在Jasmine单元测试期间加载aurelia-validation插件-使用Webpack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆