Summon an error through Jasmine

Here is a function that checks for the presence of £ and p in an input:

function checkforLandP(n){
if(n[0]=== '£' && n[n.length-1] === 'p'){
    // remove the p 
    n =n.slice(0, -1);
    // remove the £
    n = n.substr(1);

    // take the value  and multiply by 100
    n =Math.round(n*100);
    if(isNaN(n)){
        window.alert('Please enter a valid number');
        throw ('Please enter a valid number');
    }
}
return n;
};

Below is a test to check the function:

describe("check for £ and p", function(){
  it("checks the array for £ and p together", function(){
    expect(checkforLandP('£234p')).toBe(23400);
    expect(checkforLandP(234)).toBe(234);
    expect(checkforLandP('asdfsdf')).toBe('asdfsdf');
    expect(checkforLandP('£asdfsdfp')).toThrow("Please enter a valid number");
    });
});

The test is failing with the message: Please enter a valid number thrown.

I have double-checked the syntax and everything seems correct.

Answer №1

Instead of passing the result, pass a function to be executed. You can try using an anonymous function like this:

expect(function() {checkforLandP('£asdfsdfp'); } ).toThrow("Please enter a valid number");

For more information, check out this answer:

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

Send the form with validation in place

Currently, I am in the process of developing a code to handle form submissions using React alongside Typescript and Material-UI: export default function OpenTicket(props: any) { const classes = useStyles(); const formHandler = (e: React.FormEvent& ...

Error: SyntaxError - Found an unexpected token ":"

Can someone help me with this unexpected token : error that I am encountering? Here is the code where the issue is happening. <script type="text/javascript> $('.delete-btn').click(function() { $.ajax(function() { type: 'POST&apo ...

Unable to find the solution for 'material-ui/Button'

I recently encountered an issue while trying to integrate the material-ui library into my existing React project. I used the command npm install --save material-ui, but it resulted in an error upon running it. The error message received is as follows: ...

The navigation bar initially fills the width of the screen, but does not adjust to match the width of the table when scrolling

I've spent the last 2 hours playing around with CSS, using width:100% and width:100vw, but nothing seems to be working. Currently, the navigation bar fits perfectly across the screen on desktop browsers, so there doesn't seem to be an issue ther ...

HTML5 database error: Uncaught TypeError due to illegal invocation

When I test the code below in Chrome, I encounter an error: var db = openDatabase('foo', 1, 'foo', 1111); var sql = function(callback){ db.transaction(function(tx){ callback(tx.executeSql); }); }; sql(function(query){ ...

Learn how to effortlessly update models by integrating AngularJS with Django and Django Rest Framework

Here is a JSON representation of a post based on its ID: http://127.0.0.1:8000/update/1?format=json {"title": "about me", "content": "I like program", "created": "2014-11-29T18:07:18.173Z", "rating": 1, "id": 1} I am attempting to update the rating ...

I am having an issue with my registration form in node.js where it does not redirect after submitting

I am currently working on implementing a registration form using node/express for my website. The routes are all set up and the database connection is established. However, I am encountering some issues when users try to submit the registration form on th ...

Retrieve the input values and arrange them neatly in a table

I am encountering an issue with extracting values from a table. My goal is to retrieve all the input values from the table, but I am finding it challenging because the number of rows in the table can vary. <table id="receiptTable" class="table table-bo ...

Please refrain from clearing the text field as it will delete the input value

I feel like I must be overlooking something really minor because I just can't seem to find it! I've been attempting to sanitize the text input by setting the state to ('') and while it clears the variable, the HTML input keeps displayi ...

Convert HTML Tables to PDF Format

I am facing an issue with exporting my table data into a PDF using jQuery. I have made sure to install all the necessary library files, but my code doesn't seem to be working correctly. Can anyone spot any errors in my code or suggest what might be mi ...

"Multer implementation on Angular 5 for image uploads is failing to actually upload the files

I've been encountering an issue with uploading an image in Angular 5 using multer in Node.js. The upload doesn't seem to be working properly. My approach involves using formData to retrieve file details in Angular 5 and then sending this data to ...

An issue has arisen in Spring MVC where jquery-i18n-properties is unable to load the messages properties

Struggling with developing a Spring MVC 4 application using AngularJs and facing an issue with internationalization support. Attempting to utilize jquery.i18n-properties but encountering difficulty in loading messages_%s.properties. The error message rece ...

The desired result is not appearing when using a `fetch` request, but it does show up with an

I am facing an issue with two different requests - one using ajax and the other using fetch, its successor. When I send my data using fetch to the same API as ajax, I notice that the results are not consistent. Ajax $.post(this.config.baseUrl + this ...

Is there a way to position the menu above the button?

I need some help with my menu. Currently, it is showing up below the button and within my footer instead of above the content like I want it to. If anyone can assist me in understanding what I am doing wrong, I would greatly appreciate it. Thank you for ta ...

Transfer information from an array to a Vue function

Having some difficulties passing data to the function myChart within the mounted section. As a beginner in vuejs, I'm struggling with identifying the issue. I am trying to pass data in labels and datasets, which are called from my function. Can anyone ...

Show a loading icon as the synchronous Ajax request is being sent

Seeking a way to show a spinner while making a synchronous Ajax request to fetch data from the server. Acknowledging that asynchronous mode is preferred, but not permitted in this case. Aware that a sync ajax request can cause browser blocking. An attemp ...

Simplified method of connecting dynamic components without needing to remember functions in jQuery

I have a page where users can freely move, resize, and modify content. The basic setup is as follows: $('.element').on().resizable().draggable(); When this code is called on page load, it allows elements to be resizable and draggable. The issu ...

Some inquiries regarding the fundamentals of JavaScript - the significance of the dollar sign ($) and the error message "is not a function"

Teaching myself JavaScript without actually doing any explicit research on it (crazy, right?) has led to a few mysteries I can't quite crack. One of these mysteries involves the elusive dollar sign. From what I gather, it's supposed to be a con ...

Why JSON.parse is unable to determine if the argument is already in JSON format

When using JSON.parse, it typically parses a stringified JSON. However, if a variable is already in the form of JSON and you attempt to parse it with JSON.parse, an error is thrown: > a [] > a = JSON.parse(a) SyntaxError: Unexpected end of input ...

React is currently in the process of downloading images that were not fetched during

(Update: Initially, I suspected React Router was the cause of this issue. However, after eliminating React Router from the codebase, the problem persists. Therefore, I have extensively revised this inquiry.) Situation: I am dealing with paginated pages th ...