Skip to content

Instantly share code, notes, and snippets.

@ovrmrw
Last active March 4, 2018 14:24
Show Gist options
  • Save ovrmrw/57f8e02f5776656825f8db8867f03578 to your computer and use it in GitHub Desktop.
Save ovrmrw/57f8e02f5776656825f8db8867f03578 to your computer and use it in GitHub Desktop.
Create files for Jest testing for Ionic project
const fs = require('fs');
const path = require('path');
const root = path.resolve();
// Create jest.config.js
const jestConfigJS = `module.exports = {
preset: 'jest-preset-angular',
roots: ['src'],
testRegex: '\\\\.spec\\\\.ts$',
setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts',
transformIgnorePatterns: ['node_modules/(?!@ngrx|@ionic-native|@ionic)']
};
`;
fs.writeFileSync(path.join(root, 'jest.config.js'), jestConfigJS);
// Create src/setupJest.ts
const setupJestTs = `import 'jest-preset-angular';
import './jestGlobalMocks'; // browser mocks globally available for every test
`;
fs.writeFileSync(path.join(root, 'src', 'setupJest.ts'), setupJestTs);
// Create src/jestGlobalMocks.ts
const jestGlobalMocksTs = `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']
});
`;
fs.writeFileSync(path.join(root, 'src', 'jestGlobalMocks.ts'), jestGlobalMocksTs);
// Create src/tsconfig.spec.json
const tsconfigSpecJson = `{
"extends": "../tsconfig.json",
"include": [
"**/*.spec.ts"
],
"exclude": [
"node_modules"
]
}
`;
fs.writeFileSync(path.join(root, 'src', 'tsconfig.spec.json'), tsconfigSpecJson);
@ovrmrw
Copy link
Author

ovrmrw commented Mar 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment