3

How do I get the stripe script loaded so Stripe is defined during my Teaspoon-Jasmine testing.

Error:

Failure/Error: ReferenceError: Stripe is not defined

Teaspoon Tests:

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();
      });

    });
  });
});
user2012677
  • 5,465
  • 6
  • 51
  • 113

1 Answers1

1

There's probably an asynchronous period after getScript runs but before the script is loaded and a Stripe object exists on the page.

Mocha supports asynchronous callbacks, so give that a try, something like this:

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

Recent versions of Mocha support returning promises directly, so you might also be able to do it that way.

Factor Mystic
  • 26,279
  • 16
  • 79
  • 95