I am trying to use Meteor Jest stubs to unit test my server code containing Meteor modules (using jest) instead of manually stubbing out Meteor\Mongo DB uses in the application as explained here.
At ~\src\imports\api\collections.js
I have export const DomainCollection = new Mongo.Collection('domains');
In ~\src\imports\server\domainsService.test.js=>beforeEach
method, how can I mock DomainCollection
class here so it has {domain: "domain1" ,_id:1}
?
Can someone please help? I am sure I am missing something very obvious.
~\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';
**~\src\imports\server\domainsService.test.js**
describe('domainsService', () =>
{
beforeEach(() => {
//How can i mock DomainCollection class here so it has {domain: "domain1" ,_id:1}
}),
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"
},
"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/',
],
};