mocha

Mock a class method using mockery and sinon

爱⌒轻易说出口 提交于 2019-12-22 05:49:06
问题 I'm learning to unit test using the node module mockery with sinon. Using only mockery and a plain class I'm able to inject a mock successfully. However I would like to inject a sinon stub instead of a plain class but I'm having a lot of troubles with this. The class I am trying to mock: function LdapAuth(options) {} // The function that I want to mock. LdapAuth.prototype.authenticate = function (username, password, callback) {} And here is the code I'm currently using in my beforeEach()

Correct way to use done() while testing asyc/await with mocha

↘锁芯ラ 提交于 2019-12-22 05:23:30
问题 I am practicing basic unit test cases with mocha and a bit confused HOW and WHEN to use done() handler. How to use done() ? Below is my sample code where I am not able to use done : it('Testing insertDocumentWithIndex', async (done) => { try{ var data = await db.insertDocumentWithIndex('inspections', { "inspectorId" : 1, "curStatus" : 1, "lastUpdatedTS" : 1535222623216, "entryTS" : 1535222623216, "venueTypeId" : 1, "location" : [ 45.5891279, -45.0446183 ] }) expect(data.result.n).to.equal(1);

How do I perform an `around` or `aroundEach` with mocha?

删除回忆录丶 提交于 2019-12-22 05:14:31
问题 Mocha provides before , beforeEach , after , and afterEach . I'm looking for something like around or aroundEach , but I can't find anything in the mocha docs about it. My use case is that I'd like to wrap each test in a DB transaction, performing a rollback after each one is run. I envision doing something like this: aroundEach(function (testRunner, done) { sequelize.transaction().next(function (t) { testRunner(); t.rollback().done(done); }); }); As an alternative/workaround, something like

Mocha unit tests running with Karma - done() is not defined

℡╲_俬逩灬. 提交于 2019-12-22 04:07:58
问题 I'm trying to get tests written with Mocha to work running Karma, and they sort of work, but I cannot use the done() method to implement async tests, which essentially makes the tools useless to me. What am I missing? karma.conf.js module.exports = function(config) { config.set({ basePath: '../..', frameworks: ['mocha', 'requirejs', 'qunit'], client: { mocha: { ui: 'bdd' } }, files: [ {pattern: 'libs/**/*.js', included: false}, {pattern: 'src/**/*.js', included: false}, {pattern: 'tests/mocha

Redux Testing - ReferenceError: localStorage is not defined

核能气质少年 提交于 2019-12-21 20:36:01
问题 I'm currently having a lot of trouble running tests on my redux actions. The test passes but I get the following error each time it is ran: ReferenceError: localStorage is not defined I also got an error before which was: ReferenceError: fetch is not defined I fixed this by using isomorphic-fetch. Anyway I am unsure on how I should configure Mocha to run these front end tests. Any help would be much appreciated. Mocha test command: mocha -w test/test_helper.js test/*.spec.js test_helper.js:

How can I mock an imported module in ecmascript 6?

空扰寡人 提交于 2019-12-21 16:58:43
问题 I have a test-setup with mocha, babel and node that is meant to test ecmascript 6 code. Does anybody have any suggestions on how to mock away imports in the module under test? 回答1: Imports and exports in ES2015 are part of the language itself, and are designed to be statically analysable. They therefore cannot be manipulated at runtime, and that makes dynamic mocking impossible. I would recommend that you take a look at implementing some form of lightweight dependency injection framework, or

stubbing ES6 super methods using sinon

大城市里の小女人 提交于 2019-12-21 13:58:18
问题 I am having a problem stubbing the base class methods using Sinon. In the example below I am stubbing the call to base class method GetMyDetails as follows. I am sure there is a better way. actor = sinon.stub(student.__proto__.__proto__,"GetMyDetails"); And also the value of this.Role ends up being undefined. I have created a simple class in javascript "use strict"; class Actor { constructor(userName, role) { this.UserName = userName; this.Role = role; } GetMyDetails(query,projection,populate

Does '#' has special meaning in Mocha?

て烟熏妆下的殇ゞ 提交于 2019-12-21 13:06:07
问题 describe('#indexOf()'.... it('#doSth()'); Does '#' has special meaning in Mocha? What does describe and it actually do? sorry not found document for describe and it 回答1: describe and it follows a pattern called BDD , which means "Behaviour Driven Development". It just defines an interface that makes you think a little different about how you write your tests, at least it should. Nesting of describe also makes it possible to group your tests functionally, and the resulting report has a

Mocha describe is not defined [duplicate]

倖福魔咒の 提交于 2019-12-21 09:28:43
问题 This question already has answers here : ReferenceError: describe is not defined NodeJs (7 answers) Closed 3 years ago . I have a directory called MochaTests. Inside there I have the example test found on the mocha website "1.2.3 Mocha". At the command prompt(My OS is WIN7), I type in Mocha, and the result is "0 passing(2 m2)". I installed just like the instructions say; npm install -g mocha...from what I can tell it installs just fine. So, in c:\MochaTests> I type node then I type .load

sinon stub not replacing function.

痴心易碎 提交于 2019-12-21 09:22:10
问题 I tried a dummy module and to stub it, but does not work. the app.js function foo() { return run_func() } function run_func() { return '1' } exports._test = {foo: foo, run_func: run_func} the test.js app = require("./app.js")._test describe('test', function(){ it('test', function(){ var test_stub = sinon.stub(app, 'run_func').callsFake( function(){ return '0' }) test_stub.restore() var res = app.foo() assert.equal('0', res) }) }) I tried the advice from: sinon stub not replacing function But