3

如何加载条带脚本,以便在我的 Teaspoon-Jasmine 测试期间定义条带。

错误:

失败/错误:ReferenceError:条带未定义

茶匙测试:

describe("Stripe", function() {
  var paymentElement ;

  describe("constructor", function(){
    beforeAll(function(){
     // Tried this..

      var head = document.getElementsByTagName('head')[0];
      var jQueryScript = document.createElement('script');
      jQueryScript.setAttribute('type', 'text/javascript');
      jQueryScript.setAttribute('src', 'https://js.stripe.com/v3/');
      head.appendChild(jQueryScript);

     // also tried..

      $.getScript( "https://js.stripe.com/v3/");

      paymentElement = new Helpers.Stripe.PaymentElement(); 
    });

    describe("with defaults", function(){
     it("should define stripe", function(){
        expect(Stripe('test-token')).toBeDefined();
      });

      it("should define stripe through instance", function(){
        expect(paymentElement.stripe).toBeDefined();
      });

    });
  });
});
4

1 回答 1

1

getScript在运行之后但在加载脚本并且Stripe页面上存在对象之前可能有一个异步时间段。

Mocha 支持异步回调,所以试试看,像这样:

  describe("constructor", function() {
    before(function(done) {
      $.getScript('script url here', function() {
        done();
      });
    });
  });

最近版本的 Mocha 支持直接返回 Promise,所以你也可以这样做。

于 2019-07-08T14:49:34.440 回答