what is `this` in sinon.test() in mocha tests?

BAE

My codes:

it('should', sinon.test(function() {
  console.log(this); // what `this` refer here?
  ...
}));

In the above code, what this refer in the sinon.test() function?

I tried to log it, but got the error:

TypeError: Converting circular structure to JSON

Sinon version: 1.17.6

Any comments welcomed. Thanks.

UPDATE

after reading the below answer, I am still confused. Why the following two pieces of codes work when this.myOnject.log was stubbed only once?

  it('should', sinon.test(function() {
    const stubLog = this.stub(this.myObject.log, 'warn');
    // ...
    this.myObject.process();
    // expect codes...
  }));


  it('should', sinon.test(function() {
    const stubLog = sinon.stub(this.myObject.log, 'warn');
    // ...
    this.myObject.process();
    // expect codes...
  }));

UPDATE

If downvoted, please leave some comments to let me know why you downvoted. I am very confused by the question I posted. But I am more confused why so many people downvoted, but did not leave any useful comments.

Louis

sinon.test creates a new Sinon sandbox for your test and augments the normal this object that Mocha passes to your test with Sinon-specific methods like spy, stub, etc. that call the corresponding methods on the sandbox that is automatically created. The fact that it augments the object allows you to still use all the stock Mocha methods that Mocha normally provides on this, and allows you to set values on arbitrary fields. The restore method of the sandbox is automatically called when the test ends. It is a convenient way to provide isolation between tests so that modifications to common objects that one test performs through Sinon don't affect other tests.

Here's an example:

const sinon = require("sinon");

before(function () {
    // We set "moo".
    this.moo = "I'm a cow.";
});

it("test", sinon.test(function () {
    // We can access "stock" Mocha functions.
    this.timeout(0);

    // However, this is augmented with sinon functions.
    const spy = this.spy();
    spy();

    // We can access "moo";
    console.log("value of moo", this.moo);
}));

This produces:

value of moo I'm a cow.
  ✓ test

  1 passing (34ms)

The code above assumes Sinon 1.x. In Sinon 2.x and over sinon.test is no longer bundled with Sinon but is its own separate package.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unit test with enzyme, mocha, sinon and reactjs. How to see what the spy is called with?

What's wrong with this unit test of an async JavaScript function (via Mocha/Sinon)?

How to test object from factory with mocha with sinon

Unit test issue with Mocha and Sinon using javascript

Mocha/Sinon test mongoose inside express

Simulate and test a keypress event with Mocha, Chai, Sinon?

How to mock, and test, closures with mocha, chai and sinon

mocha not running all tests in test dir

Karma test runner does not run mocha tests

Mocha / Sinon Unit test JS Class and Instance issue

Stub function does not executed when executing test using sinon and mocha

Sinon/Mocha test with async ajax calls didn't return promises

Node / Mocha / Chai / Sinon - Async await unit test error

Sinon and Mocha: How to await for a promised to be resolved before assertion in test function?

How to properly test an Express controller method with Mocha, Chai, and Sinon

How to test fs.writeFile errors with mocha + chai + sinon

Ensure the done() callback is being called in this test (Mocha, Chai, Sinon)

Mocha TS tests fail with `npm test` but not if run directly

How to properly stub out request-promise in a mocha unit test using sinon?

How do I unit test localStorage being undefined with Mocha/Sinon/Chai

New to testing, how would I test this method with Mocha, Chai, Enzyme, and Sinon?

How do i unit test using mocha/chai/sinon if a variable has a react component as value?

What is the correct structure for using mocha to test your package?

What is the right way to unit test asynchronous functions in NodeJS using Mocha?

Dynamic asynchronous mocha tests

Mocha tests mocking function

Mocha Tests Are Timing Out

mocha sidebar is not showing tests

Mocha tests for asynchronous functions