Jest Cannot find module 'pg-native' from 'client.js' - node.js

My project consists of node (express), knex, objectionjs with graphql.
We are using Jest to test calls to graphql which behind the scenes uses objectionjs+knex to perform updates on Postgres db.
Any jest tests calling an update to a table that has a JSON field gives the error in console:
console.error node_modules/pg/lib/index.js:52
Cannot find module 'pg-native' from 'client.js'
However, Jest was able to find:
'./client.js'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
The tests complete anyway but this is an annoyance that I would like to get rid of.
Has anyone come across this issue?
Let me know what additional code details I can provide to help diagnose the issue.
UPDATE:
Managed to make message disappear with the workaound suggested in this post:
https://github.com/sequelize/sequelize/issues/3781#issuecomment-522198459

Find in all files:
const { connect } = require('pg/lib/native');
And delete

Related

Nestjs versioning configuration when using Fastify

For performance reasons of a relatively complex Nestjs application, we have chosen to use Fastify as our HTTP provider.
We are at a stage where we need to version out api and are running into problems after following instructions on the standard Nestjs guide:
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(fastifyInstance),
{},
);
app.enableVersioning();
The error received is:
Property 'enableVersioning' does not exist on type 'NestFastifyApplication'.ts(2339)
I haven't been able to find a solution anywhere and thought I'd ask and see if anyone else has had the same problem and found a solution.
looks like you need to upgrade #nestjs/common because enableVersioning does exists in NestFastifyApplication:
https://github.com/nestjs/nest/blob/d5b6e489209090544a4f39c4f4a716b9800ca6a8/packages/platform-fastify/interfaces/nest-fastify-application.interface.ts#L16
https://github.com/nestjs/nest/blob/d5b6e489209090544a4f39c4f4a716b9800ca6a8/packages/common/interfaces/nest-application.interface.ts#L47

How to disable function logs on Gatsby?

So, I am new to gatsby and I am trying to build gatsby functions the way it is written in this documentation: https://www.gatsbyjs.com/docs/reference/functions/getting-started/
However, whenever I execute the function, it logs something like "Executed function "/api/test" in 27ms".
Now, since this will be a frequently called function in the future, I wish to disable the logs preferably just for this function OR for all the functions under /api folder.
Can anyone help please me out?
Try using gatsby-plugin-remove-console plugin:
module.exports = {
plugins: [
'gatsby-plugin-remove-console'
],
};
There isn't any built-in method of disabling logs as far as I know so try using the plugin. However, I'm not sure about its use in Gatsby functions...

How to properly test a module that requires newrelic with jest

I'm working migrating a bunch of unit tests from mockery to jest. When I jest a module that requires the new relic agent like so: require('newrelic'), I get downstream errors like :
- TypeError: Cannot convert undefined or null to object
at Object.<anonymous> (node_modules/newrelic/lib/config.js:165:33)
at Runtime._execModule (node_modules/jest-cli/src/Runtime/Runtime.js:261:17)
at Object.<anonymous> (node_modules/newrelic/lib/logger.js:18:14)
at Object.<anonymous> (node_modules/newrelic/index.js:3:14)
What is the best way to deal with modules like newrelic which jest has a hard time mocking? What have other people done when they have both jest and newrelic in their stack?
The route I ended up taking was to create a mock module for newrelic in my __mocks__ folder:
module.exports = {
addCustomParameter: jest.fn()
};
I will probably need to add more functions later, but for now this is enough. I still wonder if there is a way to get jest to auto mock the newrelic library without erroring.
I've seen this with a few modules were automocking fails for various reasons, although it seems to happen a lot less frequently in the newer versions of Jest.
As #linuxdan suggests you might be able to work around the issue by using the manual mocking functionality documented here.
To so this you'll probably want to just export an object with the expected methods generated using jest.fn().
The reason this will work is it will stop jest trying to determine the methods it needs to auto mock on the newrelic library. It will be during this process that it fails.

MEAN stack Angular service not being injected into application

