-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): updates libraries for React to newest versions (#1584)
- Loading branch information
Showing
14 changed files
with
322 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{ | ||
"schematics": {} | ||
"schematics": { | ||
"update-8.3.0": { | ||
"version": "8.3.0-beta.1", | ||
"description": "Update React libraries", | ||
"factory": "./src/migrations/update-8-3-0/update-8-3-0" | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
packages/react/src/migrations/update-8-3-0/update-8-3-0.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Tree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; | ||
import { updateJsonInTree, readJsonInTree } from '@nrwl/workspace'; | ||
|
||
import * as path from 'path'; | ||
import { stripIndents } from '@angular-devkit/core/src/utils/literals'; | ||
|
||
describe('Update 8-0-0', () => { | ||
let tree: Tree; | ||
let schematicRunner: SchematicTestRunner; | ||
|
||
beforeEach(async () => { | ||
tree = Tree.empty(); | ||
schematicRunner = new SchematicTestRunner( | ||
'@nrwl/react', | ||
path.join(__dirname, '../../../migrations.json') | ||
); | ||
tree = await schematicRunner | ||
.callRule( | ||
updateJsonInTree('package.json', () => ({ | ||
dependencies: { | ||
react: '16.8.1', | ||
'react-dom': '16.8.1', | ||
'react-router-dom': '5.0.0', | ||
'styled-components': '4.0.0' | ||
}, | ||
devDependencies: { | ||
'@types/styled-components': '4.0.0', | ||
'react-testing-library': '5.0.0', | ||
'@types/react': '16.8.0', | ||
'@types/react-dom': '16.8.0' | ||
} | ||
})), | ||
tree | ||
) | ||
.toPromise(); | ||
}); | ||
|
||
describe('imports', () => { | ||
it(`should be migrate 'react-testing-library' to '@testing-library/react`, async () => { | ||
tree.create( | ||
'Hello.spec.tsx', | ||
` | ||
import Hello from './Hello'; | ||
import React from 'react'; | ||
import { render } from 'react-testing-library'; | ||
` | ||
); | ||
|
||
tree.create( | ||
'foo.spec.ts', | ||
` | ||
import { render } from 'react-testing-library'; | ||
` | ||
); | ||
|
||
tree = await schematicRunner | ||
.runSchematicAsync('update-8.3.0', {}, tree) | ||
.toPromise(); | ||
|
||
expect(tree.read('Hello.spec.tsx').toString('utf-8')) | ||
.toContain(stripIndents` | ||
import Hello from './Hello'; | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
`); | ||
|
||
expect(tree.read('foo.spec.ts').toString('utf-8')).toContain(stripIndents` | ||
import { render } from '@testing-library/react'; | ||
`); | ||
}); | ||
}); | ||
|
||
describe('dependencies', () => { | ||
it('should update dependencies', async () => { | ||
tree = await schematicRunner | ||
.runSchematicAsync('update-8.3.0', {}, tree) | ||
.toPromise(); | ||
|
||
const result = readJsonInTree(tree, 'package.json'); | ||
|
||
expect(result).toEqual( | ||
expect.objectContaining({ | ||
dependencies: { | ||
react: '16.8.6', | ||
'react-dom': '16.8.6', | ||
'react-router-dom': '5.0.1', | ||
'styled-components': '4.3.2' | ||
}, | ||
devDependencies: { | ||
'@testing-library/react': '8.0.5', | ||
'@types/react': '16.8.23', | ||
'@types/react-dom': '16.8.23', | ||
'@types/styled-components': '4.1.18' | ||
} | ||
}) | ||
); | ||
|
||
expect(result.devDependencies['react-testing-library']).not.toBeDefined(); | ||
}); | ||
}); | ||
}); |
140 changes: 140 additions & 0 deletions
140
packages/react/src/migrations/update-8-3-0/update-8-3-0.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { | ||
chain, | ||
Rule, | ||
SchematicContext, | ||
Tree | ||
} from '@angular-devkit/schematics'; | ||
import { stripIndents } from '@angular-devkit/core/src/utils/literals'; | ||
import { | ||
formatFiles, | ||
insert, | ||
readJsonInTree, | ||
updateJsonInTree, | ||
updatePackageJsonDependencies | ||
} from '@nrwl/workspace'; | ||
import { | ||
createSourceFile, | ||
isImportDeclaration, | ||
isStringLiteral, | ||
ScriptTarget | ||
} from 'typescript'; | ||
import { ReplaceChange } from '@nrwl/workspace/src/utils/ast-utils'; | ||
import { relative } from 'path'; | ||
import { testingLibraryVersion } from '../../utils/versions'; | ||
|
||
const ignore = require('ignore'); | ||
|
||
export default function update(): Rule { | ||
return chain([ | ||
displayInformation, | ||
updateDependencies, | ||
updateImports, | ||
formatFiles() | ||
]); | ||
} | ||
|
||
function displayInformation(host: Tree, context: SchematicContext) { | ||
context.logger.info(stripIndents` | ||
React Testing Library has been repackaged as \`@testing-library/react\` as of version 8. | ||
We are replacing your \`react-testing-library\` imports with \`@testing-library/react\`. | ||
See: https://github.com/testing-library/react-testing-library/releases/tag/v8.0.0 | ||
`); | ||
} | ||
|
||
function updateDependencies(tree: Tree) { | ||
const removeOld = updateJsonInTree( | ||
'package.json', | ||
(json, context: SchematicContext) => { | ||
json.dependencies = json.dependencies || {}; | ||
json.devDependencies = json.devDependencies || {}; | ||
// Just in case user installed it to dependencies instead of devDependencies. | ||
delete json.dependencies['react-testing-library']; | ||
delete json.devDependencies['react-testing-library']; | ||
context.logger.info(`Removing \`react-testing-library\` as a dependency`); | ||
return json; | ||
} | ||
); | ||
|
||
const packageJson = readJsonInTree(tree, '/package.json'); | ||
|
||
const candidates = { | ||
react: '16.8.6', | ||
'react-dom': '16.8.6', | ||
'react-router-dom': '5.0.1', | ||
'@types/react': '16.8.23', | ||
'@types/react-dom': '16.8.23', | ||
'styled-components': '4.3.2', | ||
'@types/styled-components': '4.1.18', | ||
'@emotion/styled': '10.0.14' | ||
}; | ||
|
||
const updated = Object.entries(candidates).reduce( | ||
(acc, [m, v]) => { | ||
if (packageJson.dependencies[m]) { | ||
acc.dependencies[m] = v; | ||
} else if (packageJson.devDependencies[m]) { | ||
acc.devDependencies[m] = v; | ||
} | ||
return acc; | ||
}, | ||
{ | ||
dependencies: {}, | ||
devDependencies: { | ||
'@testing-library/react': testingLibraryVersion | ||
} | ||
} | ||
); | ||
|
||
const addNew = updatePackageJsonDependencies( | ||
updated.dependencies, | ||
updated.devDependencies | ||
); | ||
|
||
return chain([removeOld, addNew]); | ||
} | ||
|
||
function updateImports(host: Tree) { | ||
let ig = ignore().add(['*', '!*.ts', '!*.tsx']); // include only .tsx? files | ||
|
||
if (host.exists('.gitignore')) { | ||
ig = ig.add(host.read('.gitignore').toString()); | ||
} | ||
|
||
host.visit(path => { | ||
if (ig.ignores(relative('/', path))) { | ||
return; | ||
} | ||
|
||
const sourceFile = createSourceFile( | ||
path, | ||
host.read(path).toString(), | ||
ScriptTarget.Latest, | ||
true | ||
); | ||
const changes = []; | ||
sourceFile.statements.forEach(statement => { | ||
if ( | ||
isImportDeclaration(statement) && | ||
isStringLiteral(statement.moduleSpecifier) | ||
) { | ||
const nodeText = statement.moduleSpecifier.getText(sourceFile); | ||
const modulePath = statement.moduleSpecifier | ||
.getText(sourceFile) | ||
.substr(1, nodeText.length - 2); | ||
if (modulePath === 'react-testing-library') { | ||
changes.push( | ||
new ReplaceChange( | ||
path, | ||
statement.moduleSpecifier.getStart(sourceFile), | ||
nodeText, | ||
`'@testing-library/react'` | ||
) | ||
); | ||
} | ||
} | ||
}); | ||
insert(host, path, changes); | ||
}); | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/react/src/schematics/application/files/app/src/app/__fileName__.spec.tsx__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/react/src/schematics/component/files/__name__/__fileName__.spec.tsx__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
export const nxVersion = '*'; | ||
export const frameworkVersion = '16.8.3'; | ||
export const typesVersion = '16.8.4'; | ||
export const styledComponentVersion = '4.2.0'; | ||
export const styledComponentTypesVersion = '4.1.15'; | ||
export const emotionVersion = '10.0.10'; | ||
export const domTypesVersion = '16.8.2'; | ||
export const reactRouterVersion = '5.0.0'; | ||
export const testingLibraryVersion = '6.0.0'; | ||
export const frameworkVersion = '16.8.6'; | ||
export const typesVersion = '16.8.23'; | ||
export const styledComponentsVersion = '4.3.2'; | ||
export const styledComponentsTypesVersion = '4.1.18'; | ||
export const emotionVersion = '10.0.14'; | ||
export const domTypesVersion = '16.8.23'; | ||
export const reactRouterVersion = '5.0.1'; | ||
export const testingLibraryVersion = '8.0.5'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.