简体   繁体   中英

Stubbing and restoring auth function with Sinon still results in Mocha test using stub

We are trying to stub out an authentication middleware in an express app in some but not all of our tests, and we are having trouble making stubbing work for us.

Our mocha test looks something like this: describe('primaryDeal routes unit test', () => {

    describe('Authentication allows for data fetching', () => {
        let app;
        let getPrimaryDealData;
        let queryParams;
        let isAuthenticated;    
        let count = 0;

        beforeEach(() => {
            // console.log(isAuthenticated);
            if (count === 0) {
                isAuthenticated = sinon.stub(authCheck, 'isAuthenticated');
                isAuthenticated.callsArg(2);
            }
            app = require('../../lib/index.js');
        });   


        afterEach(() => {
            if (count === 0) {
                isAuthenticated.restore();
            }
            app.close();
            count++;
        });

        it(('should send an API request, validate input and return 200 response'), () => {
            return chai.request(app)
                .get('/api/contracts/')
                .then((res) => {
                    expect(res).to.have.status(200);
                });
        });

        it(('should respond with forbidden'), () => {    

            app = require('../../lib/index.js');    

            return chai.request(app)
                .get('/api/contracts/')
                .catch((res, err) => {
                    expect(res).to.have.status(403);
                });
        });
    });    
});

Our stub works as intended for the first it , but the stub appears to not be restored for the second it and our authentication middleware is not being run. Both tests work if the other is commented out.

We've tried separating these blocks in different files, and in different describe blocks, we've also tried switching the order of the it blocks, and we've tried giving both chai.request(app) separate servers but we are at a loss.

Why could it be that our second it statement isn't calling our Auth middleware?

I recommend you to use sandbox. It's more elegant to use. No need to restore stubs individually. Here is an sample:

 let sandbox; beforeEach(() => { sandbox = sinon.sandbox.create(); isAuthenticated = sandbox.stub(authCheck, 'isAuthenticated'); }); afterEach(() => { sandbox.restore(); }); 

Also, could you try to add replace

 app = require('../../lib/index.js'); 
to

 delete require.cache[require.resolve('../../lib/index.js')] app = require('../../lib/index.js'); 

Another thought, maybe you need to use reset not restore in this particular case?

PS It's also good to see index.js source as well

I had the same issue and tried this solution without success. However the delete require.cache[require.resolve('../../lib/index.js')] gave me an idea. I was able to use decache instead of delete require. This resolved the problem for me.

const decache = require('decache');


decache('../../lib/index.js');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM