#ngneat/spectator no longer works with jest and ESM modules - angular-spectator

When using #ngneat/spectator 11.0.1 with jest 28.1.0 and jest-preset-angular 12.1.0 and ESM modules, spectator throws an error saying jest isn't defined if I try to use the mocks property to createComponentFactory. So if I do this it fails:
const createComponent = createComponentFactory({
component: HomeComponent,
detectChanges: false,
imports: [CommonModule],
mocks: [RoleService]
})
whereas if I provide something directly, like this, I don't get a jest error:
const createComponent = createComponentFactory({
component: HomeComponent,
detectChanges: false,
imports: [CommonModule],
providers: [{provide: RoleService, useValue: { editor: false }}]
})
ReferenceError: jest is not defined
at node_modules/projects/spectator/jest/src/lib/mock.ts:15:20.
at installProtoMethods (node_modules/projects/spectator/src/lib/mock.ts:55:19)
at createSpyObject (node_modules/projects/spectator/jest/src/lib/mock.ts:14:3)
at Object.useFactory (node_modules/projects/spectator/jest/src/lib/mock.ts:44:23)

Related

Mongoose Error: Cannot read property 'length' of undefined in NestJs

I am using nests framework and versions of mongodb and mongoose are as specified below.
Please refer to the screenshot for error in detail.
versions
"mongodb": "4.0.0",
"mongoose": "5.5.12",
Error Screenshot
User Document Module
import { Module } from '#nestjs/common';
import { UserDocumentsService } from './user-documents.service';
import { UserDocumentsController } from './user-documents.controller';
import { MongooseModule } from '#nestjs/mongoose';
import { UserDocumentsSchema } from './schema/user-documents.schema';
#Module({
imports: [
// showing error on this line
MongooseModule.forFeature([
{ name: 'UserDocument', schema: UserDocumentsSchema },
]),
],
controllers: [UserDocumentsController],
providers: [UserDocumentsService],
})
export class UserDocumentsModule {}
App.module.ts
#Module({
imports: [
MongooseModule.forRootAsync({
imports: [SharedModule],
useFactory: async (configService: ConfigService) => ({
uri: configService.mongoDBName(),
useNewUrlParser: true,
useFindAndModify: false,
}),
inject: [ConfigService],
}),
UserDocumentsModule,
],
providers: [AppGateway],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer): MiddlewareConsumer | void {
consumer.apply(contextMiddleware).forRoutes('*');
}
}
UPDATE
I think there is something wrong with the mongoose imports in the schema file. It says "could not find declaration for module 'mongoose'".
I tried removing and reinstalling mongoose and it's types. But now it shows new error.
I tried solutions mentioned in this post:
Node.js heap out of memory
But this also didn't work for me.
I'm using Mac-M1 with 8GB config.
UPDATE
The issue has been resolved now. The project is running on node v10.24.1 and I was using node v16.6.2.
After downgrading node version using NVM, this issue is gone.
You'll have to pull SharedModule import off MongooseModule.
Try this:
#Module({
imports: [
MongooseModule.forRootAsync({
useFactory: async (configService: ConfigService) => ({
uri: configService.mongoDBName(),
useNewUrlParser: true,
useFindAndModify: false,
}),
inject: [ConfigService],
}),
UserDocumentsModule,
SharedModule
],
providers: [AppGateway],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer): MiddlewareConsumer | void {
consumer.apply(contextMiddleware).forRoutes('*');
}
}
It was because I was using a wrong version of node. The project was built on node v10.24.1 and I was using node v16.6.2.
After downgrading node version using NVM, I was able to fix this issue.

ConfigModule isn't working properly in nestjs

I am working on nestjs and mongoose recently. I put my MongoDB connection links on .env file. I have configured my nestjs application using ConfigModule. I followed the nestjs docs. But I have no idea why env variables are not working for me.
I have one extra module.ts file called content.module.ts in which I am connecting to mongodb. I have configured this ConfigModule in my app.module.ts and put it as global.
// app.module.ts
#Module({
imports: [
ConfigModule.forRoot({
envFilePath: './config/.env',
isGlobal: true,
}),
ContentModule, //another module
],
})
export class AppModule {}
// content.module.ts
#Module({
imports: [
MongooseModule.forRoot(process.env.CONTENT_DB_CONNECTION_STRING),
MongooseModule.forFeature([{ name: 'App', schema: AppSchema },]),
],
})
export class ContentModule {}
When I run my project using the debugger, I found that MongooseModule was first getting executed in the content.module.ts file, and then configModule was getting executed.
I even tried to configure the configModule in the content.module.ts file itself like below but no luck.
#Module({
imports: [
ConfigModule.forRoot({
envFilePath: '../../config/.env',
}),
MongooseModule.forRoot(process.env.CONTENT_DB_CONNECTION_STRING),
MongooseModule.forFeature([{ name: 'App', schema: AppSchema },]),
],
})
export class ContentModule {}
.env file
PORT=3000
CONTENT_DB_CONNECTION_STRING=mongodb://localhost:27017/content
project structure
I didn't get why mongoose dependencies are getting executed before the nestjs dependencies? And I also need help to fix this env variable.

