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

Create empty mocks for jest #303

merged 19 commits into from
May 19, 2018

Conversation

bmourat
Copy link
Contributor

@bmourat bmourat commented May 10, 2018

Fixes second reason for #298. First was fixed here #301.
We could add this logic to postlink script but I think it makes more sense doing it after installation since it is not part of the linking process.

@bmourat bmourat requested review from dhei and guperrot May 10, 2018 11:44
@bmourat bmourat changed the title Create empty mocks for jest WIP Create empty mocks for jest May 10, 2018
@bmourat bmourat changed the title WIP Create empty mocks for jest Create empty mocks for jest May 10, 2018
@dhei dhei self-assigned this May 11, 2018
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')

}

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?

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!

@bmourat
Copy link
Contributor Author

bmourat commented May 14, 2018

@ajwhite @dhei
I think I found more elegant and less breakable solution. I decided not to mock NativeModules since there could be conflicts if a user has already defined mock for this module. I'm just creating mock files for each module in mocks directory (the directory where jest expects to find mocks). Thus, there is no need to modify package.json file and user always can tweak our mocks as he wishes.

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.

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.

@guperrot guperrot mentioned this pull request May 17, 2018
if (!fs.existsSync(mocksDirectory)) {
fs.mkdirSync(mocksDirectory);
}
fs.writeFileSync(`${mocksDirectory}/${mockFileName}`, mockFileContent);
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.


// 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.

@guperrot
Copy link
Member

guperrot commented May 19, 2018

@bmourat Works well for npm, however it failed when I tested with yarn. The cause is yarnpkg/yarn#853 so will need to document the manual steps for Yarn users...

@guperrot guperrot merged commit 9196d04 into develop May 19, 2018
@guperrot guperrot deleted the fix/create-mocks-for-jest branch May 19, 2018 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants