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 13 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
10 changes: 10 additions & 0 deletions TestApp/__mocks__/appcenter-crashes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

const Crashes = jest.mock('appcenter-crashes');
Crashes.generateTestCrash = jest.fn();
Crashes.hasCrashedInLastSession = jest.fn();
Crashes.lastSessionCrashReport = jest.fn();
Crashes.isEnabled = jest.fn();
Crashes.setEnabled = jest.fn();
Crashes.notifyUserConfirmation = jest.fn();
Crashes.setListener = jest.fn();
export default Crashes;
6 changes: 6 additions & 0 deletions TestApp/__mocks__/appcenter-push.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

const Push = jest.mock('appcenter-push');
Push.isEnabled = jest.fn();
Push.setEnabled = jest.fn();
Push.setListener = jest.fn();
export default Push;
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"
}
}
29 changes: 29 additions & 0 deletions appcenter-analytics/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require('fs');
const path = require('path');

const projectDirectory = path.resolve(__dirname, '..', '..', '..');
const setupFileName = `.${path.sep}node_modules${path.sep}appcenter-analytics${path.sep}test${path.sep}appCenterAnalyticsMock.js`;

// 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 = [setupFileName];
} else if (packageJson.jest.setupFiles.indexOf(setupFileName) === -1) {
packageJson.jest.setupFiles.push(setupFileName);
}
fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson));
}
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"
}
}
29 changes: 29 additions & 0 deletions appcenter-crashes/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require('fs');
const path = require('path');

const projectDirectory = path.resolve(__dirname, '..', '..', '..');
const setupFileName = `.${path.sep}node_modules${path.sep}appcenter-crashes${path.sep}test${path.sep}appCenterCrashesMock.js`;

// 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 = [setupFileName];
} else if (packageJson.jest.setupFiles.indexOf(setupFileName) === -1) {
packageJson.jest.setupFiles.push(setupFileName);
}
fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson));
}
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"
}
}
29 changes: 29 additions & 0 deletions appcenter-push/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require('fs');
const path = require('path');

const projectDirectory = path.resolve(__dirname, '..', '..', '..');
const setupFileName = `.${path.sep}node_modules${path.sep}appcenter-push${path.sep}test${path.sep}appCenterPushMock.js`;

// Check if package.json has jest as dependency
let packageJson = '';
const packageJsonFile = path.join(`${projectDirectory}`, 'package.json');
Copy link
Member

@guperrot guperrot May 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach seems ok now based on latest discussion.

However this is duplicated code, we can put shared script code in the linker, I mean the core package and use it here.

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 = [setupFileName];
} else if (packageJson.jest.setupFiles.indexOf(setupFileName) === -1) {
packageJson.jest.setupFiles.push(setupFileName);
}
fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson));
}
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
29 changes: 29 additions & 0 deletions appcenter/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require('fs');
const path = require('path');

const projectDirectory = path.resolve(__dirname, '..', '..', '..');
const setupFileName = `.${path.sep}node_modules${path.sep}appcenter${path.sep}test${path.sep}appCenterMock.js`;

// 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 = [setupFileName];
} else if (packageJson.jest.setupFiles.indexOf(setupFileName) === -1) {
packageJson.jest.setupFiles.push(setupFileName);
}
fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson));
}
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()
}));