Dummy - rohit120582sharma/Documentation GitHub Wiki

npm install --save-dev angular-mocks npm install --save-dev jest-cli npm install --save-dev @types/jest npm install --save-dev raf whatwg-fetch npm install --save-dev babel-jest npm install --save-dev babel-preset-env

package.json

{ "scripts": { "test": "jest", "test:watch": "jest --watch", "test:coverage": "jest --coverage" }, "jest": { "collectCoverageFrom": [ "**/.{js,jsx}" ], "coveragePathIgnorePatterns": [ "/\\[/\\]" ], "setupFiles": [ "/config/jest/polyfills.js" ], "setupTestFrameworkScriptFile": "/app/jest.init.js", "testPathIgnorePatterns": [ "/\\[/\\]" ], "testURL": "http://localhost", "transform": { "^.+\.(js|jsx)$": "/node_modules/babel-jest", "^.+\.css$": "/config/jest/cssTransform.js", "^(?!.\.(js|jsx|css|json)$)": "/config/jest/fileTransform.js" }, "transformIgnorePatterns": [ "[/\\]node_modules[/\\].+\.(js|jsx)$" ] } }

.babelrc

{ "presets": [ [ "env", { "targets": { "browsers": "last 2 versions" }, "loose": true, "modules": false } ] ], "env": { "test": { "plugins": ["transform-es2015-modules-commonjs"] } } }

jest.init.js

import angular from 'angular'; import jQuery from 'jquery'; import angularMocks from 'angular-mocks';

global.console = { log: jest.fn(), group: jest.fn(), groupEnd: jest.fn() }

Object.defineProperty(window, 'jQuery', { value: jQuery }); Object.defineProperty(window, '$', { value: jQuery }); Object.defineProperty(window, 'angular', { value: angular });

const mock = () => { let storage = {}; return { getItem: key => key in storage ? storage[key] : null, setItem: (key, value) => storage[key] = value || '', removeItem: key => delete storage[key], clear: () => storage = {}, }; }; Object.defineProperty(window, 'localStorage', {value: mock()}); Object.defineProperty(window, 'sessionStorage', {value: mock()}); Object.defineProperty(window, 'getComputedStyle', { value: () => ['-webkit-appearance'] });

config/jest/polyfills.js

'use strict';

if (typeof Promise === 'undefined') { // Rejection tracking prevents a common issue where React gets into an // inconsistent state due to an error, but it gets swallowed by a Promise, // and the user has no idea what causes React's erratic future behavior. require('promise/lib/rejection-tracking').enable(); window.Promise = require('promise/lib/es6-extensions.js'); }

// fetch() polyfill for making API calls. require('whatwg-fetch');

// Object.assign() is commonly used with React. // It will use the native implementation if it's present and isn't buggy. Object.assign = require('object-assign');

// In tests, polyfill requestAnimationFrame since jsdom doesn't provide it yet. // We don't polyfill it in the browser--this is user's responsibility. if (process.env.NODE_ENV === 'test') { require('raf').polyfill(global); }

config/jest/cssTransform.js

'use strict';

// This is a custom Jest transformer turning style imports into empty objects. // http://facebook.github.io/jest/docs/en/webpack.html

module.exports = { process() { return 'module.exports = {};'; }, getCacheKey() { // The output is always the same. return 'cssTransform'; }, };

config/jest/fileTransform.js

'use strict';

const path = require('path');

// This is a custom Jest transformer turning file imports into filenames. // http://facebook.github.io/jest/docs/en/webpack.html

module.exports = { process(src, filename) { return module.exports = ${JSON.stringify(path.basename(filename))};; }, };

⚠️ **GitHub.com Fallback** ⚠️