Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create empty mocks for jest #303

Merged
merged 19 commits into from
May 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion TestApp/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"parser": "babel-eslint",
"env": {
"es6": true
"es6": true,
"jest": true
},
"plugins": [
"react"
Expand Down
8 changes: 7 additions & 1 deletion TestApp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
"react-test-renderer": "^16.1.1"
},
"jest": {
"preset": "react-native"
"preset": "react-native",
"setupFiles": [
"./node_modules/appcenter/test/AppCenterMock.js",
"./node_modules/appcenter-analytics/test/AppCenterAnalyticsMock.js",
"./node_modules/appcenter-crashes/test/AppCenterCrashesMock.js",
"./node_modules/appcenter-push/test/AppCenterPushMock.js"
]
}
}
3 changes: 2 additions & 1 deletion TestApp34/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"parser": "babel-eslint",
"env": {
"es6": true
"es6": true,
"jest": true
},
"plugins": [
"react"
Expand Down
3 changes: 2 additions & 1 deletion appcenter-analytics/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"parser": "babel-eslint",
"env": {
"es6": true
"es6": true,
"jest": true
},
"plugins": [
"react"
Expand Down
3 changes: 2 additions & 1 deletion appcenter-analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"eslint-plugin-react": "^7.2.1"
},
"scripts": {
"lint": "./node_modules/.bin/eslint ."
"lint": "./node_modules/.bin/eslint .",
"postinstall": "node scripts/postinstall"
}
}
3 changes: 3 additions & 0 deletions appcenter-analytics/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const mock = require('../../appcenter/test/SetupAppCenterMock');

mock.setupMock('appcenter-analytics', 'AppCenterAnalyticsMock.js');
5 changes: 5 additions & 0 deletions appcenter-analytics/test/AppCenterAnalyticsMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
jest.mock('appcenter-analytics', () => ({
trackEvent: jest.fn(),
isEnabled: jest.fn(),
setEnabled: jest.fn()
}));
3 changes: 2 additions & 1 deletion appcenter-crashes/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"parser": "babel-eslint",
"env": {
"es6": true
"es6": true,
"jest": true
},
"plugins": [
"react"
Expand Down
3 changes: 2 additions & 1 deletion appcenter-crashes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"eslint-plugin-react": "^7.2.1"
},
"scripts": {
"lint": "./node_modules/.bin/eslint ."
"lint": "./node_modules/.bin/eslint .",
"postinstall": "node scripts/postinstall"
}
}
3 changes: 3 additions & 0 deletions appcenter-crashes/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const mock = require('../../appcenter/test/SetupAppCenterMock');

mock.setupMock('appcenter-crashes', 'AppCenterCrashesMock.js');
9 changes: 9 additions & 0 deletions appcenter-crashes/test/AppCenterCrashesMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
jest.mock('appcenter-crashes', () => ({
generateTestCrash: jest.fn(),
hasCrashedInLastSession: jest.fn(),
lastSessionCrashReport: jest.fn(),
isEnabled: jest.fn(),
setEnabled: jest.fn(),
notifyUserConfirmation: jest.fn(),
setListener: jest.fn(),
}));
3 changes: 2 additions & 1 deletion appcenter-push/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"parser": "babel-eslint",
"env": {
"es6": true
"es6": true,
"jest": true
},
"plugins": [
"react"
Expand Down
3 changes: 2 additions & 1 deletion appcenter-push/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"eslint-plugin-react": "^7.2.1"
},
"scripts": {
"lint": "./node_modules/.bin/eslint ."
"lint": "./node_modules/.bin/eslint .",
"postinstall": "node scripts/postinstall"
}
}
3 changes: 3 additions & 0 deletions appcenter-push/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const mock = require('../../appcenter/test/SetupAppCenterMock');

mock.setupMock('appcenter-push', 'AppCenterPushMock.js');
5 changes: 5 additions & 0 deletions appcenter-push/test/AppCenterPushMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
jest.mock('appcenter-push', () => ({
isEnabled: jest.fn(),
setEnabled: jest.fn(),
setListener: jest.fn()
}));
1 change: 1 addition & 0 deletions appcenter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"scripts": {
"lint": "./node_modules/.bin/eslint .",
"postinstall": "node scripts/postinstall",
"test": "jest"
},
"jest": {
Expand Down
3 changes: 3 additions & 0 deletions appcenter/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const mock = require('../test/SetupAppCenterMock');

mock.setupMock('appcenter', 'AppCenterMock.js');
9 changes: 9 additions & 0 deletions appcenter/test/AppCenterMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
jest.mock('appcenter', () => ({
getLogLevel: jest.fn(),
setLogLevel: jest.fn(),
getInstallId: jest.fn(),
isEnabled: jest.fn(),
setEnabled: jest.fn(),
setCustomProperties: jest.fn(),
getSdkVersion: jest.fn()
}));
33 changes: 33 additions & 0 deletions appcenter/test/SetupAppCenterMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fs = require('fs');
const path = require('path');

module.exports = {
setupMock(moduleName, setupFileName) {
const projectDirectory = path.resolve(__dirname, '..', '..', '..');
const setupFileNamePath = `.${path.sep}node_modules${path.sep}${moduleName}${path.sep}test${path.sep}${setupFileName}`;

// Check if package.json has jest as dependency
let packageJson = '';
const packageJsonFile = path.join(`${projectDirectory}`, 'package.json');
try {
packageJson = JSON.parse(fs.readFileSync(packageJsonFile, 'utf8'));
if (!Object.prototype.hasOwnProperty.call(packageJson.devDependencies, 'jest') &&
!Object.prototype.hasOwnProperty.call(packageJson.dependencies, 'jest')) {
return;
}
} catch (e) {
console.log('Could not read package.json file');
return;
}

// Add setup file for module
if (Object.prototype.hasOwnProperty.call(packageJson, 'jest')) {
if (packageJson.jest.setupFiles === undefined) {
packageJson.jest.setupFiles = [setupFileNamePath];
} else if (packageJson.jest.setupFiles.indexOf(setupFileNamePath) === -1) {
packageJson.jest.setupFiles.push(setupFileNamePath);
}
fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson));
}
}
};