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 10 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;
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-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"
}
}
37 changes: 37 additions & 0 deletions appcenter-crashes/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require('fs');
const path = require('path');

const projectDirectory = path.resolve(__dirname, '..', '..', '..');
const mockFileContent = `
Copy link
Member

Choose a reason for hiding this comment

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

nit - If you move the backtick(`) to the beginning of line 6, then the generated mocks/appcenter-crashes.js file won't have a empty new line at the beginning of the file.

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;
`;

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

// Create mock file for Jest
const mocksDirectory = `${projectDirectory}/__mocks__`;
const mockFileName = 'appcenter-crashes.js';
if (!fs.existsSync(`${mocksDirectory}/${mockFileName}`)) {
if (!fs.existsSync(mocksDirectory)) {
fs.mkdirSync(mocksDirectory);
}
fs.writeFileSync(`${mocksDirectory}/${mockFileName}`, mockFileContent);
Copy link

@atticoos atticoos May 15, 2018

Choose a reason for hiding this comment

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

It still feels wrong to inject source code into a dependent's project. Let's also recognize not every project will have the assumed file structure.

It would be more natural to define the mocks in the appcenter module (eg: appcenter-crashes/jest/mocks.js) and either:

  • Automatically add the file path to packageJson.jest.setupFiles during postinstall
  • Allow the user to manually add the file path to packageJson.jest.setupFiles and document this process in the README

Example:

// package.json
{
  ...
  "jest": {
    "setupFiles": [
      "__test__/mocks/*.js", 
      // simply points to the mocks in the appcenter module
      "node_modules/appcenter-crashes/mocks.js"
    ]
  }
}

But AppCenter should avoid injecting mocks into the dependent project. Just have the dependent point to the files located in app center's module.

Copy link
Member

Choose a reason for hiding this comment

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

@bmourat let's adopt the solution described here, I would vote for the first approach:

Automatically add the file path to packageJson.jest.setupFiles during postinstall.

}
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"
}
}
33 changes: 33 additions & 0 deletions appcenter-push/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fs = require('fs');
const path = require('path');

const projectDirectory = path.resolve(__dirname, '..', '..', '..');
const mockFileContent = `
const Push = jest.mock('appcenter-push');
Push.isEnabled = jest.fn();
Push.setEnabled = jest.fn();
Push.setListener = jest.fn();
export default Push;
`;

// Check if package.json has jest as dependency
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 {
const packageJson = JSON.parse(fs.readFileSync(packageJsonFile, 'utf8'));
if (!Object.prototype.hasOwnProperty.call(packageJson.devDependencies, 'jest')) {
return;
}
} catch (e) {
console.log('Could not read package.json file');
return;
}

// Create mock file for Jest
const mocksDirectory = `${projectDirectory}/__mocks__`;
const mockFileName = 'appcenter-push.js';
if (!fs.existsSync(`${mocksDirectory}/${mockFileName}`)) {
if (!fs.existsSync(mocksDirectory)) {
fs.mkdirSync(mocksDirectory);
}
fs.writeFileSync(`${mocksDirectory}/${mockFileName}`, mockFileContent);
}