2

First, I've gone through all of the other posts related to this topic and implemented Sinon.sandbox(). Unfortunately, I am still getting the following error:

TypeError: Attempted to wrap createMessage which is already wrapped

Note that I'm using Ava for testing and in this case, I've set it to run in serial mode rather than parallel.

// Load modules

import Queue from '../lib/queue';

import test from 'ava';
import Sinon from 'sinon';
import Proxyquire from 'proxyquire';

// Globals

const FAKE_APPOINTMENT_ID = 'ab9e9495-fdbf-4607-8f57-01c6e91bd8f5';

let push;
let queueStub;
let sandbox;


// Test setup

test.beforeEach(() => {

    // create the sinon sandbox
    sandbox = Sinon.sandbox.create();

    // stub the queue module
    queueStub = sandbox.stub(Queue.prototype);

    // inject the stub
    push = Proxyquire('../lib/push', {
        './queue': queueStub
    });
});


test.afterEach(t => {

    sandbox.restore();
});

// Tests

test.only('sends an appointment approved push notification', async t => {

    await push.appointmentApproved(FAKE_APPOINTMENT_ID);

    t.true(queueStub.createMessage.calledOnce);
});


test.only('sends an appointment cancelled push notification', async t => {

    await push.appointmentCancelled(FAKE_APPOINTMENT_ID);

    t.true(queueStub.createMessage.calledOnce);
});


test.only('sends an appointment updated push notification', async t => {

    await push.appointmentUpdated(FAKE_APPOINTMENT_ID);

    t.true(queueStub.createMessage.calledOnce);
});

I've also tried switching beforeEach to before, and while the first of the 3 tests passes, the last two complete because the stub's call count never seems to reset. Maybe I'm misunderstanding the purpose of restore()

Thanks!

jdixon04
  • 1,435
  • 16
  • 28
  • Trying to reproduce, but if `queue.js` exports a class, and you replace that with a stubbed _prototype_ for `push.js`, how is it instantiated there? – robertklep Jul 20 '16 at 06:57
  • @robertklep maybe that's what I'm missing. Is there a proper way to stub a class that I'm missing? – jdixon04 Jul 21 '16 at 03:43
  • From what I can see, you don't need `proxyquire` at all if you make sure that you `require('./push')` _after_ having stubbed `Queue.prototype`. I assume that you do `new Queue` somewhere in `push.js`? – robertklep Jul 21 '16 at 06:19
  • Have you tried the pattern in the SO question [Cleaning up sinon stubs easily](https://stackoverflow.com/q/11552991/1426891)? Also, from the [Sinon docs](https://sinonjs.org/releases/latest/sandbox/): "Since sinon@5.0.0, the sinon object is a default sandbox. Unless you have a very advanced setup or need a special configuration, you probably want to only use that one." – Jeff Bowman Jan 18 '22 at 18:20

0 Answers0