Jest Test Support for the experimental syntax 'jsx' isn't currently enabled

I am writing first tests for my app and just install Jest.
My test is pretty simple, so I don't think the error I am getting is coming from there.
import React from 'react';
import renderer from 'react-test-renderer';
import FancyInput from './FancyInput';
describe('FancyInput', () => {
it('should render correctly', () => {
expect(
renderer.create(
<FancyInput />
)
).toMatchSnapshot();
});
});
Error when run the test is Support for the experimental syntax 'jsx' isn't currently enabled
also
`Add #babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add #babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.`
My webpack file has the following thing:
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
'#babel/preset-react',
],
plugins: [
["#babel/plugin-proposal-decorators", { "legacy": true }],
["#babel/plugin-proposal-optional-chaining"],
["#babel/plugin-syntax-jsx"],
]
}
}
},
also my package.json has all the plugins I believe are necessary
"#babel/plugin-proposal-class-properties": "^7.10.4",
"#babel/plugin-proposal-decorators": "^7.4.4",
"#babel/plugin-proposal-optional-chaining": "^7.11.0",
"#babel/plugin-syntax-jsx": "^7.10.4",
"#babel/preset-react": "^7.0.0",
what am I missing here ?

Ionic tab app throws error on new tab add

I am trying to add tabs in my exiting Ionic app:
directory structure:
- src/pages/admin/dashboard
- src/pages/admin/tabs
- ..../tabs/ tabs.module.ts , tabs.page.html , tabes.page.ts , tabs.router.module.ts
tabs.module.ts code:
import { DashboardPage } from '../dashboard/dashboard.page';
#NgModule({
imports: [
IonicModule,
CommonModule,
FormsModule,
TabsPageRoutingModule,
DashboardPage
],
declarations: [TabsPage]
})
export class TabsPageModule {}
tabs.router.module.ts code:
const routes: Routes = [
{
path: 'tabs',
component: TabsPage,
children: [
{
path: 'dashboard',
outlet: 'dashboard',
loadChildren: '../dashboard/dashboard.module#DashboardPageModule'
}
]
}
];
#NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class TabsPageRoutingModule {}
Tried all sources googles its changes as per the error message
but still stuck with below error message:
core.js:12501 ERROR Error: Uncaught (in promise): Error: Unexpected directive 'DashboardPage' imported by the module 'TabsPageModule'. Please add a #NgModule annotation.
Error: Unexpected directive 'DashboardPage' imported by the module 'TabsPageModule'. Please add a #NgModule annotation.
app environment ionic 4
Thanks for any help or hint
Your problem is in your error code:
Unexpected directive 'DashboardPage' imported by the module 'TabsPageModule'.
In tabs.module.ts you need to import the PageModule instead of the page itself.
So change DashboardPage to DashboardPageModule.

Ionic, The pipe 'translate' could not be found, only in AoT Compiler

I have a problem with ngx-translate within a Ionic App when I try to build a production release.
During development translations work fine with
ionic serve
and
ionic cordova build android
but if I try to make a production build using
ionic cordova build android --prod --release
or for the PWA
ionic build --prod
I get some errors like
[17:47:15] typescript error
The pipe 'translate' could not be found ( ... )
Versions:
ngx-translate: 8.0.0
Ionic: 3.9.2
Angular: 5.2.10
I checked with: https://github.com/ngx-translate/core/tree/v8.0.0 and can't find what I am doing wrong.
In app.module:
// https://github.com/ngx-translate/core#1-import-the-translatemodule
// AoT requires an exported function for factories
export function createTranslateLoader(http: Http) {
return new TranslateHttpLoader( http, './assets/i18n/', '.json' );
}
#NgModule( {
...
imports: [
...
TranslateModule.forRoot( {
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [ Http ]
}
}
),
...
And in one of my pages which produce this error:
import { TranslateModule, TranslateService, TranslatePipe } from '#ngx-translate/core';
#NgModule({
imports: [
TranslateModule
],
exports: [
TranslateModule
]
})
export class ContactPage { ... }
Searching through SO produced mainly the hint to import & export the TranslateModule in the page as well as importing the TranslatePipe, but both of these changes didn't solve my problem. How do I configure the translation module so that it works in a production build?
OK, solved it. I added the import & export statement of the TranslateModule to the page class, but correct is to add it the the corresponding module.
In my example:
Wrong:
contact.ts
#NgModule({
imports: [
TranslateModule
],
exports: [
TranslateModule
]
})
export class ContactsPage {...}
Correct:
contact.module.ts
#NgModule( {
...
imports : [
...
TranslateModule
],
exports : [
TranslateModule
]
} )
export class ContactsPageModule {
}