tomscholz / meteor-jest-stubs Goto Github PK
View Code? Open in Web Editor NEWThis project forked from orangecms/jest-meteor-stubs
Stubs for using Jest to unit test Meteor modules
License: MIT License
This project forked from orangecms/jest-meteor-stubs
Stubs for using Jest to unit test Meteor modules
License: MIT License
Hello,
I am trying to use meteor-jest-stubs with my simple test but I am getting a Unexpected token {
error. What am I doing wrong? Would you have a sample demo where we have used meteor-jest-stubs?
StackTrace:
import { check } from 'meteor/check';
^
SyntaxError: Unexpected token {
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
~\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(() => {
DomainCollection=jest.fn(() => {
return { "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 --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",
"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",
"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",
"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/',
],
};
yarn add -D meteor-jest-stubs
adds /node_modules/meteor-jest-stubs/lib
, but the README still mentions the original package's folder name (jest-meteor-stubs/lib
) in the modulePaths
stanza in the Usage example.
Should be updated to:
modulePaths: [ '<rootDir>/node_modules/', '<rootDir>/node_modules/meteor-jest-stubs/lib/', ]
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/',
],
};
I had this error:
Details:
/Users/ar/meteor-react-typescript-nightwatch/node_modules/meteor-jest-stubs/lib/meteor/react-meteor-data.js:3
export const withTracker = jest.fn(Op => jest.fn(C => createContainer(Op, C)));
^^^^^^
SyntaxError: Unexpected token export
And could fix it with inlcuding this module again for the transform:
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
transformIgnorePatterns: [
"<roodDir>/node_modules/meteor-jest-stubs"
],
If this is always the case, it should be included in the README.
See:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.