Permalink
Browse files

Adding jest and jest babel preset to the react-native init command

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...
1 parent d6fe78f commit 8689b0f21cff74fd6c667d67554c7eb3df5eef30 @kentaromiura kentaromiura committed with Facebook Github Bot 7 Sep 10, 2016
@@ -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,
@@ -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,
@@ -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'));
+ }
}
});
@@ -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 />
+ );
+});
@@ -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 />
+ );
+});
@@ -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}>
@@ -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}>
View
@@ -23,7 +23,8 @@
"^[./a-zA-Z0-9$_-]+\\.png$": "RelativeImageStub"
},
"testPathIgnorePatterns": [
- "/node_modules/"
+ "/node_modules/",
+ "local-cli/generator/templates/"
],
"haste": {
"defaultPlatform": "ios",
@@ -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;

0 comments on commit 8689b0f

Please sign in to comment.