Last active
March 4, 2018 14:24
-
-
Save ovrmrw/57f8e02f5776656825f8db8867f03578 to your computer and use it in GitHub Desktop.
Create files for Jest testing for Ionic project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Qiita: https://qiita.com/ovrmrw/items/a25a229da02a6dbe2603