How to use the createSandbox function from sinon

Find comprehensive JavaScript sinon.createSandbox code examples handpicked from public code repositorys.

sinon.createSandbox creates a test sandbox to isolate and control the behavior of dependencies or external services during testing.

113
114
115
116
117
118
119
120
121
let sandbox
const colorsEnabled = chalk.enabled

beforeEach(() => {
  chalk.enabled = false
  sandbox = sinon.createSandbox()
  sandbox.spy(chalkStub, 'underline')
  sandbox.spy(chalkStub, 'dim')
})
fork icon7
star icon70
watch icon2

+ 3 other calls in file

4
5
6
7
8
9
10
11
12
13
const { expect } = chai;
chai.use(sinonChai);
const isTimestamp = require('src/lib/validations/is-timestamp');

describe('validations: is timestamp', () => {
        const sandbox = sinon.createSandbox();

        it('should return true if the given timestamp is valid', async () => {
                const result = isTimestamp(new Date().getTime());
                expect(result).to.be.true;
fork icon4
star icon2
watch icon2

+ 3 other calls in file

How does sinon.createSandbox work?

sinon.createSandbox creates a new Sinon sandbox object with methods to create and manage spies, stubs, and mocks, as well as restore them all at once. It is used to isolate tests and prevent any side effects from interfering with each other. The sandbox can be customized with options to add or remove behavior, and can be restored at the end of the test to clean up any changes made during the test.

7
8
9
10
11
12
13
14
15
16

```javascript
var sinon = require('sinon');

var myAPI = { hello: function () {} };
var sandbox = sinon.createSandbox();

describe('myAPI.hello method', function () {

    beforeEach(function () {
fork icon810
star icon1
watch icon3

+ 13 other calls in file

27
28
29
30
31
32
33
34
35
36
describe('App', () => {

  let envSnapshot;

  beforeEach(() => {
    sandbox = sinon.createSandbox();
    envSnapshot = Object.assign({}, process.env);
  });

  afterEach(() => {
fork icon8
star icon2
watch icon9

Ai Example

1
2
3
4
5
6
7
8
9
10
const sinon = require("sinon");

// create a new sandbox
const sandbox = sinon.createSandbox();

// stub a method using the sandbox
const stub = sandbox.stub(obj, "method");

// restore the stub
sandbox.restore();

In this example, sinon.createSandbox() is used to create a new sandbox object that can be used to isolate and control the behavior of sinon stubs, spies, and mocks. The sandbox object can then be used to create a stub of a method on an object obj, and later restore the stubbed method using sandbox.restore().

28
29
30
31
32
33
34
35
36
37
38
39
suite('Procedures', function() {
  setup(function() {
    this.jsdomCleanup =
        require('jsdom-global')('<!DOCTYPE html><div id="blocklyDiv"></div>');


    this.sandbox = sinon.createSandbox();
    globalThis.clock = this.sandbox.useFakeTimers();


    unregisterProcedureBlocks();
    Blockly.common.defineBlocks(blocks);
fork icon455
star icon624
watch icon26

+ 3 other calls in file

134
135
136
137
138
139
140
141
142
    'foo': '{{bar}}',
    'baz': '{{bat}}',
};

beforeEach(done => {
    sandbox = Sinon.createSandbox();
    renderer = new HandlebarsRenderer();
    done();
});
fork icon35
star icon8
watch icon60

+ 4 other calls in file

2
3
4
5
6
7
8
9
10
11
12
13
14
const { expect } = require('chai');


const { LoginClient } = require('../src/index');


let sandbox;
beforeEach(() => { sandbox = sinon.createSandbox(); });
afterEach(() => sandbox.restore());


describe('index.js', () => {
  describe('LoginClient', () => {
fork icon2
star icon6
watch icon3

+ 5 other calls in file

26
27
28
29
30
31
32
33
34
35
36
37
38
}




describe("API Suite test", () => {
    let api = {};
    let sandbox = sinon.createSandbox();


    beforeEach(() => {
        sandbox = sinon.createSandbox();
    });
fork icon0
star icon0
watch icon1

+ 3 other calls in file

16
17
18
19
20
21
22
23
24
25
if (!current.dialect.supports.transactions) {
  return;
}

beforeEach(function () {
  this.sinon = sinon.createSandbox();
});

afterEach(function () {
  this.sinon.restore();
fork icon0
star icon0
watch icon420

161
162
163
164
165
166
167
168
169
    threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3);
    clock = sinon.useFakeTimers({
        now: threeMonthsAgo.getTime(),
        toFake: ['setTimeout']
    });
    sinon.createSandbox();
    configUtils.set('milestones', milestonesConfig);
    mockManager.mockMail();
});
fork icon0
star icon0
watch icon2

+ 2 other calls in file