Proxyquire calling original file instead of stub - node.js

Hi I'm trying to mock a Model inside my controller using proxyquire. But for some reason when a try to inject a mock, the original file is being called.
This is how my model is being required inside the controller:
var Product = require('./product.model');
And this is how I', trying to stub the model
var proxyquire = require('proxyquire').noCallThru().noPreserveCache();
var productsStub = {findAsync: sinon.stub()};
productsStub.findAsync.resolves(mockProducts);
var mockProducts = [{_id:'0001',title: 'title', price: 123, description: 'le description'}];
var productController = proxyquire('./product.controller',{'./product.model':productsStub});
but when I run my tests I get this error
Running "mochaTest:unit" (mochaTest) task
>> Mocha exploded!
>> OverwriteModelError: Cannot overwrite `Product` model once compiled.
>> at Mongoose.model (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/mongoose/lib/index.js:360:13)
>> at Object.<anonymous> (/Users/victor/Documents/projects/estudo/mean/meanshop/server/api/product/product.model.js:15:27)
>> at Module._compile (module.js:409:26)
>> at normalLoader (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/babel-core/lib/api/register/node.js:199:5)
>> at Object.require.extensions.(anonymous function) [as .js] (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/babel-core/lib/api/register/node.js:216:7)
>> at Module.load (module.js:343:32)
>> at Function.Module._load (module.js:300:12)
>> at Module.require (module.js:353:17)
>> at require (internal/module.js:12:17)
>> at Object.<anonymous> (/Users/victor/Documents/projects/estudo/mean/meanshop/server/api/product/product.model.spec.js:1:15)
>> at Module._compile (module.js:409:26)
>> at normalLoader (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/babel-core/lib/api/register/node.js:199:5)
>> at Object.require.extensions.(anonymous function) [as .js] (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/babel-core/lib/api/register/node.js:216:7)
>> at Module.load (module.js:343:32)
>> at Function.Module._load (module.js:300:12)
>> at Module.require (module.js:353:17)
>> at require (internal/module.js:12:17)
>> at /Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/mocha/lib/mocha.js:220:27
>> at Array.forEach (native)
>> at Mocha.loadFiles (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/mocha/lib/mocha.js:217:14)
>> at MochaWrapper.run (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/grunt-mocha-test/tasks/lib/MochaWrapper.js:51:15)
>> at /Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/grunt-mocha-test/tasks/mocha-test.js:86:20
My question is why I am getting this if in theory the original products.model.js module will not be called.

proxyquire does NOT stub not listed modules and they will be required as usual, so
it looks like you have more dependencies inside product.controller and they all are being normally required.
I can't find nice solution for this problem except providing full stub for proxyquire, e.g. add stubs for all files that required from directly imported files.

Related

The "amdefine" can not load a custom module

I installed the requirejs package to optimize the work with paths to files. In general, I do the next steps:
I have an object with paths to file, that have next values:
"app/database": "/home/dmitry/projects/information-platform-api/app/utils/database"
After I initialize the requirejs mapping in app.js (main file):
requirejs.config({
baseUrl: __dirname,
nodeRequire: require,
paths: paths,
map: {
'*': paths
}
});
After, I need to initialize base models, and with help for I called all files from paths object. This looks like:
for (let file in paths) {
requirejs(paths[file]);
}
When I initializing the file, which has the dependency of the above "app/database", I have a message error: Error: Cannot find module 'app/database'.
Maybe, I don't understand "How works the amdefine", but I have opinion, what a requirejs and amdefine don`t link. Do you have any ideas, why it doesn't work?
A part of the file, which need to investigate:
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define([
'sequelize',
'app/database',
'model/student'
], (
Sequelize,
sequelize,
Student
) => { });
Result of my investigation:
The amdefine doesn't know about requirejs mapping.
Full error message:
Error: Tried loading "/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company" at /home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company then tried node's require("/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company") and it failed with error: Error: Cannot find module 'app/database'
Require stack:
- /home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company.js
- /home/dmitry/projects/information-platform-api/app.js
- /home/dmitry/projects/information-platform-api/bin/www
at /home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:2600:27
at Object.execCb (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1946:33)
at Module.check (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1133:51)
at Module.enable (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1426:22)
at Object.enable (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1807:39)
at Function.req.get (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:2524:33)
at Object.localRequire [as require] (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1678:40)
at requirejs (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:2047:24)
at Object.<anonymous> (/home/dmitry/projects/information-platform-api/app.js:142:5)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/home/dmitry/projects/information-platform-api/bin/www:7:13)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
originalError: Error: Cannot find module 'app/database'
Require stack:
- /home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company.js
- /home/dmitry/projects/information-platform-api/app.js
- /home/dmitry/projects/information-platform-api/bin/www
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
at Function.Module._load (internal/modules/cjs/loader.js:862:27)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at req (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:144:31)
at stringRequire (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:220:28)
at amdRequire (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:113:24)
at /home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:177:24
at Array.map (<anonymous>)
at runFactory (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:176:25)
at define (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:277:13)
at Object.<anonymous> (/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company.js:5:1)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at /home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:2598:75
at Object.execCb (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1946:33)
at Module.check (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1133:51)
at Module.enable (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1426:22)
at Object.enable (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1807:39) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company.js',
'/home/dmitry/projects/information-platform-api/app.js',
'/home/dmitry/projects/information-platform-api/bin/www'
]
},
moduleName: '/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company',
requireModules: [
'/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company'
]
}
Process finished with exit code 1
The solution is here guys. The amdefine have two params, first param it's a global variable module, and the second it's require function for calling the local files. By default, it was just require not requirejs which contains custom mapping.
if (typeof define !== 'function') {
var define = require('amdefine')(module, require('requirejs'));
}

