Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support question - why do promises magically run synchronously #2900

Closed
ORESoftware opened this issue Dec 5, 2017 · 3 comments
Closed

Support question - why do promises magically run synchronously #2900

ORESoftware opened this issue Dec 5, 2017 · 3 comments

Comments

@ORESoftware
Copy link

ORESoftware commented Dec 5, 2017

Forgive me, I am a bit of newb with Karma + Jasmine

I am sure there is an simple answer to this.

I have the following Jasmine code with Karma:

    it('should update a comment by 59dd58092889e25745b6b208', function () {

      //expectGET to make sure this is called once.
      var url = encodeURI('/api/v1/comments/by_id/59dd58092889e25745b6b208');

      httpBackend.expectPUT(url, options1.data).respond(comments1);

      console.log('uno');

      myService.updateComment(options1.data).then(function (response) {
        expect(response.value).to.equal(comments1[0].value);
        console.log('dos');
      });

      //flush the backend to "execute" the request to do the expectedGET assertion.
      httpBackend.flush();

      console.log('tres');

    });

Expected behaviour

I would expect it to log:

LOG LOG: 'uno'
LOG LOG: 'tres'
LOG LOG: 'dos'

Actual behaviour

LOG LOG: 'uno'
LOG LOG: 'dos'
LOG LOG: 'tres'

Environment Details

  • Karma version (output of karma --version):
  • Relevant part of your karma.config.js file

Steps to reproduce the behaviour

See screenshot:

screenshot 2017-12-04 19 18 37

@ORESoftware
Copy link
Author

ORESoftware commented Dec 5, 2017

yeah, I think the Karma logger will on rare occasion log things out of order, that's not good

because I run it now, and it logs it correctly.

@ORESoftware
Copy link
Author

Maybe improve the logging order, idk, will close

@johnjbarton
Copy link
Contributor

Async code in tests requires different test API.

Try

it('should update a comment by 59dd58092889e25745b6b208', function () {

      //expectGET to make sure this is called once.
      var url = encodeURI('/api/v1/comments/by_id/59dd58092889e25745b6b208');

      httpBackend.expectPUT(url, options1.data).respond(comments1);

      console.log('uno');

      var promise = myService.updateComment(options1.data).then(function (response) {
        expect(response.value).to.equal(comments1[0].value);
        console.log('dos');
      });

      //flush the backend to "execute" the request to do the expectedGET assertion.
      httpBackend.flush();

      console.log('tres');
      return promise;

or

    it('should update a comment by 59dd58092889e25745b6b208', function (done) {

      //expectGET to make sure this is called once.
      var url = encodeURI('/api/v1/comments/by_id/59dd58092889e25745b6b208');

      httpBackend.expectPUT(url, options1.data).respond(comments1);

      console.log('uno');

      myService.updateComment(options1.data).then(function (response) {
        expect(response.value).to.equal(comments1[0].value);
        console.log('dos');
        done();
      });

      //flush the backend to "execute" the request to do the expectedGET assertion.
      httpBackend.flush();

      console.log('tres');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants