Unable to use Proxyquire for stubbing

I'm having trouble getting Proxyquire to work with a basic method substitution, and I can't seem to pinpoint the issue.

First, I have a file called lib.js:

module.exports = {
    thing: () => {
        console.log("thing");
    }
};

Then, there's test.js:

const lib = require("./lib");

module.exports = () => {
    lib.thing();
};

Next, I tried to stub the dependency and replace 'thing' with another function like so:

const proxyquire = require("proxyquire");
const libStub = {};
const test = proxyquire("./test", {"lib": libStub});

test();

libStub.thing = () => {
    console.log("replaced");
};

test();

Unfortunately, even after replacing 'thing' with "replaced", the test still logs out "thing" both times instead of the expected "replaced" on the second call. Any insight or assistance on this matter would be greatly appreciated.

Answer №1

When working with proxyquire, make sure to specify the same path used in the require statement:

Thus, it should look like this:

const proxyquire = require("proxyquire");
const libStub = {
  thing: () => console.log('replaced')
};
const testCase = proxyquire("./testCase", {"./lib": libStub});
testCase();

Answer №2

If you want a reliable solution for this recurring issue in the future, consider utilizing:

Both options will raise an exception if they encounter difficulties while attempting to mock something, providing detailed reasons and filenames of their attempts.

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

What are the best methods for querying and updating a self-relation in Prisma?

I recently obtained some self-relation tables directly from a specific Prisma example. model User { id Int @id @default(autoincrement()) name String? followedBy Follows[] @relation("follower") following Follows[] @rel ...

What is causing my Three.js scene to appear pitch black?

I am embarking on my first three.js project, but unfortunately my scene is appearing entirely black. I am utilizing Vite as my local server. Below is the JavaScript code I am currently using: import * as THREE from 'three' //Scene const scene = ...

Placing the video at the center of the background image

                    Can someone assist me with a design issue I'm facing? I have two divs within a section. The left div contains a heading and a button, while the right div contains a video. However, when I try to add a background image to ...

Trigger a click (or selection) on a specific dropdown option using jQuery

I am attempting to initiate a click function upon page load using jQuery on a dropdown option within Chosen, in order to activate a subsequent action. Unfortunately, I have not been successful in getting it to work. I have tried the following methods: jQ ...

Using jQuery to trigger alert only once variable has been updated

I have a question that may seem too basic, but I can't find the solution. How do I make sure that the variables are updated before triggering the alert? I've heard about using callbacks, but in this case, there are two functions and I'm not ...

How can you provide unique css box shadows for various browsers without relying on browser detection?

I have observed that each browser displays box shadow blur radius a bit differently, so I want to ensure consistency across all browsers. However, since they use the unprefixed version, I need to provide different stylesheets for each browser. What is the ...

Tips for showcasing content on a jQuery calendar

I have a jQuery calendar implemented on my website and I would like to show a list of local holidays on the calendar. The calendar is functioning correctly with the script below, but I just need help displaying the holidays. <script> $(document).r ...

Having trouble retrieving the value of a custom directive attribute

My custom directive, named "mycomponent", has the following configuration: restrict: 'AE', templateUrl: 'template.html', scope:{ sTransactionType: '=transactionType', sStorageVariable: '=storageVariable&apos ...

Utilize JQuery's appendTo() method along with html() function for seamless content replacement

I have a question about appending and replacing div elements. I am currently using the following code: $('.toggle_questions').appendTo('#'+sectionId).show(); While this works perfectly to append my div with the class .toggle_questions ...

Error in JScript: Unable to set value to a function's output

I have encountered an issue in my application where I am attempting to hide a table based on a specific condition. To achieve this, I have implemented a simple JavaScript function. However, the function is encountering an error at a particular line. The p ...

Caution: Anticipated the server's HTML to include a corresponding <body> within a <div> tag

Upon checking the console, I noticed a warning message appearing. I am puzzled as to why this is happening since I have two matching <body> tags in my index.js file. The complete warning message reads: Warning: Expected server HTML to contain a matc ...

placeholder for dropdown selection in Vue.js version 2.0.0

Currently, I am in the process of developing a web application using vuejs 2.0. In order to create a straightforward select input, I employed the following code: <select v-model="age"> <option value="" disabled selected hidden>Select Age&l ...

Utilizing React's multiple states for enhancing click event functionality

Here's the scenario: I am currently developing a Simon-Says game app using React Native. In this game, the user is shown a sequence of flashing buttons and must press them in the correct order. My main concern is figuring out how to utilize two diffe ...

chosen selection from AngularJS dropdown

I'm really struggling with something. Currently, I am working on a web app using AngularJS where I have created a table displaying database results. Each row in the table contains a select item loaded with a model. However, I am unsure how to mark a ...

Tips for reusing multiple sequences of chained transitions in D3

Looking for a more efficient way to code two lengthy sequences of chained transitions with varying order, properties, and attributes. For example, one sequence might be a, b, c, d, e, f, g, h, and the other e, f, g, h, a, b, c, d. Tried the code below with ...

What is the best way to handle an AJAX JSON response in AngularJS?

Need help with extracting properties from a JSON object returned by a REST service in AngularJS? Want to parse the firstName, lastName, and other attributes from the JSON response? Check out the code snippets below for guidance. Here is an example of an A ...

Ensure that any modifications made to an Angular service are reflected across all components that rely on that service

I am currently in the process of replicating a platform known as Kualitee.com, which serves as a test-management tool utilized by QA Engineers. Within Kualitee, users can access multiple projects, each containing various test cases and team members. The ab ...

Is there a way to illuminate a complete row by simply hovering over a span within one of the columns?

If I want to change the background-color of a div with classes "row" and "highlightThisRow" when hovering over a span with the class "fromThisSpan", how can I achieve that? In a list, there are multiple rows with several columns. The span in question is l ...

Filtering dynamically generated table rows using Jquery

I'm currently working on a project that involves filtering a dynamic table based on user input in a search bar. The table contains information such as name, surname, phone, and address of users. Using jQuery, I have created a form that dynamically ad ...

Using *ngFor to iterate through a nested collection in an Angular 2 application

I'm currently working on a challenge involving drilling down to iterate over an array within another collection of arrays within an Angular 2 application. To start off, I have set up my component to subscribe to an observable in the ngOnInit lifecycle ...