├── .github ├── issue_template.md └── pull_request_template.md ├── .gitignore ├── .travis.yml ├── .yarnrc ├── LICENSE ├── README.md ├── appveyor.yml ├── circle.yml ├── examples ├── basic │ ├── package.json │ └── src │ │ └── models │ │ ├── User.js │ │ └── __tests__ │ │ └── User.spec.js └── setup │ ├── jest-plugins.js │ ├── package.json │ └── src │ └── __tests__ │ └── install.spec.js ├── jest-plugins.sublime-project ├── lerna.json ├── package.json ├── packages ├── jest-plugin-action │ ├── README.md │ ├── package.json │ ├── setup.js │ └── src │ │ ├── __tests__ │ │ └── action.spec.js │ │ └── action.js ├── jest-plugin-clock │ ├── README.md │ ├── package.json │ ├── setup.js │ └── src │ │ ├── __tests__ │ │ └── clock.spec.js │ │ └── clock.js ├── jest-plugin-console-matchers │ ├── README.md │ ├── package.json │ ├── setup.js │ └── src │ │ ├── __tests__ │ │ └── matchers.spec.js │ │ └── matchers.js ├── jest-plugin-context │ ├── README.md │ ├── package.json │ ├── setup.js │ └── src │ │ ├── __tests__ │ │ └── context.spec.js │ │ └── context.js ├── jest-plugin-enzyme │ ├── README.md │ ├── package.json │ ├── setup.js │ └── src │ │ └── enzyme.js ├── jest-plugin-for-each │ ├── README.md │ ├── package.json │ ├── setup.js │ └── src │ │ ├── __tests__ │ │ └── forEach.spec.js │ │ └── forEach.js ├── jest-plugin-fs │ ├── README.md │ ├── mock.js │ ├── package.json │ ├── setup.js │ └── src │ │ ├── __fixtures__ │ │ └── test.txt │ │ ├── __tests__ │ │ └── fs.spec.js │ │ └── fs.js ├── jest-plugin-it-renders │ ├── README.md │ ├── package.json │ ├── setup.js │ └── src │ │ ├── __tests__ │ │ ├── __snapshots__ │ │ │ └── itRenders.spec.js.snap │ │ └── itRenders.spec.js │ │ └── itRenders.js ├── jest-plugin-it-shallow-renders │ ├── README.md │ ├── package.json │ ├── setup.js │ └── src │ │ ├── __tests__ │ │ ├── __snapshots__ │ │ │ └── itShallowRenders.spec.js.snap │ │ └── itShallowRenders.spec.js │ │ └── itShallowRenders.js ├── jest-plugin-its │ ├── README.md │ ├── package.json │ ├── setup.js │ └── src │ │ ├── __tests__ │ │ └── its.spec.js │ │ ├── index.js │ │ ├── its.js │ │ └── subject.js ├── jest-plugin-set │ ├── README.md │ ├── package.json │ ├── setup.js │ └── src │ │ ├── __tests__ │ │ └── set.spec.js │ │ ├── define.js │ │ ├── set.js │ │ └── undefine.js ├── jest-plugin-unhandled-promise │ ├── README.md │ ├── package.json │ ├── setup.js │ └── src │ │ └── install.js ├── jest-plugins-react │ ├── README.md │ ├── __tests__ │ │ ├── __snapshots__ │ │ │ └── plugins.spec.js.snap │ │ └── plugins.spec.js │ ├── package.json │ └── setup.js ├── jest-plugins-recommended │ ├── README.md │ ├── __tests__ │ │ └── plugins.spec.js │ ├── package.json │ └── setup.js ├── jest-plugins-rspec │ ├── README.md │ ├── __tests__ │ │ └── plugins.spec.js │ ├── package.json │ └── setup.js └── jest-plugins │ ├── README.md │ ├── package.json │ ├── setup.js │ └── src │ └── install.js └── yarn.lock /.github/issue_template.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | 3 | ## Screenshot 4 | 5 | ## Test Plan 6 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | ## Issues Fixed 2 | 3 | Fix # 4 | 5 | ## Description 6 | 7 | ## Screenshot 8 | 9 | ## TODO 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # macOS 2 | .DS_Store 3 | 4 | # Editors 5 | .idea 6 | *.sublime-workspace 7 | 8 | # General 9 | *.log 10 | 11 | # JS 12 | .eslintcache 13 | build 14 | coverage 15 | node_modules 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | node_js: 4 | - 8 5 | 6 | cache: 7 | yarn: true 8 | directories: 9 | - .eslintcache 10 | - node_modules 11 | 12 | before_install: 13 | - curl -o- -L https://yarnpkg.com/install.sh | bash 14 | - export PATH="$HOME/.yarn/bin:$PATH" 15 | 16 | script: 17 | - yarn run clean 18 | - yarn run build 19 | - yarn lint:cached 20 | - yarn test 21 | -------------------------------------------------------------------------------- /.yarnrc: -------------------------------------------------------------------------------- 1 | workspaces-experimental true 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Mark Miyashita 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jest Plugins 2 | 3 | [![CircleCI](https://img.shields.io/circleci/project/github/negativetwelve/jest-plugins.svg?label=circle)](https://circleci.com/gh/negativetwelve/jest-plugins) 4 | [![Travis](https://img.shields.io/travis/negativetwelve/jest-plugins.svg?label=travis)](https://travis-ci.org/negativetwelve/jest-plugins) 5 | [![AppVeyor](https://img.shields.io/appveyor/ci/negativetwelve/jest-plugins/master.svg?label=appveyor)](https://ci.appveyor.com/project/negativetwelve/jest-plugins) 6 | [![Coveralls](https://img.shields.io/coveralls/negativetwelve/jest-plugins.svg)](https://coveralls.io/github/negativetwelve/jest-plugins?branch=master) 7 | 8 | Adds plugins feature to jest for easily adding extensions. 9 | 10 | ## Getting Started 11 | 12 | [The full instructions for utilizing `jest-plugins` in your project are located here.](/packages/jest-plugins#readme) 13 | 14 | ## Packages 15 | 16 | The Jest Plugins repo is managed as a monorepo that is composed of many npm packages. 17 | 18 | ### Core Packages 19 | 20 | Plugin | Version | Description 21 | -------|---------|------------ 22 | [`jest-plugins`](/packages/jest-plugins) | [![npm](https://img.shields.io/npm/v/jest-plugins.svg)][npm-plugins] | Adds the `plugins` feature to Jest. 23 | [`jest-plugins-recommended`](/packages/jest-plugins-recommended) | [![npm](https://img.shields.io/npm/v/jest-plugins-recommended.svg)][npm-recommended] | Recommended set of jest plugins. 24 | 25 | [npm-plugins]: https://www.npmjs.com/package/jest-plugins 26 | [npm-recommended]: https://www.npmjs.com/package/jest-plugins-recommended 27 | 28 | ### Plugin Sets 29 | 30 | Plugin | Version | Description 31 | -------|---------|------------ 32 | [`jest-plugins-react`](/packages/jest-plugins-react) | [![npm](https://img.shields.io/npm/v/jest-plugins-react.svg)][npm-react] | Jest plugins for testing React components. 33 | [`jest-plugins-rspec`](/packages/jest-plugins-rspec) | [![npm](https://img.shields.io/npm/v/jest-plugins-rspec.svg)][npm-rspec] | Jest plugins for RSpec syntax. 34 | 35 | [npm-react]: https://www.npmjs.com/package/jest-plugins-react 36 | [npm-rspec]: https://www.npmjs.com/package/jest-plugins-rspec 37 | 38 | ### Plugins 39 | 40 | Plugin | Version | Description 41 | -------|---------|------------ 42 | [`jest-plugin-action`](/packages/jest-plugin-action) | [![npm](https://img.shields.io/npm/v/jest-plugin-action.svg)][npm-action] | Declarative method for testing actions using jest. 43 | [`jest-plugin-console-matchers`](/packages/jest-plugin-console-matchers) | [![npm](https://img.shields.io/npm/v/jest-plugin-console-matchers.svg)][npm-console-matchers] | Adds `console` matchers to expect `error`, `info`, `log`, or `warn`. 44 | [`jest-plugin-context`](/packages/jest-plugin-context) | [![npm](https://img.shields.io/npm/v/jest-plugin-context.svg)][npm-context] | Adds `context` as an alternative to `describe` for jest. 45 | [`jest-plugin-for-each`](/packages/jest-plugin-for-each) | [![npm](https://img.shields.io/npm/v/jest-plugin-for-each.svg)][npm-for-each] | Test multiple values for a single outcome. 46 | [`jest-plugin-fs`](/packages/jest-plugin-fs) | [![npm](https://img.shields.io/npm/v/jest-plugin-fs.svg)][npm-fs] | Mock out the filesystem in your tests. 47 | [`jest-plugin-it-renders`](/packages/jest-plugin-it-renders) | [![npm](https://img.shields.io/npm/v/jest-plugin-it-renders.svg)][npm-it-renders] | Easily test that your React components render. 48 | [`jest-plugin-its`](/packages/jest-plugin-its) | [![npm](https://img.shields.io/npm/v/jest-plugin-its.svg)][npm-its] | Adds `its` helper to quickly check `subject` properties. 49 | [`jest-plugin-set`](/packages/jest-plugin-set) | [![npm](https://img.shields.io/npm/v/jest-plugin-set.svg)][npm-set] | Declaratively `set` your variables lazily. Implements `let` from RSpec. 50 | [`jest-plugin-unhandled-promise`](/packages/jest-plugin-unhandled-promise) | [![npm](https://img.shields.io/npm/v/jest-plugin-unhandled-promise.svg)][npm-unhandled-promise] | Surfaces unhandled promise rejections in jest tests. 51 | 52 | [npm-action]: https://www.npmjs.com/package/jest-plugin-action 53 | [npm-console-matchers]: https://www.npmjs.com/package/jest-plugin-console-matchers 54 | [npm-context]: https://www.npmjs.com/package/jest-plugin-context 55 | [npm-for-each]: https://www.npmjs.com/package/jest-plugin-for-each 56 | [npm-fs]: https://www.npmjs.com/package/jest-plugin-fs 57 | [npm-it-renders]: https://www.npmjs.com/package/jest-plugin-it-renders 58 | [npm-its]: https://www.npmjs.com/package/jest-plugin-its 59 | [npm-set]: https://www.npmjs.com/package/jest-plugin-set 60 | [npm-unhandled-promise]: https://www.npmjs.com/package/jest-plugin-unhandled-promise 61 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - nodejs_version: 8 4 | 5 | install: 6 | - ps: Install-Product node $env:nodejs_version 7 | - yarn 8 | - yarn run clean 9 | - yarn run build 10 | 11 | cache: 12 | - .eslintcache 13 | - node_modules 14 | 15 | test_script: 16 | - yarn lint:cached 17 | - yarn test 18 | 19 | build: off 20 | -------------------------------------------------------------------------------- /circle.yml: -------------------------------------------------------------------------------- 1 | machine: 2 | node: 3 | version: 8 4 | environment: 5 | PATH: ${PATH}:${HOME}/${CIRCLE_PROJECT_REPONAME}/node_modules/.bin 6 | 7 | dependencies: 8 | cache_directories: 9 | - ~/.cache/yarn 10 | override: 11 | - curl -o- -L https://yarnpkg.com/install.sh | bash 12 | - yarn install --force 13 | - yarn clean 14 | - yarn build 15 | 16 | test: 17 | pre: 18 | - yarn lint:cached 19 | override: 20 | - yarn test:coverage 21 | post: 22 | - yarn coveralls 23 | -------------------------------------------------------------------------------- /examples/basic/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugins-example-basic", 3 | "version": "2.9.0", 4 | "private": true, 5 | "devDependencies": { 6 | "jest": "^20.0.4", 7 | "jest-plugin-action": "^2.9.0", 8 | "jest-plugin-context": "^2.9.0", 9 | "jest-plugin-its": "^2.9.0", 10 | "jest-plugin-set": "^2.9.0", 11 | "jest-plugins": "^2.9.0" 12 | }, 13 | "jest": { 14 | "setupFiles": [ 15 | "jest-plugin-action/setup", 16 | "jest-plugin-context/setup", 17 | "jest-plugin-its/setup", 18 | "jest-plugin-set/setup" 19 | ], 20 | "testEnvironment": "node" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /examples/basic/src/models/User.js: -------------------------------------------------------------------------------- 1 | class User { 2 | 3 | constructor(props) { 4 | this.props = props; 5 | } 6 | 7 | getFirstName() { 8 | return this.props.firstName; 9 | } 10 | 11 | getLastName() { 12 | return this.props.lastName; 13 | } 14 | 15 | getName() { 16 | return `${this.getFirstName()} ${this.getLastName()}`; 17 | } 18 | 19 | } 20 | 21 | 22 | export default User; 23 | -------------------------------------------------------------------------------- /examples/basic/src/models/__tests__/User.spec.js: -------------------------------------------------------------------------------- 1 | // Models 2 | import User from '../User'; 3 | 4 | 5 | /* eslint-disable no-undef */ 6 | describe('User', () => { 7 | set('firstName', () => 'Joe'); 8 | set('lastName', () => 'Wilson'); 9 | set('user', () => new User({firstName, lastName})); 10 | 11 | describe('#getFirstName', () => { 12 | it('should return the firstName', () => { 13 | expect(user.getFirstName()).toEqual('Joe'); 14 | }); 15 | }); 16 | 17 | describe('#getLastName', () => { 18 | it('should return the lastName', () => { 19 | expect(user.getLastName()).toEqual('Wilson'); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /examples/setup/jest-plugins.js: -------------------------------------------------------------------------------- 1 | require('jest-plugins')([ 2 | 'jest-plugin-action', 3 | 'jest-plugin-console-matchers', 4 | 'jest-plugin-context', 5 | 'jest-plugin-its', 6 | 'jest-plugin-set', 7 | 'jest-plugin-unhandled-promise', 8 | ]); 9 | -------------------------------------------------------------------------------- /examples/setup/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugins-example-setup", 3 | "version": "2.9.0", 4 | "private": true, 5 | "devDependencies": { 6 | "jest": "^20.0.4", 7 | "jest-plugin-action": "^2.9.0", 8 | "jest-plugin-console-matchers": "^2.9.0", 9 | "jest-plugin-context": "^2.9.0", 10 | "jest-plugin-its": "^2.9.0", 11 | "jest-plugin-set": "^2.9.0", 12 | "jest-plugin-unhandled-promise": "^2.9.0", 13 | "jest-plugins": "^2.9.0" 14 | }, 15 | "jest": { 16 | "setupTestFrameworkScriptFile": "/jest-plugins.js", 17 | "testEnvironment": "node" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /examples/setup/src/__tests__/install.spec.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable no-undef */ 2 | describe('installation', () => { 3 | describe('e2e', () => { 4 | it('installs plugins without erroring', () => { 5 | expect(1 + 1).toEqual(2); 6 | }); 7 | }); 8 | 9 | describe('set', () => { 10 | set('a', () => 1); 11 | 12 | it('installs set properly', () => { 13 | expect(a).toEqual(1); 14 | }); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /jest-plugins.sublime-project: -------------------------------------------------------------------------------- 1 | { 2 | "folders": 3 | [ 4 | { 5 | "path": ".", 6 | "folder_exclude_patterns": 7 | [ 8 | "coverage", 9 | "node_modules", 10 | "packages/*/build" 11 | ], 12 | } 13 | ], 14 | "settings": 15 | { 16 | "alignment_chars": 17 | [ 18 | "=", 19 | ":" 20 | ], 21 | "alignment_space_chars": 22 | [ 23 | "=", 24 | ":" 25 | ], 26 | "detect_indentation": false, 27 | "ensure_newline_at_eof_on_save": true, 28 | "rulers": [80], 29 | "tab_size": 2, 30 | "translate_tabs_to_spaces": true, 31 | "trim_trailing_white_space_on_save": true, 32 | "word_wrap": true, 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /lerna.json: -------------------------------------------------------------------------------- 1 | { 2 | "lerna": "2.0.0", 3 | "version": "2.9.0", 4 | "npmClient": "yarn", 5 | "useWorkspaces": true 6 | } 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "license": "MIT", 4 | "engines": { 5 | "node": ">=4", 6 | "yarn": ">=1.0.1" 7 | }, 8 | "devDependencies": { 9 | "babel-cli": "^6.26.0", 10 | "babel-preset-jolt": "^2.8.0", 11 | "coveralls": "^2.13.1", 12 | "eslint": "^4.4.1", 13 | "eslint-config-jolt": "^2.6.1", 14 | "eslint-plugin-jest": "^21.0.2", 15 | "husky": "^0.14.3", 16 | "jest": "^21.0.0-beta.1", 17 | "lerna": "^2.0.0", 18 | "lint-staged": "^4.0.3", 19 | "raf": "^3.3.2", 20 | "react": "^16.3.0", 21 | "react-dom": "^16.3.0", 22 | "rimraf": "^2.6.1" 23 | }, 24 | "babel": { 25 | "presets": [ 26 | [ 27 | "jolt", 28 | { 29 | "target": "node", 30 | "react": true 31 | } 32 | ] 33 | ] 34 | }, 35 | "eslintConfig": { 36 | "extends": "jolt" 37 | }, 38 | "eslintIgnore": [ 39 | "build", 40 | "coverage", 41 | "node_modules" 42 | ], 43 | "jest": { 44 | "projects": [ 45 | "examples/*", 46 | "packages/*" 47 | ] 48 | }, 49 | "lint-staged": { 50 | "*.js": [ 51 | "yarn lint" 52 | ] 53 | }, 54 | "workspaces": [ 55 | "examples/*", 56 | "packages/*" 57 | ], 58 | "scripts": { 59 | "bootstrap": "lerna bootstrap", 60 | "build": "lerna run build", 61 | "clean": "lerna clean --yes && lerna run clean", 62 | "coveralls": "cat ./coverage/lcov.info | coveralls", 63 | "lint": "eslint .", 64 | "lint:cached": "yarn lint --cache", 65 | "precommit": "lint-staged", 66 | "test": "jest", 67 | "test:build": "yarn run build && yarn run test", 68 | "test:coverage": "yarn test --coverage" 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /packages/jest-plugin-action/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugin-action 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugin-action.svg)](https://www.npmjs.com/package/jest-plugin-action) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugin-action.svg)](https://www.npmjs.com/package/jest-plugin-action) 5 | [![npm](https://img.shields.io/npm/l/jest-plugin-action.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Declarative method for testing actions using jest. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugin-action` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugin-action 15 | ``` 16 | 17 | ## Motivation 18 | 19 | If you haven't already, check out [`jest-set`](https://github.com/negativetwelve/jest-set). `jest-plugin-action` builds on `jest-set` by providing the `action` keyword which allows you to define a function. These functions are lazily defined and are useful for passing directly into `expect` to test errors, warnings, etc, or for passing into `beforeEach` to easily set up a scenario before defining expectations. Here's an example: 20 | 21 | ```javascript 22 | describe('User', () => { 23 | describe('.update', () => { 24 | set('user', () => new User({firstName: 'Mary', lastName: 'Lamb'})); 25 | 26 | describe('with valid firstName and lastName', () => { 27 | set('firstName', () => 'Test'); 28 | set('lastName', () => 'User'); 29 | action('updateUser', () => user.update({firstName, lastName})); 30 | beforeEach(updateUser); 31 | 32 | it('should set firstName', () => { 33 | expect(user.firstName).toEqual('Test'); 34 | }); 35 | 36 | it('should compute name', () => { 37 | expect(user.name).toEqual('Test User'); 38 | }); 39 | }); 40 | 41 | describe('with invalid firstName', () => { 42 | set('firstName', () => null); 43 | set('lastName', () => null); 44 | action('updateUser', () => user.update({firstName, lastName})); 45 | 46 | it('should throw an error', () => { 47 | expect(updateUser).toThrow(ValidationError); 48 | }); 49 | }); 50 | }); 51 | }); 52 | ``` 53 | 54 | Even in this trivial example, it's easy to see the power of `action`. 55 | 56 | 1. We can declare actions in the same way we declare our variables. That keeps all of our changes in one place. 57 | 2. We can create easy-to-read expectations for actions: `expect(userUser).toThrow(ValidationError);`. It doesn't get any better than this. 58 | 3. Using lazy actions, we can declare them in one scope and access / override variables in another scope, just like `set`! 59 | 60 | ## Usage 61 | 62 | If you want, you can import `action` from `jest-plugin-action` at the top of every test: 63 | 64 | ```javascript 65 | import action from 'jest-plugin-action'; 66 | ``` 67 | 68 | If you want to install `action` as a global, you can modify the `jest` section of your `package.json` to include: 69 | 70 | ```json 71 | "jest": { 72 | "setupFiles": [ 73 | "jest-plugin-action/setup" 74 | ] 75 | } 76 | ``` 77 | 78 | ## Example 79 | 80 | Here's an example test that tests `action` itself: 81 | 82 | ```javascript 83 | describe('action', () => { 84 | set('a', () => 1); 85 | set('b', () => 2); 86 | action('add', () => a + b); 87 | action('multiply', () => a * b); 88 | action('divide', () => { 89 | if (b === 0) { 90 | throw new Error('Cannot divide by zero'); 91 | } else { 92 | return a / b; 93 | } 94 | }); 95 | 96 | describe('add', () => { 97 | it('should not throw an error', () => { 98 | expect(add).not.toThrow(); 99 | }); 100 | }); 101 | 102 | describe('multiply', () => { 103 | it('should not throw an error', () => { 104 | expect(multiply).not.toThrow(); 105 | }); 106 | }); 107 | 108 | describe('division', () => { 109 | context('with b < 0', () => { 110 | set('b', () => -42); 111 | 112 | it('should not throw an error', () => { 113 | expect(divide).not.toThrow(); 114 | }); 115 | }); 116 | 117 | context('with b = 0', () => { 118 | set('b', () => 0); 119 | 120 | it('should throw an error', () => { 121 | expect(divide).toThrow('Cannot divide by zero'); 122 | }); 123 | }); 124 | 125 | context('with b > 0', () => { 126 | set('b', () => 100); 127 | 128 | it('should not throw an error', () => { 129 | expect(divide).not.toThrow(); 130 | }); 131 | }); 132 | }); 133 | }); 134 | ``` 135 | -------------------------------------------------------------------------------- /packages/jest-plugin-action/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugin-action", 3 | "version": "2.9.0", 4 | "description": "Declarative method for testing actions in jest.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-action", 9 | "keywords": [ 10 | "action", 11 | "declarative", 12 | "jest", 13 | "jest-plugin", 14 | "plugin", 15 | "tdd", 16 | "test" 17 | ], 18 | "main": "build/action", 19 | "files": [ 20 | "build", 21 | "setup.js" 22 | ], 23 | "dependencies": { 24 | "jest-plugin-set": "^2.9.0" 25 | }, 26 | "peerDependencies": { 27 | "jest": "*" 28 | }, 29 | "devDependencies": { 30 | "jest-plugin-context": "^2.9.0" 31 | }, 32 | "scripts": { 33 | "build": "babel src --out-dir build --ignore '**/*.spec.js'", 34 | "clean": "rimraf build", 35 | "prepublishOnly": "yarn run clean && yarn run build" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /packages/jest-plugin-action/setup.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Installs 'action' and makes it available as a global function throughout all 3 | * test files. 4 | */ 5 | global.action = require('./').default; 6 | -------------------------------------------------------------------------------- /packages/jest-plugin-action/src/__tests__/action.spec.js: -------------------------------------------------------------------------------- 1 | // Libraries 2 | import context from 'jest-plugin-context'; 3 | import set from 'jest-plugin-set'; 4 | import action from '../action'; 5 | 6 | 7 | /* eslint-disable no-undef */ 8 | describe('action', () => { 9 | set('a', () => 1); 10 | set('b', () => 2); 11 | action('add', () => a + b); 12 | action('multiply', () => a * b); 13 | action('divide', () => { 14 | if (b === 0) { 15 | throw new Error('Cannot divide by zero'); 16 | } else { 17 | return a / b; 18 | } 19 | }); 20 | 21 | describe('add', () => { 22 | it('should not throw an error', () => { 23 | expect(add).not.toThrow(); 24 | }); 25 | }); 26 | 27 | describe('multiply', () => { 28 | it('should not throw an error', () => { 29 | expect(multiply).not.toThrow(); 30 | }); 31 | }); 32 | 33 | describe('division', () => { 34 | context('with b < 0', () => { 35 | set('b', () => -42); 36 | 37 | it('should not throw an error', () => { 38 | expect(divide).not.toThrow(); 39 | }); 40 | }); 41 | 42 | context('with b = 0', () => { 43 | set('b', () => 0); 44 | 45 | it('should throw an error', () => { 46 | expect(divide).toThrow('Cannot divide by zero'); 47 | }); 48 | }); 49 | 50 | context('with b > 0', () => { 51 | set('b', () => 100); 52 | 53 | it('should not throw an error', () => { 54 | expect(divide).not.toThrow(); 55 | }); 56 | }); 57 | }); 58 | }); 59 | -------------------------------------------------------------------------------- /packages/jest-plugin-action/src/action.js: -------------------------------------------------------------------------------- 1 | // Modules 2 | import set from 'jest-plugin-set'; 3 | 4 | 5 | /** 6 | * 'action' allows us to lazily define functions that we want to test. 7 | */ 8 | const action = (name, block) => set(name, () => block); 9 | 10 | 11 | export default action; 12 | -------------------------------------------------------------------------------- /packages/jest-plugin-clock/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugin-clock 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugin-clock.svg)](https://www.npmjs.com/package/jest-plugin-clock) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugin-clock.svg)](https://www.npmjs.com/package/jest-plugin-clock) 5 | [![npm](https://img.shields.io/npm/l/jest-plugin-clock.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Jest plugin to mock dates, times, and datetimes. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugin-clock` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugin-clock 15 | ``` 16 | 17 | ## Motivation 18 | 19 | When testing dates, you often want to have deterministic outcomes even when using `new Date()` and `Date.now()`. This allows you to set the clock time such that your test can compute the deterministic outcome each time. 20 | 21 | ## Usage 22 | 23 | If you want, you can import `clock` from `jest-plugin-clock` at the top of every test: 24 | 25 | ```javascript 26 | import clock from 'jest-plugin-clock'; 27 | ``` 28 | 29 | If you want to install `clock` as a global, you can modify the `jest` section of your `package.json` to include: 30 | 31 | ```json 32 | "jest": { 33 | "setupFiles": [ 34 | "jest-plugin-clock/setup" 35 | ] 36 | } 37 | ``` 38 | 39 | ## Example 40 | 41 | Here's an example test that tests `clock` itself: 42 | 43 | ```javascript 44 | describe('clock', () => { 45 | context('with string date', () => { 46 | clock.set('2017-08-16T15:20:40.450'); 47 | 48 | it('returns the correct year', () => { 49 | expect(new Date().getFullYear()).toEqual(2017); 50 | }); 51 | 52 | it('returns the correct month', () => { 53 | // getMonth is 0-indexed. 54 | expect(new Date().getMonth()).toEqual(7); 55 | }); 56 | 57 | it('returns the correct day (date)', () => { 58 | expect(new Date().getDate()).toEqual(16); 59 | }); 60 | 61 | it('returns the correct hour', () => { 62 | expect(new Date().getHours()).toEqual(15); 63 | }); 64 | 65 | it('returns the correct minute', () => { 66 | expect(new Date().getMinutes()).toEqual(20); 67 | }); 68 | 69 | it('returns the correct seconds', () => { 70 | expect(new Date().getSeconds()).toEqual(40); 71 | }); 72 | 73 | it('returns the correct milliseconds', () => { 74 | expect(new Date().getMilliseconds()).toEqual(450); 75 | }); 76 | }); 77 | 78 | context('with date object', () => { 79 | clock.set(new Date('2017-08-16T15:20:40.450')); 80 | 81 | it('returns the correct year', () => { 82 | expect(new Date().getFullYear()).toEqual(2017); 83 | }); 84 | 85 | it('returns the correct month', () => { 86 | // getMonth is 0-indexed. 87 | expect(new Date().getMonth()).toEqual(7); 88 | }); 89 | 90 | it('returns the correct day (date)', () => { 91 | expect(new Date().getDate()).toEqual(16); 92 | }); 93 | 94 | it('returns the correct hour', () => { 95 | expect(new Date().getHours()).toEqual(15); 96 | }); 97 | 98 | it('returns the correct minute', () => { 99 | expect(new Date().getMinutes()).toEqual(20); 100 | }); 101 | 102 | it('returns the correct seconds', () => { 103 | expect(new Date().getSeconds()).toEqual(40); 104 | }); 105 | 106 | it('returns the correct milliseconds', () => { 107 | expect(new Date().getMilliseconds()).toEqual(450); 108 | }); 109 | }); 110 | }); 111 | ``` 112 | -------------------------------------------------------------------------------- /packages/jest-plugin-clock/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugin-clock", 3 | "version": "2.9.0", 4 | "description": "Jest plugin to mock dates, times, and datetimes.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-clock", 9 | "keywords": [ 10 | "clock", 11 | "date", 12 | "datetime", 13 | "jest", 14 | "jest-plugin", 15 | "plugin", 16 | "time", 17 | "test" 18 | ], 19 | "main": "build/clock", 20 | "files": [ 21 | "build", 22 | "setup.js" 23 | ], 24 | "dependencies": { 25 | "mockdate": "^2.0.2" 26 | }, 27 | "peerDependencies": { 28 | "jest": "*" 29 | }, 30 | "devDependencies": { 31 | "jest-plugin-context": "^2.9.0" 32 | }, 33 | "scripts": { 34 | "build": "babel src --out-dir build --ignore '**/*.spec.js'", 35 | "clean": "rimraf build", 36 | "prepublishOnly": "yarn run clean && yarn run build" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /packages/jest-plugin-clock/setup.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Installs clock and makes it available as a global function throughout all 3 | * test files. 4 | */ 5 | global.clock = require('./').default; 6 | -------------------------------------------------------------------------------- /packages/jest-plugin-clock/src/__tests__/clock.spec.js: -------------------------------------------------------------------------------- 1 | // Modules 2 | import context from 'jest-plugin-context'; 3 | import clock from '../clock'; 4 | 5 | 6 | /* eslint-disable no-undef */ 7 | describe('clock', () => { 8 | context('with string date', () => { 9 | clock.set('2017-08-16T15:20:40.450'); 10 | 11 | it('returns the correct year', () => { 12 | expect(new Date().getFullYear()).toEqual(2017); 13 | }); 14 | 15 | it('returns the correct month', () => { 16 | // getMonth is 0-indexed. 17 | expect(new Date().getMonth()).toEqual(7); 18 | }); 19 | 20 | it('returns the correct day (date)', () => { 21 | expect(new Date().getDate()).toEqual(16); 22 | }); 23 | 24 | it('returns the correct hour', () => { 25 | expect(new Date().getHours()).toEqual(15); 26 | }); 27 | 28 | it('returns the correct minute', () => { 29 | expect(new Date().getMinutes()).toEqual(20); 30 | }); 31 | 32 | it('returns the correct seconds', () => { 33 | expect(new Date().getSeconds()).toEqual(40); 34 | }); 35 | 36 | it('returns the correct milliseconds', () => { 37 | expect(new Date().getMilliseconds()).toEqual(450); 38 | }); 39 | }); 40 | 41 | context('with date object', () => { 42 | clock.set(new Date('2017-08-16T15:20:40.450')); 43 | 44 | it('returns the correct year', () => { 45 | expect(new Date().getFullYear()).toEqual(2017); 46 | }); 47 | 48 | it('returns the correct month', () => { 49 | // getMonth is 0-indexed. 50 | expect(new Date().getMonth()).toEqual(7); 51 | }); 52 | 53 | it('returns the correct day (date)', () => { 54 | expect(new Date().getDate()).toEqual(16); 55 | }); 56 | 57 | it('returns the correct hour', () => { 58 | expect(new Date().getHours()).toEqual(15); 59 | }); 60 | 61 | it('returns the correct minute', () => { 62 | expect(new Date().getMinutes()).toEqual(20); 63 | }); 64 | 65 | it('returns the correct seconds', () => { 66 | expect(new Date().getSeconds()).toEqual(40); 67 | }); 68 | 69 | it('returns the correct milliseconds', () => { 70 | expect(new Date().getMilliseconds()).toEqual(450); 71 | }); 72 | }); 73 | }); 74 | -------------------------------------------------------------------------------- /packages/jest-plugin-clock/src/clock.js: -------------------------------------------------------------------------------- 1 | // Libraries 2 | import MockDate from 'mockdate'; 3 | 4 | 5 | const clock = { 6 | 7 | /** 8 | * Gives us the ability to modify time. Accepts any object / string that 9 | * can be passed to the Date constructor including another javascript 10 | * Date object. 11 | */ 12 | set: (date) => { 13 | beforeEach(() => { 14 | MockDate.set(new Date(date)); 15 | }); 16 | 17 | afterEach(() => { 18 | MockDate.reset(); 19 | }); 20 | }, 21 | 22 | }; 23 | 24 | 25 | export default clock; 26 | -------------------------------------------------------------------------------- /packages/jest-plugin-console-matchers/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugin-console-matchers 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugin-console-matchers.svg)](https://www.npmjs.com/package/jest-plugin-console-matchers) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugin-console-matchers.svg)](https://www.npmjs.com/package/jest-plugin-console-matchers) 5 | [![npm](https://img.shields.io/npm/l/jest-plugin-console-matchers.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Adds `jest` matchers for watching `console` methods. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugin-console-matchers` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugin-console-matchers 15 | ``` 16 | 17 | ## Matchers 18 | 19 | Adds the following matchers which check for `console` methods to be called: 20 | 21 | | Matcher | Method | 22 | |---------|--------| 23 | | `toConsoleError` | `console.error` | 24 | | `toConsoleInfo` | `console.info` | 25 | | `toConsoleLog` | `console.log` | 26 | | `toConsoleWarn` | `console.warn` | 27 | | `toThrowWarning` | `warning` | 28 | 29 | ## Usage 30 | 31 | If you want, you can import the matchers to install them in a set of tests. 32 | 33 | ```javascript 34 | import 'jest-plugin-console-matchers/setup'; 35 | ``` 36 | 37 | If you want to install the matchers globally, you can modify the jest section of your package.json to include: 38 | 39 | ```json 40 | "jest": { 41 | "setupFiles": [ 42 | "jest-plugin-console-matchers/setup" 43 | ] 44 | } 45 | ``` 46 | 47 | ## Example 48 | 49 | Example spec that uses these matchers. 50 | 51 | ```javascript 52 | import 'jest-plugin-console-matchers/setup'; 53 | 54 | describe('User', () => { 55 | set('user', () => new User()); 56 | 57 | describe('.sayHello', () => { 58 | it('logs hello', () => { 59 | expect(() => user.sayHello()).toConsoleLog(); 60 | }); 61 | }); 62 | 63 | describe('.delete', () => { 64 | it('warns about deleting', () => { 65 | expect(() => user.delete()).toConsoleWarn(); 66 | }); 67 | }); 68 | }); 69 | ``` 70 | 71 | ## Contributing 72 | 73 | If you have any ideas on how this plugin could be better, [create an Issue](https://github.com/negativetwelve/jest-plugins/issues) or [submit a PR](https://github.com/negativetwelve/jest-plugins/pulls). 74 | -------------------------------------------------------------------------------- /packages/jest-plugin-console-matchers/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugin-console-matchers", 3 | "version": "2.9.0", 4 | "description": "Adds jest matchers for watching console methods.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-console-matchers", 9 | "keywords": [ 10 | "console", 11 | "jest", 12 | "jest-plugin", 13 | "matchers", 14 | "plugin", 15 | "specs", 16 | "test" 17 | ], 18 | "main": "build/matchers", 19 | "files": [ 20 | "build", 21 | "setup.js" 22 | ], 23 | "peerDependencies": { 24 | "jest": "*" 25 | }, 26 | "devDependencies": { 27 | "jest-plugin-action": "^2.9.0", 28 | "jest-plugin-context": "^2.9.0" 29 | }, 30 | "scripts": { 31 | "build": "babel src --out-dir build --ignore '**/*.spec.js'", 32 | "clean": "rimraf build", 33 | "prepublishOnly": "yarn run clean && yarn run build" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /packages/jest-plugin-console-matchers/setup.js: -------------------------------------------------------------------------------- 1 | const { 2 | toConsoleError, 3 | toConsoleInfo, 4 | toConsoleLog, 5 | toConsoleWarn, 6 | toThrowWarning, 7 | } = require('./'); 8 | 9 | 10 | // Injects custom matchers into jest. 11 | expect.extend({ 12 | toConsoleError, 13 | toConsoleInfo, 14 | toConsoleLog, 15 | toConsoleWarn, 16 | toThrowWarning, 17 | }); 18 | -------------------------------------------------------------------------------- /packages/jest-plugin-console-matchers/src/__tests__/matchers.spec.js: -------------------------------------------------------------------------------- 1 | // Modules 2 | import action from 'jest-plugin-action'; 3 | import context from 'jest-plugin-context'; 4 | import '../../setup'; 5 | 6 | 7 | /* eslint-disable no-undef */ 8 | describe('console matchers', () => { 9 | action('noop', () => {}); 10 | 11 | describe('toConsoleError', () => { 12 | context('with console.error message', () => { 13 | action('error', () => console.error('error message')); 14 | 15 | it('should console.error', () => { 16 | expect(error).toConsoleError(); 17 | }); 18 | }); 19 | 20 | context('without console.error message', () => { 21 | it('should not console.error', () => { 22 | expect(noop).not.toConsoleError(); 23 | }); 24 | }); 25 | }); 26 | 27 | describe('toConsoleInfo', () => { 28 | context('with console.info message', () => { 29 | action('info', () => console.info('info message')); 30 | 31 | it('should console.info', () => { 32 | expect(info).toConsoleInfo(); 33 | }); 34 | }); 35 | 36 | context('without console.info message', () => { 37 | it('should not console.info', () => { 38 | expect(noop).not.toConsoleInfo(); 39 | }); 40 | }); 41 | }); 42 | 43 | describe('toConsoleLog', () => { 44 | context('with console.log message', () => { 45 | action('log', () => console.log('log message')); 46 | 47 | it('should console.log', () => { 48 | expect(log).toConsoleLog(); 49 | }); 50 | }); 51 | 52 | context('without console.log message', () => { 53 | it('should not console.log', () => { 54 | expect(noop).not.toConsoleLog(); 55 | }); 56 | }); 57 | }); 58 | 59 | describe('toConsoleWarn', () => { 60 | context('with console.warn message', () => { 61 | action('warn', () => console.warn('warn message')); 62 | 63 | it('should console.warn', () => { 64 | expect(warn).toConsoleWarn(); 65 | }); 66 | }); 67 | 68 | context('without console.warn message', () => { 69 | it('should not console.warn', () => { 70 | expect(noop).not.toConsoleWarn(); 71 | }); 72 | }); 73 | }); 74 | }); 75 | -------------------------------------------------------------------------------- /packages/jest-plugin-console-matchers/src/matchers.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Base of all console matchers. This is a higher order function that mocks 3 | * out the console method and counts the number of calls. 4 | */ 5 | export const createConsoleMatcher = (method) => (callback, message) => { 6 | const spy = jest.spyOn(global.console, method).mockImplementation(jest.fn); 7 | const value = callback(); 8 | const count = spy.mock.calls.length; 9 | const pass = count > 0; 10 | 11 | // Restore the mock to allow other console methods. 12 | spy.mockRestore(); 13 | 14 | if (count > 0) { 15 | return { 16 | pass: true, 17 | message: () => `expected 'console.${method}' to not be called`, 18 | }; 19 | } else { 20 | return { 21 | pass: false, 22 | message: () => `expected 'console.${method}' to be called at least once`, 23 | }; 24 | } 25 | }; 26 | 27 | export const toConsoleError = createConsoleMatcher('error'); 28 | export const toConsoleInfo = createConsoleMatcher('info'); 29 | export const toConsoleLog = createConsoleMatcher('log'); 30 | export const toConsoleWarn = createConsoleMatcher('warn'); 31 | 32 | // Special case for Facebook's warning module. 33 | export const toThrowWarning = createConsoleMatcher('error'); 34 | -------------------------------------------------------------------------------- /packages/jest-plugin-context/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugin-context 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugin-context.svg)](https://www.npmjs.com/package/jest-plugin-context) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugin-context.svg)](https://www.npmjs.com/package/jest-plugin-context) 5 | [![npm](https://img.shields.io/npm/l/jest-plugin-context.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Adds `context` as an alternative to `describe` for jest. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugin-context` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugin-context 15 | ``` 16 | 17 | ## Motivation 18 | 19 | [RSpec](http://rspec.info/) took the ruby world by storm with its declarative method of TDD. In RSpec, `describe` it used to wrap a set of tests against one functionality while `context` is to wrap a set of tests against one functionality under the same state. 20 | 21 | The difference being you should only `describe` to test the User model and specifically `describe` the `#name` method. However, testing different states of the `#name` method should use different context. You can view an example of this below. 22 | 23 | ## Usage 24 | 25 | If you want, you can import `context` from `jest-plugin-context` at the top of every test: 26 | 27 | ```javascript 28 | import context from 'jest-plugin-context'; 29 | ``` 30 | 31 | If you want to install `context` as a global, you can modify the `jest` section of your `package.json` to include: 32 | 33 | ```json 34 | "jest": { 35 | "setupFiles": [ 36 | "jest-plugin-context/setup" 37 | ] 38 | } 39 | ``` 40 | 41 | ## Example 42 | 43 | Here's an example test that uses `context`: 44 | 45 | ```javascript 46 | describe('User', () => { 47 | 48 | describe('#name', () => { 49 | set('firstName', () => 'Harry'); 50 | set('lastName', () => 'Potter'); 51 | set('user', () => new User({firstName, lastName})); 52 | 53 | context('with blank first name', () => { 54 | set('firstName', () => null); 55 | 56 | it('should return only the last name', () => { 57 | expect(user.name).toEqual('Potter'); 58 | }); 59 | }); 60 | 61 | context('with blank last name', () => { 62 | set('lastName', () => null); 63 | 64 | it('should return only the first name', () => { 65 | expect(user.name).toEqual('Harry'); 66 | }); 67 | }); 68 | }); 69 | }); 70 | ``` 71 | 72 | ## Types for Typescript 73 | 74 | Types are available for this package. Install them using yarn: 75 | ``` 76 | yarn add --dev @types/jest-plugin-context 77 | ``` 78 | -------------------------------------------------------------------------------- /packages/jest-plugin-context/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugin-context", 3 | "version": "2.9.0", 4 | "description": "Adds context as an alternative to describe to jest.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-context", 9 | "keywords": [ 10 | "context", 11 | "jest", 12 | "jest-plugin", 13 | "plugin", 14 | "test" 15 | ], 16 | "main": "build/context", 17 | "files": [ 18 | "build", 19 | "setup.js" 20 | ], 21 | "peerDependencies": { 22 | "jest": "*" 23 | }, 24 | "scripts": { 25 | "build": "babel src --out-dir build --ignore '**/*.spec.js'", 26 | "clean": "rimraf build", 27 | "prepublishOnly": "yarn run clean && yarn run build" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /packages/jest-plugin-context/setup.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Sets `context` as a valid global in jest. Because `describe` is not defined 3 | * at this point in the setup process, we define a lazy getter to access it. 4 | * 5 | * NOTE(mark): When jest-plugins works properly, this hack won't be required. 6 | */ 7 | Object.defineProperty(global, 'context', { 8 | get: function() { 9 | return require('./').default; 10 | }, 11 | }); 12 | -------------------------------------------------------------------------------- /packages/jest-plugin-context/src/__tests__/context.spec.js: -------------------------------------------------------------------------------- 1 | // Modules 2 | import context from '../context'; 3 | 4 | 5 | describe('context', () => { 6 | context('with context', () => { 7 | it('should run tests normally', () => { 8 | expect(1 + 1).toEqual(2); 9 | }); 10 | }); 11 | }); 12 | -------------------------------------------------------------------------------- /packages/jest-plugin-context/src/context.js: -------------------------------------------------------------------------------- 1 | /** 2 | * In jest, `describe` is a global that does exactly what we want `context` to 3 | * do. We use `describe` for objects and methods and `context` when we want to 4 | * add additional information to the test cases. 5 | */ 6 | export default global.describe; 7 | -------------------------------------------------------------------------------- /packages/jest-plugin-enzyme/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugin-enzyme 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugin-enzyme.svg)](https://www.npmjs.com/package/jest-plugin-enzyme) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugin-enzyme.svg)](https://www.npmjs.com/package/jest-plugin-enzyme) 5 | [![npm](https://img.shields.io/npm/l/jest-plugin-enzyme.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Jest plugin to quickly add enzyme to tests. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugin-enzyme` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugin-enzyme 15 | ``` 16 | 17 | ## Usage 18 | 19 | This package isn't used directly within tests. It's used to construct other `jest-plugins` like `jest-plugin-it-shallow-renders`. 20 | 21 | If you want to configure enzyme for react automatically, you can modify the `jest` section of your `package.json` to include: 22 | 23 | ```json 24 | "jest": { 25 | "setupFiles": [ 26 | "jest-plugin-enzyme/setup" 27 | ] 28 | } 29 | ``` 30 | -------------------------------------------------------------------------------- /packages/jest-plugin-enzyme/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugin-enzyme", 3 | "version": "2.9.0", 4 | "description": "Jest plugin to quickly add enzyme to your react tests.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-enzyme", 9 | "keywords": [ 10 | "enzyme", 11 | "jest", 12 | "jest-plugin", 13 | "plugin", 14 | "react", 15 | "test" 16 | ], 17 | "main": "build/enzyme", 18 | "files": [ 19 | "build", 20 | "setup.js" 21 | ], 22 | "dependencies": { 23 | "enzyme": "^3.3.0", 24 | "enzyme-adapter-react-16": "^1.1.1", 25 | "enzyme-to-json": "^3.3.3" 26 | }, 27 | "peerDependencies": { 28 | "jest": "*", 29 | "react": "*", 30 | "react-dom": "*" 31 | }, 32 | "scripts": { 33 | "build": "babel src --out-dir build --ignore \"**/__tests__/**\"", 34 | "clean": "rimraf build", 35 | "prepublishOnly": "yarn run clean && yarn run build" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /packages/jest-plugin-enzyme/setup.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Configures enzyme with support for react. 3 | */ 4 | require('./'); 5 | -------------------------------------------------------------------------------- /packages/jest-plugin-enzyme/src/enzyme.js: -------------------------------------------------------------------------------- 1 | // Libraries 2 | import Enzyme, {shallow} from 'enzyme'; 3 | import Adapter from 'enzyme-adapter-react-16'; 4 | import toJSON from 'enzyme-to-json'; 5 | 6 | Enzyme.configure({adapter: new Adapter()}); 7 | 8 | export {shallow, toJSON}; 9 | export default Enzyme; 10 | -------------------------------------------------------------------------------- /packages/jest-plugin-for-each/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugin-for-each 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugin-for-each.svg)](https://www.npmjs.com/package/jest-plugin-for-each) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugin-for-each.svg)](https://www.npmjs.com/package/jest-plugin-for-each) 5 | [![npm](https://img.shields.io/npm/l/jest-plugin-for-each.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Jest plugin to test multiple values for a single outcome. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugin-for-each` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugin-for-each 15 | ``` 16 | 17 | ## Motivation 18 | 19 | Building off of `jest-plugin-set`, we can use `jest-plugin-for-each` to define a set of tests that all expect the same outcome given a different set of fixed values. Here's an example: 20 | 21 | ```javascript 22 | // TODO 23 | ``` 24 | 25 | ## Usage 26 | 27 | If you want, you can import `forEach` from `jest-plugin-for-each` at the top of every test: 28 | 29 | ```javascript 30 | import forEach from 'jest-plugin-for-each'; 31 | ``` 32 | 33 | If you want to install `forEach` as a global, you can modify the `jest` section of your `package.json` to include: 34 | 35 | ```json 36 | "jest": { 37 | "setupFiles": [ 38 | "jest-plugin-for-each/setup" 39 | ] 40 | } 41 | ``` 42 | 43 | ## Example 44 | 45 | Here's an example test that tests `forEach` itself: 46 | 47 | ```javascript 48 | // TODO 49 | ``` 50 | -------------------------------------------------------------------------------- /packages/jest-plugin-for-each/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugin-for-each", 3 | "version": "2.9.0", 4 | "description": "Jest plugin to test multiple values for a single outcome.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-for-each", 9 | "keywords": [ 10 | "declarative", 11 | "jest", 12 | "jest-plugin", 13 | "let", 14 | "plugin", 15 | "set", 16 | "tdd", 17 | "test" 18 | ], 19 | "main": "build/forEach", 20 | "files": [ 21 | "build", 22 | "setup.js" 23 | ], 24 | "dependencies": { 25 | "invariant": "^2.2.2", 26 | "jest-plugin-set": "^2.9.0" 27 | }, 28 | "peerDependencies": { 29 | "jest": "*" 30 | }, 31 | "scripts": { 32 | "build": "babel src --out-dir build --ignore '**/*.spec.js'", 33 | "clean": "rimraf build", 34 | "prepublishOnly": "yarn run clean && yarn run build" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /packages/jest-plugin-for-each/setup.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Installs forEach and makes it available as a global function throughout all 3 | * test files. 4 | */ 5 | global.forEach = require('./').default; 6 | -------------------------------------------------------------------------------- /packages/jest-plugin-for-each/src/__tests__/forEach.spec.js: -------------------------------------------------------------------------------- 1 | // Modules 2 | import forEach from '../forEach'; 3 | 4 | 5 | /* eslint-disable no-undef */ 6 | describe('forEach', () => { 7 | forEach({value: [1, 2, 3]}, () => { 8 | it('should be > 0', () => { 9 | expect(value).toBeGreaterThan(0); 10 | }); 11 | }); 12 | 13 | forEach({value: [-1, -2, -3]}, () => { 14 | it('should be < 0', () => { 15 | expect(value).toBeLessThan(0); 16 | }); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /packages/jest-plugin-for-each/src/forEach.js: -------------------------------------------------------------------------------- 1 | // Libraries 2 | import invariant from 'invariant'; 3 | import set from 'jest-plugin-set'; 4 | 5 | 6 | /** 7 | * Allows us to test multiple values for the same variable with the same 8 | * desired outcome. Example, say there were multiple values that all caused 9 | * an error, it would look like: 10 | * 11 | * forEach({numTires: [1, 2, 3]}, () => { 12 | * it('raises an error when numTires < 4', () => { 13 | * expect(drive(numTires)).toThrow(); 14 | * }); 15 | * }); 16 | * 17 | */ 18 | const forEach = (object, block) => { 19 | const pairs = Object.entries(object); 20 | 21 | invariant( 22 | pairs.length === 1, 23 | `forEach accepts exactly one key / value pair for evaluating. ` + 24 | `Passing multiple values is currently not supported.`, 25 | ); 26 | 27 | const [key, values] = pairs[0]; 28 | 29 | values.map(value => { 30 | describe(`with ${key} set to ${value}`, () => { 31 | set(key, () => value); 32 | block(); 33 | }); 34 | }); 35 | }; 36 | 37 | 38 | export default forEach; 39 | -------------------------------------------------------------------------------- /packages/jest-plugin-fs/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugin-fs 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugin-fs.svg)](https://www.npmjs.com/package/jest-plugin-fs) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugin-fs.svg)](https://www.npmjs.com/package/jest-plugin-fs) 5 | [![npm](https://img.shields.io/npm/l/jest-plugin-fs.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Jest plugin for mocking out the filesystem. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugin-fs` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugin-fs 15 | ``` 16 | 17 | ## Motivation 18 | 19 | Jest currently does not have an easy way to mock out the filesystem. This plugin aims to change that. Here's an example: 20 | 21 | ```javascript 22 | import fs from 'jest-plugin-fs'; 23 | 24 | // Mock out the filesystem. 25 | jest.mock('fs', () => require('jest-plugin-fs/mock')); 26 | 27 | describe('FileWriter', () => { 28 | // Create an in-memory filesystem. 29 | beforeEach(() => fs.mock()); 30 | afterEach(() => fs.restore()); 31 | 32 | describe('.write', () => { 33 | set('filename', () => 'path/to/my/file'); 34 | action('write', () => FileWriter.write(filename)); 35 | 36 | it('should write a new file', () => { 37 | expect(write).not.toThrow(); 38 | }); 39 | 40 | describe('resulting file', () => { 41 | beforeEach(() => write()); 42 | 43 | it('should create the new file', () => { 44 | expect(fs.readFileSync(filename)).toEqual('new-file'); 45 | }); 46 | }); 47 | }); 48 | }); 49 | ``` 50 | 51 | ## Usage 52 | 53 | If you want, you can import `fs` from `jest-plugin-fs` at the top of every test: 54 | 55 | ```javascript 56 | import fs from 'jest-plugin-fs'; 57 | 58 | // This installs the mock for 'fs'. 59 | jest.mock('fs', () => require('jest-plugin-fs/mock')); 60 | ``` 61 | 62 | If you want to install `fs` attached to the global `jest` object, you can modify the `jest` section of your `package.json` to include: 63 | 64 | ```json 65 | "jest": { 66 | "setupFiles": [ 67 | "jest-plugin-fs/setup" 68 | ] 69 | } 70 | ``` 71 | 72 | ## Example 73 | 74 | Here's an example test that tests `fs` itself: 75 | 76 | ```javascript 77 | // TODO 78 | ``` 79 | -------------------------------------------------------------------------------- /packages/jest-plugin-fs/mock.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./').mock; 2 | -------------------------------------------------------------------------------- /packages/jest-plugin-fs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugin-fs", 3 | "version": "2.9.0", 4 | "description": "Jest plugin for mocking out the filesystem.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-fs", 9 | "keywords": [ 10 | "filesystem", 11 | "fs", 12 | "jest", 13 | "jest-plugin", 14 | "mock", 15 | "plugin", 16 | "test" 17 | ], 18 | "main": "build/fs", 19 | "files": [ 20 | "build", 21 | "mock.js", 22 | "setup.js" 23 | ], 24 | "dependencies": { 25 | "memfs": "^2.5.5" 26 | }, 27 | "peerDependencies": { 28 | "jest": "*" 29 | }, 30 | "devDependencies": { 31 | "fs-extra": "^4.0.1", 32 | "jest-plugin-set": "^2.9.0" 33 | }, 34 | "scripts": { 35 | "build": "babel src --out-dir build --ignore \"**/__tests__/**\"", 36 | "clean": "rimraf build", 37 | "prepublishOnly": "yarn run clean && yarn run build" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /packages/jest-plugin-fs/setup.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Installs fs and makes it available globally throughout all test files. 3 | */ 4 | global.fs = require('./').default; 5 | -------------------------------------------------------------------------------- /packages/jest-plugin-fs/src/__fixtures__/test.txt: -------------------------------------------------------------------------------- 1 | this is a test 2 | -------------------------------------------------------------------------------- /packages/jest-plugin-fs/src/__tests__/fs.spec.js: -------------------------------------------------------------------------------- 1 | // Modules 2 | import context from 'jest-plugin-context'; 3 | import set from 'jest-plugin-set'; 4 | import fsExtra from 'fs-extra'; 5 | import path from 'path'; 6 | import fs from '../fs'; 7 | 8 | // Path to the fixtures directory. 9 | const FIXTURES = path.join(__dirname, '..', '__fixtures__'); 10 | 11 | // Require the mock for the `fs` module. 12 | jest.mock('fs', () => require('jest-plugin-fs/mock')); 13 | 14 | /* eslint-disable no-undef */ 15 | describe('fs', () => { 16 | beforeEach(() => fs.mock()); 17 | afterEach(() => fs.restore()); 18 | 19 | context('with new file', () => { 20 | set('filename', () => 'some/path/to/file.txt'); 21 | beforeEach(() => fsExtra.outputFileSync(filename, 'test-content')); 22 | 23 | it('should create the new file', () => { 24 | expect(fsExtra.readFileSync(filename, 'utf8')).toEqual('test-content'); 25 | }); 26 | }); 27 | 28 | context('with nested directories', () => { 29 | set('filesystem', () => ({ 30 | test: { 31 | directory: { 32 | nested: { 33 | file: 'hello there', 34 | }, 35 | }, 36 | }, 37 | })); 38 | 39 | beforeEach(() => fs.mock(filesystem)); 40 | 41 | it('should create nested directories', () => { 42 | expect( 43 | fsExtra.readFileSync('/test/directory/nested/file', 'utf8'), 44 | ).toEqual('hello there'); 45 | }); 46 | }); 47 | 48 | describe('#files', () => { 49 | context('with empty filesystem', () => { 50 | beforeEach(() => fs.mock()); 51 | 52 | it('should return an empty object', () => { 53 | expect(fs.files()).toEqual({}); 54 | }); 55 | }); 56 | 57 | context('with files', () => { 58 | beforeEach(() => fs.mock({test: 'content', hello: 'goodbye'})); 59 | 60 | it('should return the files', () => { 61 | expect(fs.files()).toEqual({'/test': 'content', '/hello': 'goodbye'}); 62 | }); 63 | }); 64 | 65 | context('with nested files', () => { 66 | beforeEach(() => fs.mock({ 67 | test: { 68 | nested: { 69 | file: 'hi', 70 | }, 71 | }, 72 | })); 73 | 74 | it('should return the full path', () => { 75 | expect(fs.files()).toEqual({'/test/nested/file': 'hi'}); 76 | }); 77 | }); 78 | }); 79 | 80 | describe('#unmock', () => { 81 | context('with unmocked file', () => { 82 | set('filename', () => path.join(FIXTURES, 'test.txt')); 83 | 84 | beforeEach(() => fs.unmock([filename])); 85 | 86 | it('should exist in the virtual filesystem', () => { 87 | expect(fsExtra.readFileSync(filename, 'utf8')).toEqual('this is a test\n'); 88 | }); 89 | }); 90 | }); 91 | }); 92 | -------------------------------------------------------------------------------- /packages/jest-plugin-fs/src/fs.js: -------------------------------------------------------------------------------- 1 | // Libraries 2 | import os from 'os'; 3 | import path from 'path'; 4 | import {fs as mockFs, vol} from 'memfs'; 5 | 6 | 7 | /** 8 | * Root of the filesystem. 9 | */ 10 | const isWindows = os.platform() === 'win32'; 11 | const root = isWindows ? process.cwd().split(path.sep)[0] : '/'; 12 | 13 | /** 14 | * We allow passing in a nested object of paths. This function combines the 15 | * paths into a single flattened object of absolute path -> content. 16 | */ 17 | const flatten = (absolutePath, object) => { 18 | const accumulate = (all, [currentPath, value]) => { 19 | const joinedPath = path.join(absolutePath, currentPath); 20 | const fullPath = path.isAbsolute(currentPath) ? currentPath : joinedPath; 21 | 22 | if (typeof value === 'string') { 23 | return {...all, [fullPath]: value}; 24 | } else { 25 | return {...all, ...flatten(fullPath, value)}; 26 | } 27 | }; 28 | 29 | return Object.entries(object).reduce(accumulate, {}); 30 | }; 31 | 32 | /** 33 | * Escape hatch that uses real `fs` to read files from the filesystem. 34 | * Use this to load fixture data from real files. 35 | */ 36 | const read = (filename) => { 37 | return require.requireActual('fs').readFileSync(filename, 'utf8'); 38 | }; 39 | 40 | /** 41 | * Returns a JS object with the mocked filesystem contents. 42 | */ 43 | const files = () => vol.toJSON(); 44 | 45 | /** 46 | * When we mock the filesystem, we traverse the object to get the 47 | * full paths to the file. 48 | */ 49 | const mock = (filesystem = {}, fsRoot = root) => { 50 | vol.fromJSON(flatten(fsRoot, filesystem), fsRoot); 51 | }; 52 | 53 | /** 54 | * Reads the passed in files from the filesystem and adds them to the 55 | * virtual mocked filesystem 56 | */ 57 | const unmock = (files = [], fsRoot = root) => { 58 | const readAll = (all, file) => ({...all, [file]: read(file)}); 59 | const filesystem = files.reduce(readAll, {}); 60 | 61 | mock(filesystem, fsRoot); 62 | }; 63 | 64 | /** 65 | * Resets the mocked volume and restores the fs module. 66 | */ 67 | const restore = () => vol.reset(); 68 | 69 | /** 70 | * Mocked filesystem module that contains the helper functions to create and 71 | * reset filesystems. 72 | */ 73 | const jestFs = { 74 | root, 75 | files, 76 | read, 77 | restore, 78 | 79 | // NOTE(mark): This allows the root to be configurable by the fs object. 80 | mock: (filesystem) => mock(filesystem, jestFs.root), 81 | unmock: (files) => unmock(files, jestFs.root), 82 | }; 83 | 84 | 85 | export {mockFs as mock}; 86 | export default jestFs; 87 | -------------------------------------------------------------------------------- /packages/jest-plugin-it-renders/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugin-it-renders 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugin-it-renders.svg)](https://www.npmjs.com/package/jest-plugin-it-renders) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugin-it-renders.svg)](https://www.npmjs.com/package/jest-plugin-it-renders) 5 | [![npm](https://img.shields.io/npm/l/jest-plugin-it-renders.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Jest plugin to quickly test if a React component renders properly. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugin-it-renders` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugin-it-renders 15 | ``` 16 | 17 | *NOTE:* If you're using a version of React before 16.0.0 (basically anything in the ^15.0.0 range), you'll have to use `jest-plugin-it-renders` version `1.8.0`. 18 | 19 | ## Motivation 20 | 21 | For almost any React component, you want to test the following rules: 22 | 23 | 1. It renders without erroring. 24 | 2. Changes to a component are expected (snapshot testing). 25 | 26 | This plugin allows you to do both just by rendering the component. Here's an example: 27 | 28 | ```javascript 29 | import UserAvatar from '../UserAvatar'; 30 | 31 | describe('UserAvatar', () => { 32 | context('with default props', () => { 33 | itRenders(() => ); 34 | }); 35 | 36 | context('with user object', () => { 37 | set('user', () => new User()); 38 | itRenders(() => ); 39 | }); 40 | }); 41 | ``` 42 | 43 | ## Usage 44 | 45 | If you want, you can import `itRenders` from `jest-plugin-it-renders` at the top of every test: 46 | 47 | ```javascript 48 | import itRenders from 'jest-plugin-it-renders'; 49 | ``` 50 | 51 | If you want to install `itRenders` as a global, you can modify the `jest` section of your `package.json` to include: 52 | 53 | ```json 54 | "jest": { 55 | "setupFiles": [ 56 | "jest-plugin-it-renders/setup" 57 | ] 58 | } 59 | ``` 60 | -------------------------------------------------------------------------------- /packages/jest-plugin-it-renders/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugin-it-renders", 3 | "version": "2.9.0", 4 | "description": "Jest plugin to quickly test if a React component renders properly.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-it-renders", 9 | "keywords": [ 10 | "component", 11 | "jest", 12 | "jest-plugin", 13 | "plugin", 14 | "react", 15 | "render", 16 | "snapshot", 17 | "test" 18 | ], 19 | "main": "build/itRenders", 20 | "files": [ 21 | "build", 22 | "setup.js" 23 | ], 24 | "dependencies": { 25 | "react-test-renderer": "^16.0.0" 26 | }, 27 | "peerDependencies": { 28 | "jest": "*", 29 | "react": "*" 30 | }, 31 | "scripts": { 32 | "build": "babel src --out-dir build --ignore \"**/__tests__/**\"", 33 | "clean": "rimraf build", 34 | "prepublishOnly": "yarn run clean && yarn run build" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /packages/jest-plugin-it-renders/setup.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Installs itRenders and makes it available as a global function throughout all 3 | * test files. 4 | */ 5 | global.itRenders = require('./').default; 6 | -------------------------------------------------------------------------------- /packages/jest-plugin-it-renders/src/__tests__/__snapshots__/itRenders.spec.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`itRenders Text renders snapshot 1`] = `
`; 4 | 5 | exports[`itRenders View renders snapshot 1`] = `
`; 6 | -------------------------------------------------------------------------------- /packages/jest-plugin-it-renders/src/__tests__/itRenders.spec.js: -------------------------------------------------------------------------------- 1 | // Libraries 2 | import React from 'react'; 3 | 4 | // Modules 5 | import itRenders from '../itRenders'; 6 | 7 | 8 | // Test Components 9 | const View = () =>
; 10 | const Text = () =>
; 11 | 12 | 13 | /* eslint-disable no-undef */ 14 | describe('itRenders', () => { 15 | describe('View', () => { 16 | itRenders(() => ); 17 | }); 18 | 19 | describe('Text', () => { 20 | itRenders(() => ); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /packages/jest-plugin-it-renders/src/itRenders.js: -------------------------------------------------------------------------------- 1 | // Libraries 2 | import Renderer from 'react-test-renderer'; 3 | 4 | 5 | const itRenders = (render) => { 6 | it('renders without error', () => { 7 | expect(render).not.toThrow(); 8 | }); 9 | 10 | it('renders snapshot', () => { 11 | const rendered = render(); 12 | const element = Renderer.create(rendered).toJSON(); 13 | 14 | expect(element).toMatchSnapshot(); 15 | }); 16 | }; 17 | 18 | 19 | export default itRenders; 20 | -------------------------------------------------------------------------------- /packages/jest-plugin-it-shallow-renders/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugin-it-shallow-renders 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugin-it-shallow-renders.svg)](https://www.npmjs.com/package/jest-plugin-it-shallow-renders) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugin-it-shallow-renders.svg)](https://www.npmjs.com/package/jest-plugin-it-shallow-renders) 5 | [![npm](https://img.shields.io/npm/l/jest-plugin-it-shallow-renders.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Jest plugin to quickly test if a React component shallow renders properly. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugin-it-shallow-renders` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugin-it-shallow-renders 15 | ``` 16 | 17 | *NOTE:* This plugin is only compatible with React version >= 16.0.0. 18 | 19 | ## Motivation 20 | 21 | For almost any React component, you want to test the following rules: 22 | 23 | 1. It renders without erroring. 24 | 2. Changes to a component are expected (snapshot testing). 25 | 26 | This plugin allows you to do both just by shallow rendering the component. Here's an example: 27 | 28 | ```javascript 29 | import UserAvatar from '../UserAvatar'; 30 | 31 | describe('UserAvatar', () => { 32 | context('with default props', () => { 33 | itShallowRenders(() => ); 34 | }); 35 | 36 | context('with user object', () => { 37 | set('user', () => new User()); 38 | itShallowRenders(() => ); 39 | }); 40 | }); 41 | ``` 42 | 43 | ## Usage 44 | 45 | If you want, you can import `itShallowRenders` from `jest-plugin-it-shallow-renders` at the top of every test: 46 | 47 | ```javascript 48 | import itShallowRenders from 'jest-plugin-it-shallow-renders'; 49 | ``` 50 | 51 | If you want to install `itShallowRenders` as a global, you can modify the `jest` section of your `package.json` to include: 52 | 53 | ```json 54 | "jest": { 55 | "setupFiles": [ 56 | "jest-plugin-it-shallow-renders/setup" 57 | ] 58 | } 59 | ``` 60 | -------------------------------------------------------------------------------- /packages/jest-plugin-it-shallow-renders/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugin-it-shallow-renders", 3 | "version": "2.9.0", 4 | "description": "Jest plugin to quickly test if a React component shallow renders properly.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-it-shallow-renders", 9 | "keywords": [ 10 | "component", 11 | "jest", 12 | "jest-plugin", 13 | "plugin", 14 | "react", 15 | "render", 16 | "shallow", 17 | "snapshot", 18 | "test" 19 | ], 20 | "main": "build/itShallowRenders", 21 | "files": [ 22 | "build", 23 | "setup.js" 24 | ], 25 | "dependencies": { 26 | "jest-plugin-enzyme": "^2.9.0" 27 | }, 28 | "peerDependencies": { 29 | "jest": "*" 30 | }, 31 | "scripts": { 32 | "build": "babel src --out-dir build --ignore \"**/__tests__/**\"", 33 | "clean": "rimraf build", 34 | "prepublishOnly": "yarn run clean && yarn run build" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /packages/jest-plugin-it-shallow-renders/setup.js: -------------------------------------------------------------------------------- 1 | // Set up Enzyme for React. 2 | require('jest-plugin-enzyme/setup'); 3 | 4 | /** 5 | * Installs itShallowRenders and makes it available as a global function 6 | * throughout all test files. 7 | */ 8 | global.itShallowRenders = require('./').default; 9 | -------------------------------------------------------------------------------- /packages/jest-plugin-it-shallow-renders/src/__tests__/__snapshots__/itShallowRenders.spec.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`itShallowRenders Text shallow renders snapshot 1`] = ` 4 | 5 |
6 | 7 | `; 8 | 9 | exports[`itShallowRenders View shallow renders snapshot 1`] = ` 10 |
11 |
12 |
13 | `; 14 | -------------------------------------------------------------------------------- /packages/jest-plugin-it-shallow-renders/src/__tests__/itShallowRenders.spec.js: -------------------------------------------------------------------------------- 1 | // Libraries 2 | import 'raf/polyfill'; 3 | import React from 'react'; 4 | 5 | // Modules 6 | import itShallowRenders from '../itShallowRenders'; 7 | 8 | 9 | // Test Components 10 | const View = () =>
; 11 | const Text = () =>
; 12 | 13 | 14 | /* eslint-disable no-undef */ 15 | describe('itShallowRenders', () => { 16 | describe('View', () => { 17 | itShallowRenders(() => ); 18 | }); 19 | 20 | describe('Text', () => { 21 | itShallowRenders(() => ); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /packages/jest-plugin-it-shallow-renders/src/itShallowRenders.js: -------------------------------------------------------------------------------- 1 | // Libraries 2 | import {shallow, toJSON} from 'jest-plugin-enzyme'; 3 | 4 | const itShallowRenders = (render) => { 5 | it('shallow renders without error', () => { 6 | expect(render).not.toThrow(); 7 | }); 8 | 9 | it('shallow renders snapshot', () => { 10 | const rendered = render(); 11 | const element = toJSON(shallow(rendered)); 12 | 13 | expect(element).toMatchSnapshot(); 14 | }); 15 | }; 16 | 17 | 18 | export default itShallowRenders; 19 | -------------------------------------------------------------------------------- /packages/jest-plugin-its/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugin-its 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugin-its.svg)](https://www.npmjs.com/package/jest-plugin-its) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugin-its.svg)](https://www.npmjs.com/package/jest-plugin-its) 5 | [![npm](https://img.shields.io/npm/l/jest-plugin-its.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Adds `subject` and `its` implementations from RSpec to Jest. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugin-its` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugin-its 15 | ``` 16 | 17 | ## Motivation 18 | 19 | ## Usage 20 | 21 | If you want, you can import `its` and `subject` for each test file via: 22 | 23 | ```javascript 24 | import {its, subject} from 'jest-plugin-its'; 25 | ``` 26 | 27 | If you want to install `its` and `subject` as globals, you can modify the `jest` section of your `package.json` to include: 28 | 29 | ```json 30 | "jest": { 31 | "setupFiles": [ 32 | "jest-plugin-its/setup" 33 | ] 34 | }, 35 | ``` 36 | 37 | ## Example 38 | 39 | Here's an example that tests the implementation of `its`: 40 | 41 | ```javascript 42 | import {its, subject} from '../'; 43 | 44 | describe('its', () => { 45 | describe('with primitives', () => { 46 | subject(() => ({a: 1, b: 2})); 47 | its('a', () => isExpected.toEqual(1)); 48 | its('b', () => isExpected.toEqual(2)); 49 | }); 50 | 51 | describe('with functions', () => { 52 | subject(() => ({a: () => 1, b: () => 2})); 53 | its('a', () => isExpected.toEqual(1)); 54 | its('b', () => isExpected.toEqual(2)); 55 | }); 56 | 57 | describe('with nested properties', () => { 58 | subject(() => ({a: {b: {c: 1, d: 2}}})); 59 | its('a.b.c', () => isExpected.toEqual(1)); 60 | its('a.b.d', () => isExpected.toEqual(2)); 61 | }); 62 | }); 63 | ``` 64 | -------------------------------------------------------------------------------- /packages/jest-plugin-its/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugin-its", 3 | "version": "2.9.0", 4 | "description": "'subject' and 'its' implementations from RSpec in Jest.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-its", 9 | "keywords": [ 10 | "its", 11 | "jest", 12 | "jest-plugin", 13 | "plugin", 14 | "subject", 15 | "test" 16 | ], 17 | "main": "build/index", 18 | "files": [ 19 | "build", 20 | "setup.js" 21 | ], 22 | "dependencies": { 23 | "invariant": "^2.2.2", 24 | "jest-plugin-set": "^2.9.0" 25 | }, 26 | "peerDependencies": { 27 | "jest": "*" 28 | }, 29 | "scripts": { 30 | "build": "babel src --out-dir build --ignore '**/*.spec.js'", 31 | "clean": "rimraf build", 32 | "prepublishOnly": "yarn run clean && yarn run build" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /packages/jest-plugin-its/setup.js: -------------------------------------------------------------------------------- 1 | // Globals 2 | global.its = require('./').its; 3 | global.subject = require('./').subject; 4 | -------------------------------------------------------------------------------- /packages/jest-plugin-its/src/__tests__/its.spec.js: -------------------------------------------------------------------------------- 1 | // Modules 2 | import its from '../its'; 3 | import subject from '../subject'; 4 | 5 | 6 | /* eslint-disable no-undef */ 7 | describe('its', () => { 8 | describe('with primitives', () => { 9 | subject(() => ({a: 1, b: 2})); 10 | its('a', () => isExpected.toEqual(1)); 11 | its('b', () => isExpected.toEqual(2)); 12 | }); 13 | 14 | describe('with functions', () => { 15 | subject(() => ({a: () => 1, b: () => 2})); 16 | its('a', () => isExpected.toEqual(1)); 17 | its('b', () => isExpected.toEqual(2)); 18 | }); 19 | 20 | describe('with nested properties', () => { 21 | subject(() => ({a: {b: {c: 1, d: 2}}})); 22 | its('a.b.c', () => isExpected.toEqual(1)); 23 | its('a.b.d', () => isExpected.toEqual(2)); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /packages/jest-plugin-its/src/index.js: -------------------------------------------------------------------------------- 1 | // Modules 2 | export its from './its'; 3 | export subject from './subject'; 4 | -------------------------------------------------------------------------------- /packages/jest-plugin-its/src/its.js: -------------------------------------------------------------------------------- 1 | // Libraries 2 | import invariant from 'invariant'; 3 | import set from 'jest-plugin-set'; 4 | 5 | 6 | // TOOD(mark): We should try to create a local scope to not clutter 7 | // global. This works for now. 8 | const scope = global; 9 | 10 | /** 11 | * Method can either be a single string `getTask` or it can be a chain 12 | * of methods `props.app.cdn`. We evaluate all parts of the split method 13 | * making sure to chain the caller as we traverse the methods. 14 | */ 15 | const evaluateMethod = (subject, method) => { 16 | const tokens = method.split('.'); 17 | let caller = subject; 18 | let value = subject; 19 | 20 | tokens.forEach(token => { 21 | caller = value; 22 | 23 | // By default, we just treat it as a property. 24 | // 25 | // Example: 26 | // its('id', () => {}); 27 | // 28 | // This tests `subject.id`. 29 | // 30 | value = value[token]; 31 | 32 | invariant( 33 | value !== undefined, 34 | `Undefined method: '${token}' for subject: ${caller}.`, 35 | ); 36 | 37 | // If it's a function (like a getter function) and it takes in 0 args, 38 | // then we can also evaluate it: 39 | // 40 | // Example: 41 | // its('getIsCurrent', () => {}); 42 | // 43 | // This tests `subject.getIsCurrent()`. 44 | // 45 | if (typeof value === 'function') { 46 | value = value.call(caller); 47 | } 48 | }); 49 | 50 | return value; 51 | }; 52 | 53 | const its = (method, expectation) => { 54 | describe(`#${method}`, () => { 55 | set('__itsSubject', () => { 56 | const subject = scope.__subject; 57 | 58 | invariant( 59 | subject !== undefined, 60 | `Subject is not defined. Make sure you set a subject using: ` + 61 | `'subject(() => someVariable);'`, 62 | ); 63 | 64 | return evaluateMethod(subject, method); 65 | }); 66 | 67 | set('isExpected', () => expect(scope.__itsSubject)); 68 | 69 | // TODO(mark): Better it messages, right now we only get a good message 70 | // when it fails. 71 | it(`is correct`, () => { 72 | // Call the expectation to assert values. 73 | 74 | // TODO(mark): expect.assertions(number) is coming soon to jest. 75 | // Until then, we'll have to keep hackily checking that the return 76 | // value is undefined. 77 | const result = expectation.call(scope); 78 | 79 | // All calls to `isExpected` will set the `actual` value. 80 | // This is a quick check to make sure `isExpected` was called. 81 | invariant( 82 | typeof result === 'undefined', 83 | `Expectation did not call 'isExpected'. Test did not validate ` + 84 | `anything.` 85 | ); 86 | }); 87 | }); 88 | }; 89 | 90 | 91 | export default its; 92 | -------------------------------------------------------------------------------- /packages/jest-plugin-its/src/subject.js: -------------------------------------------------------------------------------- 1 | // Libraries 2 | import invariant from 'invariant'; 3 | import set from 'jest-plugin-set'; 4 | 5 | 6 | // NOTE(mark): We have to give the globally set __subject a different 7 | // name so it doesn't override the function `subject`. 8 | const subject = (block) => { 9 | invariant( 10 | typeof block === 'function', 11 | `'subject' requires a function. Received: ${typeof block}.`, 12 | ); 13 | 14 | set('__subject', block); 15 | }; 16 | 17 | 18 | export default subject; 19 | -------------------------------------------------------------------------------- /packages/jest-plugin-set/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugin-set 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugin-set.svg)](https://www.npmjs.com/package/jest-plugin-set) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugin-set.svg)](https://www.npmjs.com/package/jest-plugin-set) 5 | [![npm](https://img.shields.io/npm/l/jest-plugin-set.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Declarative JS tests with lazy evaluation for jest. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugin-set` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugin-set 15 | ``` 16 | 17 | ## Motivation 18 | 19 | [RSpec](http://rspec.info/) took the ruby world by storm with its declarative method of TDD. Since moving to JavaScript, I've wanted a similar way of declaring the setup for my tests. Here's what you would normally do to declare a test: 20 | 21 | ```javascript 22 | describe('User', () => { 23 | let user; 24 | 25 | describe('.update', () => { 26 | beforeEach(() => { 27 | user = new User({firstName: 'Mary', lastName: 'Lamb'}); 28 | }); 29 | 30 | describe('with valid firstName and lastName', () => { 31 | let firstName; 32 | let lastName; 33 | 34 | beforeEach(() => { 35 | firstName = 'Test'; 36 | lastName = 'User'; 37 | user.update({firstName, lastName}); 38 | }); 39 | 40 | it('should set firstName', () => { 41 | expect(user.firstName).toEqual('Test'); 42 | }); 43 | 44 | it('should compute name', () => { 45 | expect(user.name).toEqual('Test User'); 46 | }); 47 | }); 48 | 49 | describe('with invalid firstName', () => { 50 | let firstName; 51 | let lastName; 52 | 53 | beforeEach(() => { 54 | firstName = null; 55 | lastName = null; 56 | user.update({firstName, lastName}); 57 | }); 58 | 59 | it('should not override the original firstName', () => { 60 | expect(user.firstName).toEqual('Mary'); 61 | }); 62 | }); 63 | }); 64 | }); 65 | ``` 66 | 67 | Some notes: 68 | 69 | 1. Because of scoping in javascript, we have to declare our variables outside the `beforeEach` blocks in order to reference them. 70 | 2. Our `beforeEach` blocks contain _all_ of the setup code necessary which in this trivial example is at least 3 lines per test. 71 | 3. We can override variables in nested scopes, but following the chain is non-trivial because the actual variable declaration might be several layers up. 72 | 73 | Here's what the same tests look like with using `set` from `jest-plugin-set`: 74 | 75 | ```javascript 76 | describe('User', () => { 77 | describe('.update', () => { 78 | set('user', () => new User({firstName: 'Mary', lastName: 'Lamb'})); 79 | 80 | describe('with valid firstName and lastName', () => { 81 | set('firstName', () => 'Test'); 82 | set('lastName', () => 'User'); 83 | 84 | beforeEach(() => user.update({firstName, lastName})); 85 | 86 | it('should set firstName', () => { 87 | expect(user.firstName).toEqual('Test'); 88 | }); 89 | 90 | it('should compute name', () => { 91 | expect(user.name).toEqual('Test User'); 92 | }); 93 | }); 94 | 95 | describe('with invalid firstName', () => { 96 | set('firstName', () => null); 97 | set('lastName', () => null); 98 | 99 | beforeEach(() => user.update({firstName, lastName})); 100 | 101 | it('should not override the original firstName', () => { 102 | expect(user.firstName).toEqual('Mary'); 103 | }); 104 | }); 105 | }); 106 | }); 107 | ``` 108 | 109 | Even in this trivial example, things are much easier to follow. 110 | 111 | 1. We can declare `firstName` and `lastName` as variables that we can then reference in our `beforeEach` blocks. 112 | 2. We can break up the large `beforeEach` blocks into several distinct `set` blocks. 113 | 3. We can easily set defaults in outer scopes (which may or may not be used within a particular test saving performance) and then overriding the values in nested blocks. 114 | 115 | ## Why `set`? 116 | 117 | In JavaScript, `let` is a keyword so the next closest word is...`set` (which still keeps the meaning of what we're doing - settings variables (lazily)). 118 | 119 | ## Usage 120 | 121 | If you want, you can import `set` from `jest-plugin-set` at the top of every test: 122 | 123 | ```javascript 124 | import set from 'jest-plugin-set'; 125 | ``` 126 | 127 | If you want to install `set` as a global, you can modify the `jest` section of your `package.json` to include: 128 | 129 | ```json 130 | "jest": { 131 | "setupFiles": [ 132 | "jest-plugin-set/setup" 133 | ] 134 | } 135 | ``` 136 | 137 | ## Example 138 | 139 | Here's an example test that tests `set` itself: 140 | 141 | ```javascript 142 | describe('set', () => { 143 | set('a', () => 1); 144 | set('b', () => 2); 145 | set('c', () => 'hello world'); 146 | 147 | describe('variables set to primitives', () => { 148 | it('should set a', () => { 149 | expect(a).toEqual(1); 150 | }); 151 | 152 | it('should set b', () => { 153 | expect(b).toEqual(2); 154 | }); 155 | 156 | it('should set c', () => { 157 | expect(c).toEqual('hello world'); 158 | }); 159 | }); 160 | 161 | describe('variables set to arrays', () => { 162 | set('a', () => [1, 2, 3]); 163 | 164 | it('should properly set arrays', () => { 165 | expect(a).toEqual([1, 2, 3]); 166 | }); 167 | }); 168 | 169 | describe('variables set to objects', () => { 170 | set('b', () => ({test: '1', value: 2, other: 'three'})); 171 | 172 | it('should properly set objects', () => { 173 | expect(b).toEqual({other: 'three', value: 2, test: '1'}); 174 | }); 175 | }); 176 | 177 | describe('nested set calls', () => { 178 | set('a', () => 10); 179 | 180 | it('should take the inner set', () => { 181 | expect(a).toEqual(10); 182 | }); 183 | }); 184 | 185 | describe('variables set within other set calls', () => { 186 | set('b', () => a + 10); 187 | 188 | it('should evaluate outer variables', () => { 189 | expect(b).toEqual(11); 190 | }); 191 | 192 | it('should be able to reference variables from the outer scope', () => { 193 | expect(c).toEqual('hello world'); 194 | }); 195 | }); 196 | }); 197 | ``` 198 | -------------------------------------------------------------------------------- /packages/jest-plugin-set/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugin-set", 3 | "version": "2.9.0", 4 | "description": "Declarative JS tests with lazy evaluation using jest.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-set", 9 | "keywords": [ 10 | "declarative", 11 | "jest", 12 | "jest-plugin", 13 | "let", 14 | "plugin", 15 | "set", 16 | "tdd", 17 | "test" 18 | ], 19 | "main": "build/set", 20 | "files": [ 21 | "build", 22 | "setup.js" 23 | ], 24 | "peerDependencies": { 25 | "jest": "*" 26 | }, 27 | "scripts": { 28 | "build": "babel src --out-dir build --ignore '**/*.spec.js'", 29 | "clean": "rimraf build", 30 | "prepublishOnly": "yarn run clean && yarn run build" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /packages/jest-plugin-set/setup.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Installs set and makes it available as a global function throughout all 3 | * test files. 4 | */ 5 | global.set = require('./').default; 6 | -------------------------------------------------------------------------------- /packages/jest-plugin-set/src/__tests__/set.spec.js: -------------------------------------------------------------------------------- 1 | // Modules 2 | import set from '../set'; 3 | 4 | 5 | /* eslint-disable no-undef */ 6 | describe('set', () => { 7 | set('a', () => 1); 8 | set('b', () => 2); 9 | set('c', () => 'hello world'); 10 | 11 | describe('variables set to primitives', () => { 12 | it('should set a', () => { 13 | expect(a).toEqual(1); 14 | }); 15 | 16 | it('should set b', () => { 17 | expect(b).toEqual(2); 18 | }); 19 | 20 | it('should set c', () => { 21 | expect(c).toEqual('hello world'); 22 | }); 23 | }); 24 | 25 | describe('variables set to arrays', () => { 26 | set('a', () => [1, 2, 3]); 27 | 28 | it('should properly set arrays', () => { 29 | expect(a).toEqual([1, 2, 3]); 30 | }); 31 | }); 32 | 33 | describe('variables set to objects', () => { 34 | set('b', () => ({test: '1', value: 2, other: 'three'})); 35 | 36 | it('should properly set objects', () => { 37 | expect(b).toEqual({other: 'three', value: 2, test: '1'}); 38 | }); 39 | }); 40 | 41 | describe('nested set calls', () => { 42 | set('a', () => 10); 43 | 44 | it('should take the inner set', () => { 45 | expect(a).toEqual(10); 46 | }); 47 | }); 48 | 49 | describe('variables set within other set calls', () => { 50 | set('b', () => a + 10); 51 | 52 | it('should evaluate outer variables', () => { 53 | expect(b).toEqual(11); 54 | }); 55 | 56 | it('should be able to reference variables from the outer scope', () => { 57 | expect(c).toEqual('hello world'); 58 | }); 59 | }); 60 | }); 61 | -------------------------------------------------------------------------------- /packages/jest-plugin-set/src/define.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Defines a variable called `name` in scope `scope`. The value is evaluated 3 | * the only when the variable is used. This allows us to create 4 | * lazily evaluated variables for testing purposes. 5 | */ 6 | const define = ({scope, name, block}) => { 7 | let value; 8 | let isEvaluated = false; 9 | 10 | Object.defineProperty(scope, name, { 11 | configurable: true, 12 | 13 | // Set to the lazily evaluated function. 14 | get() { 15 | // If the value has been computed before, return that value. 16 | // Otherwise, we should compute it! 17 | if (!isEvaluated) { 18 | try { 19 | value = block.call(scope); 20 | } finally { 21 | isEvaluated = true; 22 | } 23 | } 24 | 25 | return value; 26 | }, 27 | }); 28 | }; 29 | 30 | 31 | export default define; 32 | -------------------------------------------------------------------------------- /packages/jest-plugin-set/src/set.js: -------------------------------------------------------------------------------- 1 | // Modules 2 | import define from './define'; 3 | import undefine from './undefine'; 4 | 5 | 6 | /** 7 | * 'set' allows us to lazily evaluate blocks and override them within 8 | * different 'describe' blocks. 9 | */ 10 | const set = (name, block) => { 11 | beforeEach(() => { 12 | define({scope: global, name, block}); 13 | }); 14 | 15 | afterEach(() => { 16 | undefine({scope: global, name}); 17 | }); 18 | }; 19 | 20 | 21 | export default set; 22 | -------------------------------------------------------------------------------- /packages/jest-plugin-set/src/undefine.js: -------------------------------------------------------------------------------- 1 | const undefine = ({scope, name}) => { 2 | Object.defineProperty(scope, name, { 3 | configurable: true, 4 | 5 | get() { 6 | return undefined; 7 | }, 8 | }); 9 | }; 10 | 11 | 12 | export default undefine; 13 | -------------------------------------------------------------------------------- /packages/jest-plugin-unhandled-promise/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugin-unhandled-promise 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugin-unhandled-promise.svg)](https://www.npmjs.com/package/jest-plugin-unhandled-promise) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugin-unhandled-promise.svg)](https://www.npmjs.com/package/jest-plugin-unhandled-promise) 5 | [![npm](https://img.shields.io/npm/l/jest-plugin-unhandled-promise.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Surfaces unhandled promise rejections in jest tests. 8 | 9 | ## NOTE: This is unecessary in Jest 21+ 10 | 11 | Jest 21 introduced unhandled promise handling natively within the framework. Therefore, this plugin is unnecessary. 12 | 13 | ## Getting Started 14 | 15 | Install `jest-plugin-unhandled-promise` using `yarn`: 16 | 17 | ```shell 18 | yarn add --dev jest-plugin-unhandled-promise 19 | ``` 20 | 21 | ## Motivation 22 | 23 | Currently, when running tests in `jest`, if you have an unhandled promise rejection, you'll see this warning: 24 | 25 | ``` 26 | (node:23112) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: unhandled error 27 | (node:23112) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 28 | ``` 29 | 30 | Your tests may or may not pass, and you might miss this error. 31 | 32 | With `jest-plugin-unhandled-promise`, you'll see this nice, red stack trace in your console: 33 | 34 | ``` 35 | Error: unhandled error 36 | at /Users/mark/projects/negativetwelve/jest-plugin-unhandled-promise/src/__tests__/install.spec.js:9:61 37 | at Promise () 38 | at /Users/mark/projects/negativetwelve/jest-plugin-unhandled-promise/src/__tests__/install.spec.js:9:7 39 | at Promise () 40 | at /Users/mark/projects/negativetwelve/jest-plugin-unhandled-promise/src/__tests__/install.spec.js:7:56 41 | at _callee4$ (/Users/mark/projects/negativetwelve/jest-plugin-unhandled-promise/src/__tests__/install.spec.js:59:17) 42 | at tryCatch (/Users/mark/projects/negativetwelve/jest-plugin-unhandled-promise/node_modules/regenerator-runtime/runtime.js:65:40) 43 | at Generator.invoke [as _invoke] (/Users/mark/projects/negativetwelve/jest-plugin-unhandled-promise/node_modules/regenerator-runtime/runtime.js:303:22) 44 | at Generator.prototype.(anonymous function) [as next] (/Users/mark/projects/negativetwelve/jest-plugin-unhandled-promise/node_modules/regenerator-runtime/runtime.js:117:21) 45 | at step (/Users/mark/projects/negativetwelve/jest-plugin-unhandled-promise/src/__tests__/install.spec.js:2:364) 46 | ``` 47 | 48 | Now you can easily handle errors and fix up your specs! 49 | 50 | ## Usage 51 | 52 | If you want, you can import `installUnhandledPromise` from `jest-plugin-unhandled-promise` at the top of every test: 53 | 54 | ```javascript 55 | import installUnhandledPromise from 'jest-plugin-unhandled-promise'; 56 | 57 | // Pass in your custom log function. 58 | installUnhandledPromise(console.log); 59 | ``` 60 | 61 | If you want to install it as a global for all tests, you can modify the `jest` section of your `package.json` to include: 62 | 63 | ```json 64 | "jest": { 65 | "setupFiles": [ 66 | "jest-plugin-unhandled-promise/setup" 67 | ] 68 | } 69 | ``` 70 | 71 | ## Contributing 72 | 73 | If you have any ideas on how this module could be better, [create an Issue](https://github.com/negativetwelve/jest-plugins/issues) or [submit a PR](https://github.com/negativetwelve/jest-plugins/pulls). 74 | -------------------------------------------------------------------------------- /packages/jest-plugin-unhandled-promise/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugin-unhandled-promise", 3 | "version": "2.9.0", 4 | "description": "Surfaces unhandled promise rejections in jest tests.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-unhandled-promise", 9 | "keywords": [ 10 | "dx", 11 | "jest", 12 | "jest-plugin", 13 | "plugin", 14 | "promise", 15 | "test" 16 | ], 17 | "main": "build/install", 18 | "files": [ 19 | "build", 20 | "setup.js" 21 | ], 22 | "peerDependencies": { 23 | "jest": "*" 24 | }, 25 | "devDependencies": { 26 | "jest-plugin-action": "^2.9.0", 27 | "jest-plugin-console-matchers": "^2.9.0", 28 | "jest-plugin-context": "^2.9.0", 29 | "jest-plugin-set": "^2.9.0" 30 | }, 31 | "scripts": { 32 | "build": "babel src --out-dir build --ignore '**/*.spec.js'", 33 | "clean": "rimraf build", 34 | "prepublishOnly": "yarn run clean && yarn run build" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /packages/jest-plugin-unhandled-promise/setup.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Installs jest-plugin-unhandled-promise. Add this to jest's `setupFiles`. 3 | * This uses the default `console.error` logger. 4 | */ 5 | require('./').default(); 6 | -------------------------------------------------------------------------------- /packages/jest-plugin-unhandled-promise/src/install.js: -------------------------------------------------------------------------------- 1 | // Log all unhandled promises so we can find the source of them. 2 | export default (log = console.error) => { 3 | process.on('unhandledRejection', error => { 4 | log(error.stack); 5 | }); 6 | }; 7 | -------------------------------------------------------------------------------- /packages/jest-plugins-react/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugins-react 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugins-react.svg)](https://www.npmjs.com/package/jest-plugins-react) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugins-react.svg)](https://www.npmjs.com/package/jest-plugins-react) 5 | [![npm](https://img.shields.io/npm/l/jest-plugins-react.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Recommended set of jest plugins. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugins-react` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugins-react 15 | ``` 16 | 17 | ## Usage 18 | 19 | In your `jest-plugins.js` file, add: 20 | 21 | ```javascript 22 | // Require jest-plugins and install them. 23 | require('jest-plugins')([ 24 | 'jest-plugins-react', 25 | ]); 26 | ``` 27 | 28 | ## Contributing 29 | 30 | If you have any ideas on how this module could be better, [create an Issue](https://github.com/negativetwelve/jest-plugins/issues) or [submit a PR](https://github.com/negativetwelve/jest-plugins/pulls). 31 | -------------------------------------------------------------------------------- /packages/jest-plugins-react/__tests__/__snapshots__/plugins.spec.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`jest-plugins-react itRenders renders snapshot 1`] = `
`; 4 | 5 | exports[`jest-plugins-react itRenders renders snapshot 2`] = `
`; 6 | 7 | exports[`jest-plugins-react itShallowRenders shallow renders snapshot 1`] = `
`; 8 | 9 | exports[`jest-plugins-react itShallowRenders shallow renders snapshot 2`] = ` 10 | 11 |
12 | 13 | `; 14 | -------------------------------------------------------------------------------- /packages/jest-plugins-react/__tests__/plugins.spec.js: -------------------------------------------------------------------------------- 1 | // Modules 2 | import 'raf/polyfill'; 3 | import '../setup'; 4 | import React from 'react'; 5 | 6 | 7 | // Test Components 8 | const View = () =>
; 9 | const Text = () =>
; 10 | const Undefined = () => undefined; 11 | 12 | 13 | /* eslint-disable no-undef */ 14 | describe('jest-plugins-react', () => { 15 | const globals = [ 16 | 'itRenders', 17 | 'itShallowRenders', 18 | ]; 19 | 20 | globals.forEach(globalFunction => { 21 | describe(globalFunction, () => { 22 | it('should be defined as a global', () => { 23 | expect(global[globalFunction]).toBeDefined(); 24 | }); 25 | }); 26 | }); 27 | 28 | describe('itRenders', () => { 29 | itRenders(() => ); 30 | itRenders(() => ); 31 | }); 32 | 33 | describe('itShallowRenders', () => { 34 | itShallowRenders(() => ); 35 | itShallowRenders(() => ); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /packages/jest-plugins-react/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugins-react", 3 | "version": "2.9.0", 4 | "description": "Jest plugins for testing React components.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugins-react", 9 | "keywords": [ 10 | "component", 11 | "jest", 12 | "jest-plugins", 13 | "plugins", 14 | "react", 15 | "react-native", 16 | "render", 17 | "test" 18 | ], 19 | "files": [ 20 | "setup.js" 21 | ], 22 | "dependencies": { 23 | "jest-plugin-it-renders": "^2.9.0", 24 | "jest-plugin-it-shallow-renders": "^2.9.0", 25 | "jest-plugins": "^2.9.0" 26 | }, 27 | "peerDependencies": { 28 | "jest": "*" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /packages/jest-plugins-react/setup.js: -------------------------------------------------------------------------------- 1 | // Sets up the plugins to help test React components. 2 | require('jest-plugins')([ 3 | 'jest-plugin-it-renders', 4 | 'jest-plugin-it-shallow-renders', 5 | ]); 6 | -------------------------------------------------------------------------------- /packages/jest-plugins-recommended/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugins-recommended 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugins-recommended.svg)](https://www.npmjs.com/package/jest-plugins-recommended) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugins-recommended.svg)](https://www.npmjs.com/package/jest-plugins-recommended) 5 | [![npm](https://img.shields.io/npm/l/jest-plugins-recommended.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Recommended set of jest plugins. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugins-recommended` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugins-recommended 15 | ``` 16 | 17 | ## Usage 18 | 19 | In your `jest-plugins.js` file, add: 20 | 21 | ```javascript 22 | // Require jest-plugins and install them. 23 | require('jest-plugins')([ 24 | 'jest-plugins-recommended', 25 | ]); 26 | ``` 27 | 28 | ## Contributing 29 | 30 | If you have any ideas on how this module could be better, [create an Issue](https://github.com/negativetwelve/jest-plugins/issues) or [submit a PR](https://github.com/negativetwelve/jest-plugins/pulls). 31 | -------------------------------------------------------------------------------- /packages/jest-plugins-recommended/__tests__/plugins.spec.js: -------------------------------------------------------------------------------- 1 | // Modules 2 | import '../setup'; 3 | 4 | 5 | /* eslint-disable no-undef */ 6 | describe('jest-plugins-recommended', () => { 7 | const globals = [ 8 | 'action', 9 | 'clock', 10 | 'context', 11 | 'forEach', 12 | 'its', 13 | 'set', 14 | 'subject', 15 | ]; 16 | 17 | globals.forEach(globalFunction => { 18 | describe(globalFunction, () => { 19 | it('should be defined as a global', () => { 20 | expect(global[globalFunction]).toBeDefined(); 21 | }); 22 | }); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /packages/jest-plugins-recommended/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugins-recommended", 3 | "version": "2.9.0", 4 | "description": "Recommended set of jest plugins.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugins-recommended", 9 | "keywords": [ 10 | "dx", 11 | "jest", 12 | "jest-plugins", 13 | "plugins", 14 | "test" 15 | ], 16 | "files": [ 17 | "setup.js" 18 | ], 19 | "dependencies": { 20 | "jest-plugin-action": "^2.9.0", 21 | "jest-plugin-clock": "^2.9.0", 22 | "jest-plugin-console-matchers": "^2.9.0", 23 | "jest-plugin-for-each": "^2.9.0", 24 | "jest-plugins": "^2.9.0", 25 | "jest-plugins-rspec": "^2.9.0" 26 | }, 27 | "peerDependencies": { 28 | "jest": "*" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /packages/jest-plugins-recommended/setup.js: -------------------------------------------------------------------------------- 1 | // Sets up the recommended set of plugins. 2 | require('jest-plugins')([ 3 | 'jest-plugins-rspec', 4 | 5 | 'jest-plugin-action', 6 | 'jest-plugin-clock', 7 | 'jest-plugin-console-matchers', 8 | 'jest-plugin-for-each', 9 | ]); 10 | -------------------------------------------------------------------------------- /packages/jest-plugins-rspec/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugins-rspec 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugins-rspec.svg)](https://www.npmjs.com/package/jest-plugins-rspec) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugins-rspec.svg)](https://www.npmjs.com/package/jest-plugins-rspec) 5 | [![npm](https://img.shields.io/npm/l/jest-plugins-rspec.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Recommended set of jest plugins. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugins-rspec` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugins-rspec 15 | ``` 16 | 17 | ## Usage 18 | 19 | In your `jest-plugins.js` file, add: 20 | 21 | ```javascript 22 | // Require jest-plugins and install them. 23 | require('jest-plugins')([ 24 | 'jest-plugins-rspec', 25 | ]); 26 | ``` 27 | 28 | ## Contributing 29 | 30 | If you have any ideas on how this module could be better, [create an Issue](https://github.com/negativetwelve/jest-plugins/issues) or [submit a PR](https://github.com/negativetwelve/jest-plugins/pulls). 31 | -------------------------------------------------------------------------------- /packages/jest-plugins-rspec/__tests__/plugins.spec.js: -------------------------------------------------------------------------------- 1 | // Modules 2 | import '../setup'; 3 | 4 | 5 | /* eslint-disable no-undef */ 6 | describe('jest-plugins-rspec', () => { 7 | const globals = [ 8 | 'context', 9 | 'its', 10 | 'set', 11 | 'subject', 12 | ]; 13 | 14 | globals.forEach(globalFunction => { 15 | describe(globalFunction, () => { 16 | it('should be defined as a global', () => { 17 | expect(global[globalFunction]).toBeDefined(); 18 | }); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /packages/jest-plugins-rspec/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugins-rspec", 3 | "version": "2.9.0", 4 | "description": "Jest plugins to emulate RSpec syntax and structure.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugins-rspec", 9 | "keywords": [ 10 | "context", 11 | "dx", 12 | "its", 13 | "jest", 14 | "jest-plugins", 15 | "let", 16 | "plugins", 17 | "rspec", 18 | "set", 19 | "test" 20 | ], 21 | "files": [ 22 | "setup.js" 23 | ], 24 | "dependencies": { 25 | "jest-plugin-context": "^2.9.0", 26 | "jest-plugin-its": "^2.9.0", 27 | "jest-plugin-set": "^2.9.0", 28 | "jest-plugins": "^2.9.0" 29 | }, 30 | "peerDependencies": { 31 | "jest": "*" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /packages/jest-plugins-rspec/setup.js: -------------------------------------------------------------------------------- 1 | // Sets up the plugins for an RSpec like environment. 2 | require('jest-plugins')([ 3 | 'jest-plugin-context', 4 | 'jest-plugin-its', 5 | 'jest-plugin-set', 6 | ]); 7 | -------------------------------------------------------------------------------- /packages/jest-plugins/README.md: -------------------------------------------------------------------------------- 1 | # jest-plugins 2 | 3 | [![npm](https://img.shields.io/npm/v/jest-plugins.svg)](https://www.npmjs.com/package/jest-plugins) 4 | [![npm](https://img.shields.io/npm/dt/jest-plugins.svg)](https://www.npmjs.com/package/jest-plugins) 5 | [![npm](https://img.shields.io/npm/l/jest-plugins.svg)](https://github.com/negativetwelve/jest-plugins/blob/master/LICENSE) 6 | 7 | Adds plugins support to Jest. 8 | 9 | ## Getting Started 10 | 11 | Install `jest-plugins` using `yarn`: 12 | 13 | ```shell 14 | yarn add --dev jest-plugins 15 | ``` 16 | 17 | ## Usage 18 | 19 | In order to utilize `jest-plugins` in your project, you must configure the setup to add your custom plugins. If `jest-plugins` ever is merged into `jest`, then this will be simpler, until then, follow the instructions below. 20 | 21 | First, find your `jest` config which is either in your `package.json` or in a top-level file called `jest.config.js`. Add a key `setupTestFrameworkScriptFile` and point it to another file at the top-level called `jest-plugins.js`. 22 | 23 | Inside `jest-plugins.js`, write the following: 24 | 25 | ```javascript 26 | require('jest-plugins')([ 27 | // List all jest-plugins here. 28 | 'jest-plugin-action', 29 | 'jest-plugin-console-matchers', 30 | 'jest-plugin-context', 31 | 'jest-plugin-its', 32 | 'jest-plugin-set', 33 | 'jest-plugin-unhandled-promise', 34 | ]); 35 | ``` 36 | 37 | This is requiring each of the plugins and including it automatically in your project. By design, the `setupTestFrameworkScriptFile` runs _after_ `jest` and the test environment have been set up which gives plugins the maximum capabilities to modify the environment. 38 | 39 | Once installed, run `jest` to run your tests and plugins should be accessible in your test files. 40 | -------------------------------------------------------------------------------- /packages/jest-plugins/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-plugins", 3 | "version": "2.9.0", 4 | "description": "Adds plugins support to jest.", 5 | "license": "MIT", 6 | "author": "Mark Miyashita ", 7 | "homepage": "https://github.com/negativetwelve/jest-plugins", 8 | "repository": "https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugins", 9 | "keywords": [ 10 | "jest", 11 | "jest-plugin", 12 | "jest-plugins", 13 | "plugins", 14 | "test" 15 | ], 16 | "main": "build/install", 17 | "files": [ 18 | "build", 19 | "setup.js" 20 | ], 21 | "peerDependencies": { 22 | "jest": "*" 23 | }, 24 | "scripts": { 25 | "build": "babel src --out-dir build --ignore '**/*.spec.js'", 26 | "clean": "rimraf build", 27 | "prepublishOnly": "yarn run clean && yarn run build" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /packages/jest-plugins/setup.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Ideally, this would be the only entry point to jest-plugins. However, we 3 | * have a couple of issues to overcome: 4 | * 5 | * 1. jest-config and jest-validate work together to validate the config. 6 | * This raises warnings whenever a custom key is applied to the config. 7 | * 2. We cannot reliably require the package.json of the currently tested 8 | * project because of the new `projects` feature of jest. Once we have 9 | * a way to resolve the package.json, we can handle setting up the plugins 10 | * automatically. 11 | * 12 | */ 13 | -------------------------------------------------------------------------------- /packages/jest-plugins/src/install.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Takes in a package.json from the root directory. This reads the package.json 3 | * and installs all jest-plugins. If a plugin is incorrectly installed or not 4 | * found, it will throw an error. 5 | */ 6 | module.exports = (plugins) => { 7 | plugins.forEach(plugin => { 8 | try { 9 | require(`${plugin}/setup`); 10 | } catch (error) { 11 | throw new Error( 12 | `Unable to find jest plugin: '${plugin}'. Check that your ` + 13 | `dependencies are installed correctly and that '${plugin}' is a ` + 14 | `valid jest plugin. Full error below:\n\n${error.message}` 15 | ); 16 | } 17 | }); 18 | }; 19 | --------------------------------------------------------------------------------