GithubHelp home page GithubHelp logo

tomscholz / meteor-jest-stubs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from orangecms/jest-meteor-stubs

10.0 2.0 15.0 139 KB

Stubs for using Jest to unit test Meteor modules

License: MIT License

JavaScript 100.00%

meteor-jest-stubs's Introduction

meteor-jest-stubs

This is a supported fork of orangecms/jest-meteor-stubs. Please note that, the package name is slightly different than the original one:

- jest-meteor-stubs
+ meteor-jest-stubs

About

Testing Meteor code is very slow when using the built-in meteor test command. These stubs will help you get rid of the dependencies which are not present in your project's node_modules directory. Use the setup below which does most of the actual magic. :)

Installation

npm i -D meteor-jest-stubs or yarn add -D meteor-jest-stubs

How it works

Jest preprocesses the files to be executed through the test runner. When pulling in dependencies, i.e. using require (in ES6 import), similar to the default behavior of Node.js, the resolver will look for them in a local directory named node_modules. However, Meteor injects additional modules when running meteor instead of plain Node.js, which do not reside in this directory. Jest allows you to overcome this issue by providing additional locations where the load should look for modules.

Usage

Add this module's lib/ directory to the modulePaths in your Jest config. For Meteor packages, you will need to use moduleNameMapper to rewrite the module names to use _ instead of : for the namespaces as : is not allowed by some file/operating systems. The configuration has to be in jest.config.js in Jest 20 because of a bug in the mapper.

Here is a working example:

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

meteor-jest-stubs's People

Contributors

arichter83 avatar armellarcier avatar callmephilip avatar chmac avatar mackbrowne avatar marcuslma avatar orangecms avatar smartcrash avatar tahoemph avatar tomscholz avatar voles avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

meteor-jest-stubs's Issues

Fixing "SyntaxError: Unexpected token export"

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:

Jest test is unable to import meteor libraries.

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/',
  ],
};

Update README for new package name

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/', ]

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

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/',
  ],
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.