Oracle DB connection with Node.js Getting "Error: Schema User name is not Set! Try Set Environment Variable NODE_ORACLEDB_USER."

I have installed Node JS version 12, cloned node-oracle db from github.
I have also set OCI_LIB_DIR Path as mentioned in this article.
module.exports = {
user : process.env.NODE_ORACLEDB_USER || "hr",
// Get the password from the environment variable
// NODE_ORACLEDB_PASSWORD. The password could also be a hard coded
// string (not recommended), or it could be prompted for.
// Alternatively use External Authentication so that no password is
// needed.
password : process.env.NODE_ORACLEDB_PASSWORD || abcd,
// For information on connection strings see:
// https://oracle.github.io/node-oracledb/doc/api.html#connectionstrings
connectString : process.env.NODE_ORACLEDB_CONNECTIONSTRING || "jdbc:oracle:thin:#localhost:1521/orcl",
// Setting externalAuth is optional. It defaults to false. See:
// https://oracle.github.io/node-oracledb/doc/api.html#extauth
externalAuth : process.env.NODE_ORACLEDB_EXTERNALAUTH ? true : false
};
I have created a basic connection in SQL developer, would it help.
I have installed npm in node-oracledb and also set the username, but when I try to run the command "npm test" , It gives me the error
Deeksha ~/Desktop/nodewithoracle/node-oracledb (master)
$ npm test
> oracledb#4.1.0 test C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb
> mocha --opts test/opts/mocha.opts
C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb\node_modules\yargs\yargs.js:1163
else throw err
^
Error: Schema User name is not Set! Try Set Environment Variable NODE_ORACLEDB_USER.
at Object.<anonymous> (C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb\test\dbconfig.js:48:9)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (C:\Users\850044533\Desktop\nodewithoracle\node-oracledb\test\notes.js:32:18)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb\node_modules\mocha\lib\mocha.js:330:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb\node_modules\mocha\lib\mocha.js:327:14)
at Mocha.run (C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb\node_modules\mocha\lib\mocha.js:804:10)
at Object.exports.singleRun (C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb\node_modules\mocha\lib\cli\run-helpers.js:207:16)
at exports.runMocha (C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb\node_modules\mocha\lib\cli\run-helpers.js:300:13)
at Object.exports.handler (C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb\node_modules\mocha\lib\cli\run.js:296:3)
at Object.runCommand (C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb\node_modules\yargs\lib\command.js:242:26)
at Object.parseArgs [as _parseArgs] (C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb\node_modules\yargs\yargs.js:1087:28)
at Object.parse (C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb\node_modules\yargs\yargs.js:566:25)
at Object.exports.main (C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb\node_modules\mocha\lib\cli\cli.js:63:6)
at Object.<anonymous> (C:\Users\Deeksha\Desktop\nodewithoracle\node-oracledb\node_modules\mocha\bin\_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11
npm ERR! Test failed. See above for more details.
set the credential environment variables to your DB credential values before you start Node.js. The error message says NODE_ORACLEDB_USER is not set.
Alternatively you can set the values directly in your getConnection() calls:
connection = await oracledb.getConnection({ user: 'hr', password: 'welcome', connectString: 'localhost/orcl' });
But be careful of hard coding passwords.
Use a valid connection string; a JDBC connection string is not usable (Node.js is not JDBC). See the node-oracledb doc JDBC and Oracle SQL Developer Connection Strings for how to determine what to use. Based on what you posted, you should use just localhost:1521/orcl.
Save yourself some time, and read the node-oracledb installation manual, the documentation, and the examples.

installing with NPM gives out error:2406C06E:random number generator

I'm using Ubuntu server 18.04.
Every time I try to use NPM it crashes and shows the following exception:
internal/crypto/random.js:119
if (ex) throw ex;
^
Error: error:2406C06E:random number generator:RAND_DRBG_instantiate:error retrieving entropy
at Object.randomBytes (internal/crypto/random.js:54:31)
at /usr/lib/node_modules/npm/lib/npm.js:444:32
at Object.<anonymous> (/usr/lib/node_modules/npm/lib/npm.js:496:3)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at /usr/lib/node_modules/npm/bin/npm-cli.js:28:13 {
opensslErrorStack: [
'error:2406B072:random number generator:RAND_DRBG_generate:in error state',
'error:2406C06E:random number generator:RAND_DRBG_instantiate:error retrieving entropy',
'error:2406B072:random number generator:RAND_DRBG_generate:in error state',
'error:2406C06E:random number generator:RAND_DRBG_instantiate:error retrieving entropy'
]
}
Does anybody has any idea of what could be wrong?

Can't import multiple modules using 'import' [duplicate]

This question already has an answer here:
Node.js plans to support import/export ES6 (ECMAScript 2015) modules
(1 answer)
Closed 4 years ago.
I'm trying to import classes from a .js file, but when I run the program it seems to have a Syntax error. Here's my code:
import { RTUser, RTEvent, RTPurchase, RTMedia } from "./defs";
and here's the error that's being shown in the console.
/Users/williamg/retake/api/RTKit/index.js:34
import { RTUser, RTEvent, RTPurchase, RTMedia } from "./defs";
^
SyntaxError: Unexpected token {
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.wrappedLoad [as _load] (/Users/williamg/retake/api/node_modules/newrelic/lib/shimmer.js:372:38)
at Module.require (internal/modules/cjs/loader.js:637:17)
An example of an export in the defs.js file is:
export class RTPurchase extends RTClass { }
The files are built from .ts files using tsc.
Any help would be appreciated!
Try Using var esLint = require('./ejbs');

TypeError: Wrong input type "String" for args

I'm trying to create a connection between nodejs and redisql i fall into problem when creating table something called TypeError: Wrong input type "String" for args
the stacktrace will be added at the end of post, i have added
const redisql = require('redisql');
const sqlClient = redisql.createClient();
sqlClient.create_table("employee", " id int , name TEXT , salary double ", redisql.print);
once i run the third command i get this stack trace
TypeError: Wrong input type "String" for args
at RedisClient.send_command.RedisClient.sendCommand (d:\BuildProjects\redisproject\node_modules\redis\lib\extendedApi.js:27:19)
at rsql_send_command (d:\BuildProjects\redisproject\node_modules\redisql\index.js:28:22)
at RedisClient.proto.CREATE_TABLE (d:\BuildProjects\redisproject\node_modules\redisql\index.js:47:5)
at new Cache (d:\BuildProjects\redisproject\app\index.js:11:15)
at Object.<anonymous> (d:\BuildProjects\redisproject\app\index.js:45:20)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (d:\BuildProjects\redisproject\index.js:1:77)
at Module._compile (module.js:649:14)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
Waiting for the debugger to disconnect...
TypeError: Wrong input type "String" for args
extendedApi.js:27
at RedisClient.send_command.RedisClient.sendCommand (d:\BuildProjects\redisproject\node_modules\redis\lib\extendedApi.js:27:19)
at rsql_send_command (d:\BuildProjects\redisproject\node_modules\redisql\index.js:28:22)
a
i followed this project link in order to build mine
have no idea why, i read deeply the code and it seems it should be okay. could someone help me ?

Resources