-
Notifications
You must be signed in to change notification settings - Fork 136
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
Changes from 6 commits
354c659
ba4efdf
54be94d
7cdca52
df4ee9b
980045d
16a29bd
5d62992
c56d4f0
ba562fc
12c0b91
bfd5a94
7431b3d
e38e7df
7cf3956
6222c6e
688e28e
fe3fdc3
91565da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
|
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" | ||
|
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'); | ||
|
||
// 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit - is there a reason to name this variable "projectJson" not "packageJson"? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', () => ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does |
||
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() | ||
} | ||
})); | ||
`); |
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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
// 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() | ||
} | ||
})); | ||
`); |
There was a problem hiding this comment.
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: