tomscholz / meteor-jest-stubs

Stubs for using Jest to unit test Meteor modules
MIT License
10 stars 15 forks source link

Using meteor-jest-stubs to mock meteor, mongo collections. #13

Open ajitgoel opened 4 years ago

ajitgoel commented 4 years ago

Do we have an example of how to use meteor-jest-stubs to mock meteor, mongo collections? Somehow I am unable to wrap my head around how to use this.

~\src\imports\server\domainsService.test.js

'use strict';
import {check} from 'meteor/check';
import {Mongo} from 'meteor/mongo';
import {DomainCollection} from '../api/collections';
import domainsService from './domainsService';

describe('domainsService', () => 
{
  beforeEach(() => {
    **//How can i mock DomainCollection class here.** 
  }),
  test.only('doesDomainExist1', () => 
  {
    expect(domainsService.doesDomainExist("domain1")).toBe(true);
  }); 
  test.only('doesDomainExist2', () => 
  {
    expect(domainsService.doesDomainExist("domain2")).toBe(false);
  }); 
})

~\src\imports\server\domainsService.js

'use strict';
import { check } from 'meteor/check';
import { Mongo } from 'meteor/mongo';
import { UserCollection,UserDomainCollection,DomainCollection } from '../api/collections';
import { MeteorErrors, StateVariables, SecureRoutes, NonEmptyString} from '../api/constants';

export const domainsService = 
{
  doesDomainExist(domain) 
  {
    check(domain, NonEmptyString);    
    domain=domain.toString().toLowerCase(); 
    let domainExist= DomainCollection.find({"domain":domain}, {_id: 1}).count()>0?true:false; 
    return domainExist;
  },
}

~\src\imports\api\collections.js

import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';
import SimpleSchema from 'simpl-schema';
export const DomainCollection = new Mongo.Collection('domains');
**package.json:**

{
  "name": "vue-meteor-demo",
  "private": true,
  "scripts": {
    "test": "jest src/imports/server --coverage --watch"
  },
  "jest": {
    "collectCoverage": true,
    "coverageReporters": [
      "html"
    ]
  },
  "dependencies": {
    "@babel/runtime": "^7.7.4",
    "@riophae/vue-treeselect": "^0.4.0",
    "@syncfusion/ej2-vue-grids": "^17.4.40",
    "@syncfusion/ej2-vue-navigations": "^17.4.39",
    "aws-sdk": "^2.556.0",
    "babel-jest": "^24.9.0",
    "bcrypt": "^3.0.6",
    "bootstrap": "^4.4.1",
    "core-js": "^3.3.2",
    "cross-env": "^6.0.3",
    "faker": "^4.1.0",
    "generate-password": "^1.4.2",
    "graphql": "^14.5.8",
    "graphql-tag": "^2.10.1",
    "intersection-observer": "^0.6.0",
    "isomorphic-fetch": "^2.2.1",
    "jest-cli": "^23.6.0",
    "marker-clusterer-plus": "^2.1.4",
    "meteor-jest-stubs": "^2.1.0",
    "meteor-node-stubs": "^1.0.0",
    "moment": "^2.24.0",
    "papaparse": "^5.1.1",
    "pug": "^2.0.4",
    "simpl-schema": "^1.5.6",
    "vue": "^2.5.21",
    "vue-googlemaps": "^0.1.2",
    "vue-meteor-tracker": "^2.0.0-beta.5",
    "vue-observe-visibility": "^0.4.6",
    "vue-router": "^3.1.3",
    "vue-supply": "^0.3.0",
    "vuelidate": "^0.7.4",
    "vuex": "^3.1.2",
    "vuex-router-sync": "^5.0.0",
    "winston": "^3.2.1",
    "winston-loggly-bulk": "^3.0.1"
  },
  "devDependencies": {
    "@types/meteor": "^1.4.37",
    "@types/mocha": "^5.2.7",
    "@types/node": "^12.12.21",
    "@types/underscore": "^1.9.4",
    "babel-helper-vue-jsx-merge-props": "^2.0.3",
    "babel-plugin-syntax-jsx": "^6.18.0",
    "babel-plugin-transform-vue-jsx": "^3.7.0",
    "babel-preset-es2015": "^6.24.1",
    "chai": "^4.2.0",
    "cypress": "^3.8.2",
    "jest": "^24.9.0",
    "meteor-typings": "^1.4.1",
    "sinon": "^7.5.0",
    "start-server-and-test": "^1.10.6",
    "ts-node": "^8.5.4",
    "tslint": "^5.20.1",
    "typescript": "^3.7.4",
    "vue-template-compiler": "2.6.10"
  }
}

~\jest.config.js:

module.exports = {
  transform: {
    '^.+\\.jsx?$': 'babel-jest',
  },
  moduleFileExtensions: [
    'js',
    'jsx',
  ],
  modulePaths: [
    '<rootDir>/node_modules/',
    '<rootDir>/node_modules/meteor-jest-stubs/lib/',
  ],
  moduleNameMapper: {
    '^(.*):(.*)$': '$1_$2',
  },
  unmockedModulePathPatterns: [
    '/^imports\\/.*\\.jsx?$/',
    '/^node_modules/',
  ],
};
karina-y commented 3 years ago

did you end up finding a solution for this? with mongo i just get Cannot find module 'meteor/mongo' from 'imports/api/Water/Water.js'