I am relatively new to MEAN stack development having been working with it for the past few months. I have an issue I am trying to resolve that I have been researching extensively but cannot seem to get to the bottom of.
So, I have a MEAN stack application that I scaffolded using Yeomen. I created a route which has a view that I would like to show a record set using Angulars ng-repeat directive. I get the dataset via an http get call via an Angular service. However, when I run the app using Grunt I get the error:
Error: [$injector:nomod] Module 'Records' is not available!
I have put a reference to the service in my controller:
angular.module('recordsApp').controller('RecordsCtrl', function ($scope, $http, $filter, Records) {
I have also injected into it the app within the app.js file:
angular.module('recordsApp', [
'ngCookies',
'ngResource',
'ngSanitize',
'ngRoute',
'Records'
])
Can anyone please help point me in the right direction here as to what I am missing?
Thank you.
You don't need to include 'Records' module as your module dependancy, as you only have a service called Records in the same module
angular.module('recordsApp', [
'ngCookies',
'ngResource',
'ngSanitize',
'ngRoute'
])

How to test an AngularJS/SocketStream/Node.js app using Karma

I am working on an AngularJS application that is delivered by a SocketStream/node.js server.
I have an AngularJS service that calls api functions on the SocketStream server and progress has been good so far.
But now the time has come to start writing the first tests and the first testing framework that came to mind is Karma/Jasmine, since this is the recommend AngularJS set up.
So far so good, but since my AngularJS modules are imported using 'require' (SocketStream's version, not require.js) and server api calls are part of the test, I need to configure Karma to load SocketStream (at least its client side).
I took a good look at 'https://github.com/yiwang/angular-phonecat-livescript-socketstream' but when I run this example I get run time errors, possibly because I have later versions of variuous dependencies installed.
I managed to get 'required' resolved by packing my SocketStream app by adding 'ss.client.packAssets()' to app.js and run 'SS_PACK=1 node app.js', but when I start karma it logs an error message saying:
'Chrome 23.0 (Linux) ERROR
Uncaught TypeError: undefined is not a function
at /the...path/client/static/assets/app/1368026081351.js:25'
'1368026081351.js' is the SocketStream packed assets file. If I don't load it the error message is something like 'require is undefined', so my best guess is that the error is happening somewhere inside the SocketStream require code. Also because I run karma in DEBUG mode and can see all the files being served.
I have been trying different approaches as to find out what is happening but to now avail. So my questions are:
Is anybody else successfully testing AngularJS/SocketStream using Karma?
Does anybody have any suggestions as to how I can fix, or at least debug this problem?
Are there any alternatives/better solutions?
Time to answer, sort of, my own question:
Sort of, because I came to the conclusion that Karma and node.js/SocketStream have a lot of overlap, so I decided to see if I can omit Karma altogether and deliver the Jasmine testing platform through SocketStream. It turns out that that is possible and here's how I did it:
I defined a new SocketStream route and client in my 'app.js' file:
ss.client.define( 'test', {
view: 'SpecRunner.html',
css: ['libs/test'],
code: ['libs', 'tests', 'app'],
tmpl: 'none'
});
ss.http.route( '/test', function(req, res) {
res.serveClient( 'test' );
});
I downloaded jasmine-standalone-1.3.1.zip and copied 'SpecRunner.html' to the 'client/views' folder. I then edited it to make it load AngularJS and all SocketStream client files, like all other views:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular-resource.min.js"></script>
<SocketStream/>
I removed the 'script' tags that import the sample source files ( 'Player.js' and 'Song.js' ) and specs but let the last 'script' block in place unmodified.
I then created a new folder inside 'client/css/libs' called 'test' and copied 'jasmine.css' in there unmodified.
Then I copied 'jasmine.js' and 'jasmine-html.js' renamed to '01-jasmine.js' and '02-jasmine-html.js' but otherwise unmodified, into '/client/code/libs'.
Now Jasmine is in place and will be invoked by using the '/test' route. The slightly unsatisfactory bit is that I haven't found an elegant place to store my spec files. They only work so far if I place them inside the 'libs' folder. Anywhere else and they are served by SocketStream as modules and are not run.
But I can live with that for now. I can run Jasmine tests without having to configure a special Karma setup.

Resources