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

const projectDirectory = path.resolve(`${__dirname}${path.sep}..${path.sep}..${path.sep}..`);
const testDirectory = path.join(projectDirectory, 'test');
Copy link

@atticoos atticoos May 11, 2018

Choose a reason for hiding this comment

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

This is overall a really interesting solution, and it'd be wonderful if there was an established standard in providing mocks to dependents.

Anyway, this assume's the dependent's directory structure, and overall doesn't feel right to "inject" source files into a dependent, versus link to them

What if the mocks lived in a file here, and you just link to that in projectJson.jest.setupFiles?

Eg:

packageJson.jest.setupFiles.push('node_modules/appcenter-crashes/jest/mocks.js')

const setupFileName = 'setupAppCenter.js';
const packageJsonFile = path.join(`${projectDirectory}`, 'package.json');

// Update project.json
let packageJsonContent;
try {
packageJsonContent = fs.readFileSync(packageJsonFile, 'utf8');
} catch (e) {
console.log('Could not read package.json file');
return;
}
const projectJson = JSON.parse(packageJsonContent);
Copy link
Member

Choose a reason for hiding this comment

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

nit - is there a reason to name this variable "projectJson" not "packageJson"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just a typo, thanks!

if (Object.prototype.hasOwnProperty.call(projectJson, 'jest')) {
const setupFileNameValue = `.${path.sep}test${path.sep}${setupFileName}`;
if (projectJson.jest.setupFiles === undefined) {
projectJson.jest.setupFiles = [setupFileNameValue];
} else if (projectJson.jest.setupFiles.indexOf(setupFileNameValue) === -1) {
projectJson.jest.setupFiles.push(setupFileNameValue);
}
fs.writeFileSync(packageJsonFile, JSON.stringify(projectJson));
}

// Create setup mock file for Jest
if (!fs.existsSync(testDirectory)) {
fs.mkdirSync(testDirectory);
}

fs.writeFileSync(`${testDirectory}/${setupFileName}`, `
jest.mock('NativeModules', () => ({

Choose a reason for hiding this comment

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

How does jest.mock handle the scenario where the user has already defined a mock for NativeModules? Does this override/conflict with that? Or simply merge them?

AppCenterReactNativeCrashes: {
generateTestCrash: jest.fn(),
hasCrashedInLastSession: jest.fn(),
lastSessionCrashReport: jest.fn(),
isEnabled: jest.fn(),
setEnabled: jest.fn(),
notifyUserConfirmation: jest.fn(),
setListener: jest.fn()
},
AppCenterReactNativePush: {
setEnabled: jest.fn(),
isEnabled: jest.fn(),
setListener: jest.fn()
}
}));
`);
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"
}
}
50 changes: 50 additions & 0 deletions appcenter-push/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fs = require('fs');
const path = require('path');

const projectDirectory = path.resolve(`${__dirname}${path.sep}..${path.sep}..${path.sep}..`);
const testDirectory = path.join(projectDirectory, 'test');
const setupFileName = 'setupAppCenter.js';
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.


// Update project.json
let packageJsonContent;
try {
packageJsonContent = fs.readFileSync(packageJsonFile, 'utf8');
} catch (e) {
console.log('Could not read package.json file');
return;
}
const projectJson = JSON.parse(packageJsonContent);
if (Object.prototype.hasOwnProperty.call(projectJson, 'jest')) {
const setupFileNameValue = `.${path.sep}test${path.sep}${setupFileName}`;
if (projectJson.jest.setupFiles === undefined) {
projectJson.jest.setupFiles = [setupFileNameValue];
} else if (projectJson.jest.setupFiles.indexOf(setupFileNameValue) === -1) {
projectJson.jest.setupFiles.push(setupFileNameValue);
}
fs.writeFileSync(packageJsonFile, JSON.stringify(projectJson));
}

// Create setup mock file for Jest
if (!fs.existsSync(testDirectory)) {
fs.mkdirSync(testDirectory);
}

fs.writeFileSync(`${testDirectory}/${setupFileName}`, `
jest.mock('NativeModules', () => ({
AppCenterReactNativeCrashes: {
generateTestCrash: jest.fn(),
hasCrashedInLastSession: jest.fn(),
lastSessionCrashReport: jest.fn(),
isEnabled: jest.fn(),
setEnabled: jest.fn(),
notifyUserConfirmation: jest.fn(),
setListener: jest.fn()
},
AppCenterReactNativePush: {
setEnabled: jest.fn(),
isEnabled: jest.fn(),
setListener: jest.fn()
}
}));
`);