proxyquire

How can I mock a fake database for when unit testing against Knex?

巧了我就是萌 提交于 2020-06-24 22:21:34
问题 I've been using Knex successfully to connect to a backend database. But I want to be able to unit test my code. Is there a way to mock the database connection? I've tried using proxyquire but I can't seem to get it to work. The problem seems to be with the way Knex is initialized. var knex = require('knex')({ client: 'mysql', connection: {} }); I setup knex to be mocked in my unit test. myService = proxyquire('../app/myService', { 'knex': knexProxy }); My service includes knex. var knex =

How can I mock a fake database for when unit testing against Knex?

自古美人都是妖i 提交于 2020-06-24 22:21:11
问题 I've been using Knex successfully to connect to a backend database. But I want to be able to unit test my code. Is there a way to mock the database connection? I've tried using proxyquire but I can't seem to get it to work. The problem seems to be with the way Knex is initialized. var knex = require('knex')({ client: 'mysql', connection: {} }); I setup knex to be mocked in my unit test. myService = proxyquire('../app/myService', { 'knex': knexProxy }); My service includes knex. var knex =

How to mock middleware in Express to skip authentication for unit test?

丶灬走出姿态 提交于 2019-12-17 15:54:25
问题 I have the following in Express //index.js var service = require('./subscription.service'); var auth = require('../auth/auth.service'); var router = express.Router(); router.post('/sync', auth.isAuthenticated, service.synchronise); module.exports = router; I want to override or mock isAuthenticated to return this auth.isAuthenticated = function(req, res, next) { return next(); } Here is my unit test: it('it should return a 200 response', function(done) { //proxyquire here? request(app).post('

Node Stub a method that returns an object

老子叫甜甜 提交于 2019-12-11 05:29:53
问题 I have a module that has some properties. I am using it as below Var propmodule = require('me-props'); var prop = new propmodule('server'); prop.get('min); //returns 3 prop.get('max') //returns 10 I have to mock this for testing. Did the below code using proxyquire and sinon var spro = proxyquire('../lib/add.js',{ 'me-props' : sinon.stub.returns({ get : sinon.stub.returns({ min :'3', max : '10 )} )} }) The above code works. But while testing, the get method call returns an object. get(min)

Stubbing with proxyquire

被刻印的时光 ゝ 提交于 2019-12-10 17:54:51
问题 How would I stub the following module with proxyquire and sinon: var email = require("emailjs").server.connect.send(); I did the following, but its not working, because when I try to trigger an error within send() it still sends the email. sendMailStub = sinon.stub(email, "send"); testedModule = proxyquire('../index.js', { 'email': { 'server': { 'send': sendMailStub } } }); And also tried: testedModule = proxyquire('../index.js', { email: {send: sendMailStub} }); This is the full test so far,

How to assert stubbed fetch more than once

余生长醉 提交于 2019-12-10 04:19:23
问题 Using proxyquire, sinon, and mocha. I am able to stub fetch on the first call of fetch. But on the second fetch call, which is recursive, I am not able to assert it. From the output, it looks like the assertion may run before the test finishes. You will see this with second fetch console out after assertion. index.js var fetch = require('node-fetch'); function a() { console.log('function a runs'); fetch('https://www.google.com') .then((e) => { console.log('first fetch'); b(); }) .catch((e)=>

How to assert stubbed fetch more than once

戏子无情 提交于 2019-12-05 04:38:11
Using proxyquire, sinon, and mocha. I am able to stub fetch on the first call of fetch. But on the second fetch call, which is recursive, I am not able to assert it. From the output, it looks like the assertion may run before the test finishes. You will see this with second fetch console out after assertion. index.js var fetch = require('node-fetch'); function a() { console.log('function a runs'); fetch('https://www.google.com') .then((e) => { console.log('first fetch'); b(); }) .catch((e)=> { console.log('error') }); } function b() { fetch('https://www.google.com') .then((e) => { console.log(

Proxyquire, rewire, SandboxedModule, and Sinon: pros & cons

不想你离开。 提交于 2019-12-03 01:00:49
问题 When mocking Node dependencies, I've happened upon the following libraries: Proxyquire Rewire SandboxedModule Sinon They all seem to do more-or-less the same thing: allow you to mock require() calls (with the exception of Sinon which mocks pretty much everything). They all seem to require some pretty elaborate setup, noting the exact syntax of the string passed to require -- not great during refactoring. What are the pros and cons of each library? When would I choose one over the other? What

Proxyquire, rewire, SandboxedModule, and Sinon: pros & cons

风流意气都作罢 提交于 2019-12-02 14:21:30
When mocking Node dependencies, I've happened upon the following libraries: Proxyquire Rewire SandboxedModule Sinon They all seem to do more-or-less the same thing: allow you to mock require() calls (with the exception of Sinon which mocks pretty much everything). They all seem to require some pretty elaborate setup, noting the exact syntax of the string passed to require -- not great during refactoring. What are the pros and cons of each library? When would I choose one over the other? What are example use-cases where each library excels? What are other products in this space that are better?