Issue with uncaught exceptions in promise rejection test

I am experiencing an issue with the following code:

function foo(){
  bar().catch(function(){
    //do stuff
  }
}

function bar(){
  return promiseReturner().then(
    function(result){
      if(_.isEmpty(result)){
        throw "Result is empty";
      }
    }
  )
}

My goal is to test that the //do stuff block is executed when result is empty:

deferred.resolve(null);
foo();
$rootScope.$apply();

Although the throw block is triggered, it seems that the catch function does not catch it. Strangely, this behavior only occurs in the testing environment, as outside of tests it works correctly.

Why is the catch block not being triggered in my test code?

Answer №1

It seems like there may be some uncertainty regarding the type of promise implementation being used and the specific context. However, a potential solution could involve simply adding a return statement to the foo function as shown below:

function foo(){
  return bar().catch(function(){
    //do stuff
  });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Images obscure dropdown menu

The issue I am experiencing with my blog is that the dropdown menu is appearing behind the image slider on the main page. You can see it here: Can anyone offer guidance on how to prevent this from happening so that the dropdown menu is not obscured? Just ...

Dealing with an unspecified parameter can be tricky - here's how you

Currently, I am in the process of developing an angular application. In this project, there is a specific scenario that needs to be handled where a parameter is undefined. Here's a snippet of my code: myImage() { console.log('test') ...

Tips on setting the default sorting order in AngularJS

I have implemented a custom order function in my controller with the following code: $scope.customOrder = function (item) { var empStatus = item.empState; switch (empStatus) { case 'Working': return 1; case & ...

Managing JWT tokens for secured pages using Sails.js

In an effort to enhance security in my system, I decided to implement JWT token authentication. For the front-end, I opted for statellizer. Once a user logs into the system, a token is generated like this: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI ...

Error: Kinetic.js cannot upload image to canvas

There must be something simple that I'm missing here. I've checked my code line by line, but for some reason, the image just won't load. var displayImage = function(){ var stage = new Kinetic.Stage("imgarea", 250, 256); var layer = new ...

Can you explain the purpose and function of stub.callsArg(index) feature in Sinon.JS?

Confusion has set in as I try to make sense of this. According to the documentation: stub.callsArg(index) - This command prompts the stub to execute the callback function found at the specified index. For instance, using stub.callsArg(0); will trigger the ...

What could be the underlying reason behind this error message: TypeError - Unable to access property 'key' as it is undefined

Encountering this error type is common in React, Angular, and React-Native. I have come across numerous questions about this error, but I am wondering under what circumstances the console actually throws this error? Edit: Could someone please provide an e ...

remove a specific element from an array

Hey there! I'm attempting to remove only the keys from an array. Here's the initial array: {everyone: "everyone", random: "random", fast response time: "fast response time", less conversations: "less conversatio ...

Setting up Vagrant with Xvfb Troubleshooting

Setting up a vagrant machine with Ubuntu 14.04 to conduct tests using Selenium and Protractor. The goal is to execute all tests on this headless machine without the need for a graphical interface, just plain terminal output. Following these resources for ...

Angular URL containing dynamic path parameters

I am looking to update the Angular URL to /jobs/id. I have written the code below, but I'm unsure if it will work: $location.path("/JobHire/jobs/"+response.data.id); How should I set up the route configuration? Currently, I have it configured like t ...

What are some methods to activate a scope watch within a directive?

I'm attempting to activate my directive code using a scopewatcher: var gMaps = function($timeout, $q, GeoCoder, mockdataService) { return { restrict: 'EA', template: '<div class="gmaps"></div>', ...

What is the best way to display only the host and pathname of the links generated by Array.map()?

I am currently utilizing Array.map() to display a list of files in cache: <div data-offline></div> <script> var version = 'v1:'; if (navigator && navigator.serviceWorker) { caches.open(versio ...

What is the most effective method for comparing two API requests in AngularJS?

I have a frontend built with AngularJS for my API-driven application. The services within the application are responsible for making API calls, like the ones shown below. My goal is to compare variables retrieved from these calls: Retrieve user data: thi ...

Arranging Data in a Table using Tabs and JavaScript

I am seeking assistance with a table I created that has multiple tabs containing different data. Each tab displays different information within the table rows, including a column for the number of votes. I am looking to automatically sort the rows based on ...

Automatically forward to m.example.com on mobile devices using nodejs

Is there a way to create a subdomain in Node.js, such as m.example.com, and have it redirect to m.example.com on mobile devices? I've searched for answers but haven't found a satisfactory solution. One suggestion is to use nginx in front of Node, ...

What is the best way to combine two arrays while ensuring the elements of the second array appear before the elements of the first

Is there a way to concatenate two arrays in JavaScript, where the second array appears before the first? For example: const arr1 = [1, 2, 3]; const arr2 = [4, 5, 6]; const combinedArr = arr1.concat(arr2); console.log(combinedArr); I am aware that yo ...

Using Django to Display a Line Chart with Data from a Dictionary in a Template

I have a dictionary that was generated in the views.py file. The structure of the dictionary is as follows, but it contains data for an unspecified number of enterprises: { enterprise1: {'Year': ['2014/2015', '2016/2017', &ap ...

Executing a npm script (script.js) with custom arguments - a step-by-step guide

I'm considering creating a setup similar to lodash custom builds. Basically, I want to allow users to input a command like: lodash category=collection,function This would create a custom module with the specified category. I've been looking in ...

Experiencing issues with creating HTML using JavaScript?

I'm a JavaScript novice and struggling to figure out what's wrong with my code. Here is the snippet: var postCount = 0; function generatePost(title, time, text) { var div = document.createElement("div"); div.className = "content"; d ...

Why is the Slick Slider displaying previous and next buttons instead of icons?

I'm currently working on creating a slider using the slick slider. Everything seems to be functioning properly, but instead of displaying arrows on the sides, it's showing Previous and Next buttons stacked vertically on the left side. It appears ...