What is the best way to access a global variable that has not been initialized in a test?

I'm currently working on unit testing my code, but I've encountered an issue with a library that my code relies on. The library, named setup, is not directly accessible during compile time. However, it will be available when the code is executed. It's important to note that I am not using webpack or any similar tool, only TS node.

Production code:

declare let setup: any;

setup.rest = (): void => { // this signature cannot be altered

}

Test code:

import "jasmine";

describe("App", () => {
    beforeEach(() => {
        setup = {};
    })

    it("should have a rest function", () => {
        setup.rest();
    });
});

My test environment is powered by Jasmine as a test runner. However, upon running the tests, I encounter the following error:

ReferenceError: setup is not defined

I need assistance in finding a way to initialize the setup variable and make it accessible to both my test and production code. How can I achieve this?

Answer №1

It seems like you're trying to initialize the function super

You could include a spy here.

const setupSpy = TestBed.get(setup);
setupSpy.rest.and.returnValue(add an object/variable here to return)

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

Looking to streamline a JavaScript function, while also incorporating jQuery techniques

I've got this lengthy function for uploading photos using hidden iFrames, and while it does the job well, it's quite messy. I'm looking to simplify it into a cleaner function with fewer lines of code for easier maintenance. function simplif ...

Execute the test case multiple times in Boost test

Is it possible to run a unit test or group of unit tests multiple times in Boost test? Imagine I have the following code: BOOST_FIXTURE_TEST_SUITE(someSuite, someFixture) BOOST_AUTO_TEST_CASE(someTest) { ... } BOOST_AUTO_TEST_SUITE_END() ... and I ...

Creating dual graphs simultaneously in Rickshaw

Can two graphs with different Y axes be plotted together? I have data on page views organized in a stacked area chart by referrer, and I would like to overlay a line graph depicting the number of actions over time. Although I already have both graphs ind ...

Ways to automatically update options in a dropdown menu using text box entries

When a character is typed into the text box, I want the select box to display options that contain that character. For example, if I type 'a', the select box should show "Anand", "Arun", and "Ananya". If I then type 'n', only "Anand" an ...

HTML & Javascript |iOS| - Eliminate emojis

Right now, I have an HTML textarea element that enables users to input text, which works well. However, the issue is that users are able to input emojis. I am wondering if there is a way to remove these emojis after they have been entered by the user, or ...

Using JavaScript to control a dropdown list based on the selection of another dropdown

Hello, I am new to programming and a JavaScript beginner seeking help from you all. I have two dropdown lists and I am attempting to manipulate one based on the selection of the other using JavaScript. Both dropdown lists contain time in hours denoting st ...

How can you tell if a specific keyboard key is being pressed along with the CTRL button?

Is there a way to call functions when a specific key is pressed along with the CTRL key (on a Windows system)? While testing for a particular keyCode, I used event.keyCode. I researched the codes assigned to each key and assumed that 17 + 73 would represe ...

A ) is missing after the argument list

I am developing code in AngularJS to run a function, but I encountered an error stating that there is a missing ')' after the argument list. "content": '<b>DocumentType</b>:'+s.getdocumentTypeName(s.documentType)+'< ...

Verifying if a form was submitted through ajax in PHP

I am facing an issue with my login form validation process, where the form data is first validated using JavaScript and then sent to a PHP file for further processing. The form submission is handled through AJAX. Currently, I have implemented an if statem ...

How can I add a Google Street View image to a document?

I attempted two different sets of code, but unfortunately, neither were successful. $("#search").click(function(){ var citySearch = $("#city").val(); var map = $("map"); var streetView = "https://maps.googleapis.com/maps/api/streetview?size=400x400&l ...

Controller is not being triggered by Ajax method when there is a decimal value

I am currently working on implementing a time registration feature in my web application. Users can select the project they worked on and enter the number of hours spent on that project. Everything is functioning properly until users start adding half-hou ...

Creating a centered and beautifully styled picture with a specific maximum size using React

I recently completed the development of a new website, which can be viewed at Upon inspection of the website, it is evident that the photo is not centered and appears too large on mobile phones. Despite my efforts to align it using various methods outline ...

Tabulator - Using AJAX to retrieve a JSON document

I am currently working on importing a json file from an Azure blob container using Azure Data Factory. Despite closely following the documentation and researching various resources on Stack Overflow, I am facing challenges with making the ajax request fun ...

Can a for loop be implemented within a mongoose schema method?

Is there a way to modify this for loop so that it runs through the entire array instead of adding one by one? Any suggestions? EndorsedSkillSchema.methods = { async userEndorsedSkill(arr) { for (var i = 0; i < arr.length; i++) { const skil ...

What might be causing the delay in synchronization between the state in my parent component?

import React, { Component } from "react"; import "./Game.css"; class Game extends Component { static defaultProps = { list: ["rock", "paper", "scissors"] }; constructor(props) { super(props); this.state = { play: false, rando ...

Having trouble reaching the upload file in PHP

I am attempting to transfer files to a remote server using JavaScript, with PHP as the backend. The JavaScript code seems to be functioning properly, but on the PHP side, the $_FILES and $_POST arrays are empty. However, the $_SERVER array contains some da ...

Transform the collection of nested objects into an array of objects with identical key-value pairs and then output the result after each iteration

My goal is to transform an object with objects inside into an array of objects. The initial data looks like this: "data" :{ "tDetails": { "tName": "Limited", "tPay": "xyz" } ...

Assurance-driven number tally

I'm diving into JavaScript and recently started exploring promises. I've put together a code snippet that logs the value passed to the promise function as a parameter after the setTimeout function is triggered. Now, I'm wondering if there&ap ...

What is the best way to create floating text that appears when clicked, similar to the mechanics

https://i.stack.imgur.com/nRKG1.jpg Upon clicking the "big cookie" in my game, a popup appears displaying the number of cookies earned (like +276.341 septillion in this image). The popup slowly moves upward and then fades out. I successfully incorporated ...

JavaScript Array data value

When working with data retrieved from an interface, I need to extract specific values for a particular menu. How can I efficiently achieve this using JavaScript? [ { "title": "11-07 - 11-08", "weekhit": "Weekhit from 11 ...