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 https://github.com/facebook/react-native/pull/9719

Differential Revision: D3843037

Pulled By: bestander

fbshipit-source-id: 004e27ebd3f257a202ed43f378d6fe6cc23ced52
This commit is contained in:
kentaromiura 2016-09-09 17:32:20 -07:00 committed by Facebook Github Bot 7
parent d6fe78fc25
commit 8689b0f21c
7 changed files with 75 additions and 4 deletions

View File

@ -8,6 +8,7 @@
*/
'use strict';
var fs = require('fs');
var path = require('path');
var yeoman = require('yeoman-generator');
var utils = require('../generator-utils');
@ -15,7 +16,6 @@ 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'));
}
}
});

View File

@ -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 />
);
});

View File

@ -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 />
);
});

View File

@ -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 File

@ -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 File

@ -23,7 +23,8 @@
"^[./a-zA-Z0-9$_-]+\\.png$": "RelativeImageStub"
},
"testPathIgnorePatterns": [
"/node_modules/"
"/node_modules/",
"local-cli/generator/templates/"
],
"haste": {
"defaultPlatform": "ios",

View File

@ -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;