Skip to content

Commit

Permalink
Adding jest and jest babel preset to the react-native init command
Browse files Browse the repository at this point in the history
Summary:
Adding jest and its presets to the react-native init command

**Test plan (required)**
run react-native init foo (using `npm link` to use the local `react-native` version)
inside foo there are now a .babelrc file and the package.json is set up as described by
https://facebook.github.io/jest/docs/tutorial-react-native.html#setup
Closes #9719

Differential Revision: D3843037

Pulled By: bestander

fbshipit-source-id: 004e27ebd3f257a202ed43f378d6fe6cc23ced52
  • Loading branch information
kentaromiura authored and Facebook Github Bot 7 committed Sep 10, 2016
1 parent d6fe78f commit 8689b0f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 4 deletions.
43 changes: 42 additions & 1 deletion local-cli/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
*/
'use strict';

var fs = require('fs');
var path = require('path');
var yeoman = require('yeoman-generator');
var utils = require('../generator-utils');

module.exports = yeoman.generators.NamedBase.extend({
constructor: function() {
yeoman.generators.NamedBase.apply(this, arguments);

this.option('skip-ios', {
desc: 'Skip generating iOS files',
type: Boolean,
Expand All @@ -26,6 +26,11 @@ module.exports = yeoman.generators.NamedBase.extend({
type: Boolean,
defaults: false
});
this.option('skip-jest', {
desc: 'Skip installing Jest',
type: Boolean,
defaults: false
});
this.option('upgrade', {
desc: 'Specify an upgrade',
type: Boolean,
Expand Down Expand Up @@ -108,5 +113,41 @@ module.exports = yeoman.generators.NamedBase.extend({
}

this.npmInstall(`react@${reactVersion}`, { '--save': true, '--save-exact': true });
if (!this.options['skip-jest']) {
this.npmInstall(`jest babel-jest jest-react-native babel-preset-react-native react-test-renderer@${reactVersion}`.split(' '), {
saveDev: true,
'--save-exact': true
});
fs.writeFileSync(
path.join(
this.destinationRoot(),
'.babelrc'
),
'{\n"presets": ["react-native"]\n}'
);

this.fs.copy(
this.templatePath('__tests__'),
this.destinationPath('__tests__'),
{
nodir: false
}
);

var packageJSONPath = path.join(
this.destinationRoot(),
'package.json'
);
var packageJSON = JSON.parse(
fs.readFileSync(
packageJSONPath
)
);
packageJSON.scripts.test = 'jest';
packageJSON.jest = {
preset: 'jest-react-native'
};
fs.writeFileSync(packageJSONPath, JSON.stringify(packageJSON, null, '\t'));
}
}
});
12 changes: 12 additions & 0 deletions local-cli/generator/templates/__tests__/index.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'react-native';
import React from 'react';
import Index from '../index.android.js';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', () => {
const tree = renderer.create(
<Index />
);
});
12 changes: 12 additions & 0 deletions local-cli/generator/templates/__tests__/index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'react-native';
import React from 'react';
import Index from '../index.ios.js';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', () => {
const tree = renderer.create(
<Index />
);
});
2 changes: 1 addition & 1 deletion local-cli/generator/templates/index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
View
} from 'react-native';

class <%= name %> extends Component {
export default class <%= name %> extends Component {
render() {
return (
<View style={styles.container}>
Expand Down
2 changes: 1 addition & 1 deletion local-cli/generator/templates/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
View
} from 'react-native';

class <%= name %> extends Component {
export default class <%= name %> extends Component {
render() {
return (
<View style={styles.container}>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"^[./a-zA-Z0-9$_-]+\\.png$": "RelativeImageStub"
},
"testPathIgnorePatterns": [
"/node_modules/"
"/node_modules/",
"local-cli/generator/templates/"
],
"haste": {
"defaultPlatform": "ios",
Expand Down
5 changes: 5 additions & 0 deletions scripts/run-ci-e2e-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ try {
exitCode = 1;
throw Error(exitCode);
}
if (exec(`npm test`).code) {
echo('Jest test failure');
exitCode = 1;
throw Error(exitCode);
}
}
exitCode = 0;

Expand Down

0 comments on commit 8689b0f

Please sign in to comment.