Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Commit

Permalink
Commit to check tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sipmann committed Jun 18, 2020
1 parent 3921054 commit ab682ad
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
43 changes: 38 additions & 5 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ module.exports = class extends Generator {
},
{
name: 'Redux'
},
{
name: 'Jest'
}
]
}])
Expand All @@ -45,6 +48,7 @@ module.exports = class extends Generator {

this.reactRouter = false;
this.nodeSass = false;
this.jest = false;

if (answers.extras) {
answers.extras.forEach(el => {
Expand All @@ -56,27 +60,36 @@ module.exports = class extends Generator {
this.typescript = true;
else if (el === 'Redux')
this.redux = true;
else if (el === 'Jest')
this.jest = true;
});
}
});
}

install() {
let typescriptPackages = [];
this.npmInstall(['react', 'react-dom']);

//TODO refactor to a better (prettier) way
if (this.reactRouter) {
typescriptPackages.push('@types/react-router-dom');
this.npmInstall(['react-router-dom']);
}
if (this.nodeSass) {
this.npmInstall(['node-sass'], { 'dev': true });
this.npmInstall(['node-sass'], { 'save-dev': true });
}
if (this.jest) {
typescriptPackages.push(...['@types/jest', 'ts-jest']);
this.npmInstall(['jest', '@testing-library/react', '@testing-library/jest-dom'], { 'save-dev': true });
}
if (this.typescript) {
//this.npmInstall(['@types/react','@types/react-dom'], { 'dev': true });
this.npmInstall(['typescript', '@types/react','@types/react-dom', '@babel/preset-typescript', ...typescriptPackages], { 'save-dev': true });
}
if (this.redux) {
this.npmInstall(['redux', 'react-redux', 'redux-thunk']);
}
this.npmInstall(['parcel-bundler', 'babel-preset-env', 'babel-preset-react', 'babel-plugin-transform-object-rest-spread'], { 'dev': true });
this.npmInstall(['parcel-bundler', '@babel/preset-env', '@babel/preset-react', '@babel/plugin-proposal-object-rest-spread'], { 'save-dev': true });
}

writing() {
Expand Down Expand Up @@ -129,9 +142,29 @@ module.exports = class extends Generator {
{}
);

if (this.jest) {
const testPkgJson = {
scripts: {
test: 'jest',
"test:watch": "jest --watch"
}
};
this.fs.extendJSON(this.destinationPath('package.json'), testPkgJson);

this.fs.copyTpl(
this.templatePath('index.test.tsx'),
this.destinationPath('src/index.test.tsx')
);

this.fs.copyTpl(
this.templatePath('jest.config.js'),
this.destinationPath('jest.config.js')
);
}

this.fs.write('.babelrc', '{\n'+
' "plugins": ["transform-object-rest-spread"],\n'+
' "presets": ["env", "react"]\n'+
' "plugins": ["@babel/plugin-proposal-object-rest-spread"],\n'+
' "presets": ["@babel/env", "@babel/react", "@babel/preset-typescript"]\n'+
'}');
}
};
14 changes: 14 additions & 0 deletions generators/app/templates/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { StaticRouter as Router } from 'react-router-dom';
import { render, waitForElement, fireEvent } from '@testing-library/react'

import App from './index';

describe('Tests for App component', () => {
it('Should render without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<Router><App /></Router>, div);
ReactDOM.unmountComponentAtNode(div);
});
});
2 changes: 2 additions & 0 deletions generators/app/templates/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ class App extends React.Component<{}, {}> {
}

ReactDOM.render(<App />, document.getElementById('app'));

export default App;
4 changes: 4 additions & 0 deletions generators/app/templates/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "generator-parcel-react",
"version": "1.3.3",
"version": "1.4.0",
"description": "A simple react with parcel generator",
"main": "index.js",
"author": {
Expand All @@ -27,11 +27,11 @@
"yeoman-generator": "^4.1.0"
},
"devDependencies": {
"mocha": "^6.2.2",
"mocha": "^8.0.1",
"yeoman-assert": "^3.1.1",
"yeoman-test": "^1.9.1",
"mocha-lcov-reporter": "^1.3.0",
"coveralls": "^3.0.7",
"istanbul": "^0.3.17"
"istanbul": "^0.4.5"
}
}

0 comments on commit ab682ad

Please sign in to comment.