-
Notifications
You must be signed in to change notification settings - Fork 135
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 10 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 |
---|---|---|
@@ -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; |
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; |
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,37 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const projectDirectory = path.resolve(__dirname, '..', '..', '..'); | ||
const mockFileContent = ` | ||
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); | ||
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. 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:
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. 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. @bmourat let's adopt the solution described here, I would vote for the first approach:
|
||
} |
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'); | ||
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 |
||
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); | ||
} |
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.
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.