user16373392
user16373392

Reputation:

UnitTesting with TypeScript and Jest (Mongoose plugin problems)

I am trying to make ts-jest working but I am getting an error from not really related to the method functionality.

I've got a class ProductService with a demo method ``

import { 
    ProductModel, 
    ProductPriceModel, 
    ProductTypes, 
    ProductVariantModel,
} from "../models/Product"

class ProductService {

    async testJest(input: string) {
        return input.split('').reverse().join('')
    }

}

export const productService = new ProductService();

My test file


import { productService } from '../../src/services/product.service'

describe('Product service', () => {

    describe('testMethod', () => {

        describe('type field', () => {

            it('case 1', async () => {
                expect(await productService.testJest('Hello world!')).toBe('!dlrow olleH')
            })
    
        })

    })


})

and the weird error I am getting the error I am getting

That's my Schema but it works fine actually when I run the product

import { Schema, Document, model, Types } from "mongoose"
import { plugin as autocomplete } from 'mongoose-auto-increment'

const ProductSchema: Schema<Document<IProduct>> = new Schema({
    article: { type: String },
    name: { type: String },
    category: { type: Types.ObjectId, ref: 'ProductCategory' },
    group: { type: Types.ObjectId, ref: 'ProductGroup' },
    type: { type: ProductTypes, required: true },
    variants: [{ type: Types.ObjectId, ref: 'ProductVariant' }],
    description: { type: String },
    grind: { type: Boolean, default: false },
    productDetails: { type: Object },
    productImage: { type: String },
}, { collection: "products", timestamps: true })

ProductSchema.plugin(autocomplete, {
    model: 'Product',
    field: 'article',
    startAt: 10000,
})

export const ProductModel = model('Product', ProductSchema)

I have already tried initialize the database connection (what I think it should not be a good practice) but it didn't help.

I hope you have any ideas of how I can make my tests working with my setup - TypeScript + MongoDB (Mongoose) with plugins

update

updated manual db start

I'm having the same error when I am trying to run it with manual db launch. but only this time I ran it with a '--detectOpenHandles' flag

Upvotes: 0

Views: 358

Answers (0)

Related Questions