Closed anthony-moyart closed 8 years ago
I finally figured it out. The final test code looks like
let valStub = sinon.stub(lib, 'val').returns(false);
import script from './script'
describe('script', () => {
describe('can mock a lib that', () => {
it('should return false instead of true', () => {
script.exec().should.equal(false);
});
})
});
And the test passes
@k4zuk0 how did you make lib available so you can stub something on it? it's not clear to me from your examples.
Hi,
I'm quite new to JS unit testing (I'm more of a PHP backend developper). I recently worked on an Express API, fully written in ES6 (using babel), and wanted to fortify it with some unit tests.
I managed to setup Mocha & Chai to work like a charm on my "standard" tests, but when it comes to mocks, I can't seem to figure it out. I've tried quite everything I had in mind, but still can't get it to work.
I first needed to mock a class within the
elastic search
module, and I couldn't get it to work. So I tried it in some lightweight/sandbox files.I have
lib.js
(the module I want to mock)Then
script.js
(the module I want to test)And the full test file (
script.spec.js
, in the exact same folder as the two others)And its output (run in IntelliJ Idea) :
Dependencies :
test/mocha.opts
: