forked from nrwl/nx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testing): update to jest-preset-angular v8.0.0
Updates to jest-preset-angular to v8.0.0, includes migrations to fix any existing projects affected by the jest-preset-angular update. closes nrwl#1979
- Loading branch information
Showing
11 changed files
with
315 additions
and
16 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
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
155 changes: 155 additions & 0 deletions
155
packages/jest/src/migrations/update-8-8-0/update-8-8-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,155 @@ | ||
import { Tree } from '@angular-devkit/schematics'; | ||
import { readJsonInTree } from '@nrwl/workspace/src/utils/ast-utils'; | ||
import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; | ||
import * as path from 'path'; | ||
import { createEmptyWorkspace } from '@nrwl/workspace/testing'; | ||
import { serializeJson } from '@nrwl/workspace'; | ||
|
||
describe.only('Update 8.8.0', () => { | ||
let initialTree: Tree; | ||
let schematicRunner: SchematicTestRunner; | ||
|
||
beforeEach(async () => { | ||
initialTree = createEmptyWorkspace(Tree.empty()); | ||
|
||
schematicRunner = new SchematicTestRunner( | ||
'@nrwl/jest', | ||
path.join(__dirname, '../../../migrations.json') | ||
); | ||
|
||
initialTree.overwrite( | ||
'package.json', | ||
serializeJson({ devDependencies: { 'jest-preset-angular': '7.0.0' } }) | ||
); | ||
|
||
initialTree.overwrite( | ||
'workspace.json', | ||
serializeJson({ | ||
projects: { | ||
'angular-one': { | ||
root: 'apps/angular-one/', | ||
architect: { | ||
test: { | ||
builder: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'apps/angular-one/jest.config.js' | ||
} | ||
} | ||
} | ||
}, | ||
'angular-two': { | ||
root: 'apps/angular-two/', | ||
architect: { | ||
test: { | ||
builder: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'apps/angular-two/jest.config.js' | ||
} | ||
} | ||
} | ||
}, | ||
'non-angular-one': { | ||
root: 'apps/non-angular-one/', | ||
architect: { | ||
test: { | ||
builder: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'apps/non-angular-one/jest.config.js' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
); | ||
|
||
initialTree.create( | ||
'apps/angular-one/jest.config.js', | ||
`module.exports = { | ||
name: 'angular-one', | ||
preset: '../../jest.config.js', | ||
coverageDirectory: | ||
'../../coverage/apps/angular-one', | ||
snapshotSerializers: [ | ||
'jest-preset-angular/AngularSnapshotSerializer.js', | ||
'jest-preset-angular/HTMLCommentSerializer.js' | ||
] | ||
};` | ||
); | ||
|
||
initialTree.create( | ||
'apps/angular-two/jest.config.js', | ||
`module.exports = { | ||
name: 'angular-two', | ||
preset: '../../jest.config.js', | ||
coverageDirectory: | ||
'../../coverage/apps/angular-two', | ||
snapshotSerializers: [ | ||
'jest-preset-angular/AngularSnapshotSerializer.js', | ||
'jest-preset-angular/HTMLCommentSerializer.js' | ||
] | ||
};` | ||
); | ||
|
||
initialTree.create( | ||
'apps/non-angular-one/jest.config.js', | ||
`module.exports = { | ||
name: 'non-angular-one', | ||
preset: '../../jest.config.js', | ||
coverageDirectory: | ||
'../../coverage/apps/non-angular-one', | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'] | ||
};` | ||
); | ||
}); | ||
|
||
it('should update jest-preset-angular to 8.0.0', async () => { | ||
const result = await schematicRunner | ||
.runSchematicAsync('update-8.8.0', {}, initialTree) | ||
.toPromise(); | ||
|
||
const { devDependencies } = readJsonInTree(result, 'package.json'); | ||
expect(devDependencies['jest-preset-angular']).toEqual('8.0.0'); | ||
}); | ||
|
||
it(`it should add '/build' into jest-preset-angular snapshotSerializers in any jest.config.js where it exists`, async () => { | ||
const result = await schematicRunner | ||
.runSchematicAsync('update-8.8.0', {}, initialTree) | ||
.toPromise(); | ||
|
||
const updateJestAngularOne = result.readContent( | ||
'apps/angular-one/jest.config.js' | ||
); | ||
const updateJestAngularTwo = result.readContent( | ||
'apps/angular-two/jest.config.js' | ||
); | ||
const updateJestNonAngularOne = result.readContent( | ||
'apps/non-angular-one/jest.config.js' | ||
); | ||
|
||
expect(updateJestAngularOne).not.toContain( | ||
'jest-preset-angular/AngularSnapshotSerializer.js' | ||
); | ||
expect(updateJestAngularOne).not.toContain( | ||
'jest-preset-angular/HTMLCommentSerializer.js' | ||
); | ||
expect(updateJestAngularTwo).not.toContain( | ||
'jest-preset-angular/AngularSnapshotSerializer.js' | ||
); | ||
expect(updateJestAngularTwo).not.toContain( | ||
'jest-preset-angular/HTMLCommentSerializer.js' | ||
); | ||
expect(updateJestAngularOne).toContain( | ||
'jest-preset-angular/build/AngularSnapshotSerializer.js' | ||
); | ||
expect(updateJestAngularOne).toContain( | ||
'jest-preset-angular/build/HTMLCommentSerializer.js' | ||
); | ||
expect(updateJestAngularTwo).toContain( | ||
'jest-preset-angular/build/AngularSnapshotSerializer.js' | ||
); | ||
expect(updateJestAngularTwo).toContain( | ||
'jest-preset-angular/build/HTMLCommentSerializer.js' | ||
); | ||
}); | ||
}); |
119 changes: 119 additions & 0 deletions
119
packages/jest/src/migrations/update-8-8-0/update-8-8-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,119 @@ | ||
import { | ||
Tree, | ||
Rule, | ||
chain, | ||
SchematicContext | ||
} from '@angular-devkit/schematics'; | ||
import { readWorkspace, insert, formatFiles } from '@nrwl/workspace'; | ||
import * as ts from 'typescript'; | ||
import { | ||
ReplaceChange, | ||
updateJsonInTree, | ||
readJsonInTree, | ||
getSourceNodes | ||
} from '@nrwl/workspace/src/utils/ast-utils'; | ||
import { stripIndents } from '@angular-devkit/core/src/utils/literals'; | ||
|
||
export default function update(): Rule { | ||
return chain([ | ||
displayInformation, | ||
updateDependencies, | ||
updateJestConfigs, | ||
formatFiles() | ||
]); | ||
} | ||
|
||
function displayInformation(host: Tree, context: SchematicContext) { | ||
const config = readJsonInTree(host, 'package.json'); | ||
if (config.devDependencies && config.devDependencies['jest-preset-angular']) { | ||
context.logger.info(stripIndents` | ||
\`jest-preset-angular\` 8.0.0 has restructured folders, introducing breaking changes to | ||
jest.config.js files. | ||
We are updating snapshotSerializers in each Angular project to include appropriate paths. | ||
See: https://github.com/thymikee/jest-preset-angular/releases/tag/v8.0.0 | ||
`); | ||
} | ||
} | ||
|
||
const updateDependencies = updateJsonInTree('package.json', json => { | ||
json.devDependencies = json.devDependencies || {}; | ||
|
||
if (json.devDependencies['jest-preset-angular']) { | ||
json.devDependencies['jest-preset-angular'] = '8.0.0'; | ||
} | ||
|
||
return json; | ||
}); | ||
|
||
function updateJestConfigs(host: Tree) { | ||
const config = readJsonInTree(host, 'package.json'); | ||
|
||
if (config.devDependencies && config.devDependencies['jest-preset-angular']) { | ||
const workspaceConfig = readWorkspace(host); | ||
const jestConfigsToUpdate = []; | ||
|
||
Object.keys(workspaceConfig.projects).forEach(name => { | ||
const project = workspaceConfig.projects[name]; | ||
if ( | ||
project.architect && | ||
project.architect.test && | ||
project.architect.test.builder === '@nrwl/jest:jest' && | ||
project.architect.test.options && | ||
project.architect.test.options.jestConfig === | ||
project.root + 'jest.config.js' | ||
) { | ||
jestConfigsToUpdate.push(project.root + 'jest.config.js'); | ||
} | ||
}); | ||
|
||
jestConfigsToUpdate.forEach(configPath => { | ||
if (host.exists(configPath)) { | ||
const contents = host.read(configPath).toString(); | ||
const sourceFile = ts.createSourceFile( | ||
configPath, | ||
contents, | ||
ts.ScriptTarget.Latest | ||
); | ||
|
||
const changes: ReplaceChange[] = []; | ||
|
||
getSourceNodes(sourceFile).forEach(node => { | ||
if (node && ts.isStringLiteral(node)) { | ||
const nodeText = node.text; | ||
if ( | ||
nodeText === 'jest-preset-angular/AngularSnapshotSerializer.js' | ||
) { | ||
changes.push( | ||
new ReplaceChange( | ||
configPath, | ||
node.getStart(sourceFile) + 1, | ||
nodeText, | ||
'jest-preset-angular/build/AngularSnapshotSerializer.js' | ||
) | ||
); | ||
} | ||
|
||
if (nodeText === 'jest-preset-angular/HTMLCommentSerializer.js') { | ||
changes.push( | ||
new ReplaceChange( | ||
configPath, | ||
node.getStart(sourceFile) + 1, | ||
nodeText, | ||
'jest-preset-angular/build/HTMLCommentSerializer.js' | ||
) | ||
); | ||
} | ||
} | ||
}); | ||
|
||
insert( | ||
host, | ||
configPath, | ||
changes.sort((a, b) => (a.order > b.order ? -1 : 1)) | ||
); | ||
} | ||
}); | ||
} | ||
} |
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.