mongo-mock icon indicating copy to clipboard operation
mongo-mock copied to clipboard

Typescript Typedefs

Open dyst5422 opened this issue 8 years ago • 9 comments

Would be worth adding typescript definitions to DefinitelyTyped.

Should be easy enough to take the MongoDB typings and simply cut out the parts that aren't implemented for mongo-mock.

dyst5422 avatar Jul 20 '17 17:07 dyst5422

I'd see these working with the MongoDB typings, using extends and such.

Downchuck avatar Mar 19 '19 18:03 Downchuck

One (temporary) way to solve it, is to declare your own typings.

src/@types/mongo-mock.d.ts

declare module 'mongo-mock' {
  import type { MongoClient as IMongoClient } from 'mongodb'

  export class MongoClient extends IMongoClient {}
}

src/infra/db/utils

import { MongoClient as MongoClientMock } from 'mongo-mock'

export const MongoHelper = {
  async connect(databaseUri: string) {
    this.client = await MongoClientMock.connect(databaseUri)
                                     // ^^^^^^^ intelisense here
  }
}

TS version: 4.9.3

ricardo-cavalheiro avatar Dec 15 '22 14:12 ricardo-cavalheiro

I would love to get some more background on how people are using this lib that requires typings.

Since I'd imagine most people would be mocking the mongodb module with this, I don't think that typechecking would be a problem. If we have typings, should they mirror MongoDB exactly, or just the parts that are currently implemented?

For reference, this is how we are using this library in our tests:

  require('mongodb').MongoClient = require('mongo-mock').MongoClient
  require('mongodb').ObjectId = require('mongo-mock').ObjectId

LinusU avatar Dec 15 '22 15:12 LinusU

I would love to get some more background on how people are using this lib that requires typings.

Since I'd imagine most people would be mocking the mongodb module with this, I don't think that typechecking would be a problem. If we have typings, should they mirror MongoDB exactly, or just the parts that are currently implemented?

For reference, this is how we are using this library in our tests:

  require('mongodb').MongoClient = require('mongo-mock').MongoClient
  require('mongodb').ObjectId = require('mongo-mock').ObjectId

I have never seen someone do this to mock out modules. I don't know how I feel about it

dyst5422 avatar Dec 15 '22 15:12 dyst5422

I have never seen someone do this to mock out modules. I don't know how I feel about it

Yeah, it's a bit hacky 😅

There is also proxyquire which is a bit cleaner.

I really like that the code being tested doesn't have to be adapted to be testable though, as that introduces more code paths and possibilities for subtle bugs.


How do you use mongo-mock?

LinusU avatar Dec 15 '22 18:12 LinusU

@LinusU It kind sucks not knowing what methods the lib is exposing. I guess only the mongo-mock related typings should be added, otherwise one might end up calling a method which is not supported by lib.

ricardo-cavalheiro avatar Dec 15 '22 20:12 ricardo-cavalheiro

It kind sucks not knowing what methods the lib is exposing.

Yes that is unfortunate.

I guess only the mongo-mock related typings should be added, otherwise one might end up calling a method which is not supported by lib.

How are you using mongo-mock? I'd imagine that one would write the code to target the real mongodb, since otherwise the code wouldn't do anything, so I'm not sure that having partial typings in this project would solve the above problem?

LinusU avatar Dec 16 '22 13:12 LinusU

@LinusU Right now I'm just testing the business logic via integration tests. In practice, I only use insertOne, findOne, db().collection(), etc to make sure the data is being saved/retrieved. I'm not testing more advanced features like aggregation and stuff like that. The main reason I'm using this lib is because it's an in-memory copy of MongoDB and so I don't need to have a MongoDB instance running somewhere.

ricardo-cavalheiro avatar Dec 16 '22 13:12 ricardo-cavalheiro

@ricardo-passos how is your business logic typed? Are you passing a MongoClient instance to it? In that case, what would you want to do with types in this library? If we only typed the functions actually supported by this library you'd get a type error trying to pass it to your business logic? 🤔

LinusU avatar Dec 20 '22 08:12 LinusU