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

Commit

Permalink
chore(ionic-react): create migration for @testing-library/cypress (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
devinshoemaker authored Sep 18, 2020
1 parent 2d228ec commit e28c728
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 79 deletions.
27 changes: 7 additions & 20 deletions packages/ionic-react/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@
"description": "Upgrade Ionic to 5.2.3",
"factory": "./src/migrations/update-3-0-1/upgrade-ionic-3-0-1"
},
"upgrade-ionic-3.1.0": {
"update-3.1.0": {
"version": "3.1.0",
"description": "Upgrade Ionic to 5.3.1",
"factory": "./src/migrations/update-3-1-0/upgrade-ionic-3-1-0"
},
"update-3.2.0": {
"version": "3.2.0",
"description": "Update libraries",
"factory": "./src/migrations/update-3-2-0/update-3-2-0"
"factory": "./src/migrations/update-3-1-0/update-3-1-0"
},
"update-testing-library-cypress-tsconfig-types-3.1.0": {
"version": "3.1.0",
"description": "Update @testing-library/cypress tsconfig types",
"factory": "./src/migrations/update-3-1-0/update-testing-library-cypress-tsconfig-types-3-1-0"
}
},
"packageJsonUpdates": {
Expand Down Expand Up @@ -145,19 +145,6 @@
},
"3.1.0": {
"version": "3.1.0",
"packages": {
"@ionic/react": {
"version": "5.3.1",
"alwaysAddToPackageJson": false
},
"@ionic/react-router": {
"version": "5.3.1",
"alwaysAddToPackageJson": false
}
}
},
"3.2.0": {
"version": "3.2.0",
"packages": {
"@ionic/react": {
"version": "5.3.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { readJsonInTree, serializeJson } from '@nrwl/workspace';
import { createEmptyWorkspace } from '@nrwl/workspace/testing';
import * as path from 'path';

describe('Update 3.2.0', () => {
describe('Update 3.1.0', () => {
let initialTree: Tree;
let schematicRunner: SchematicTestRunner;

Expand Down Expand Up @@ -34,7 +34,7 @@ describe('Update 3.2.0', () => {

it(`should upgrade @nxtend/capacitor`, async () => {
const result = await schematicRunner
.runSchematicAsync('update-3.2.0', {}, initialTree)
.runSchematicAsync('update-3.1.0', {}, initialTree)
.toPromise();

const { dependencies } = readJsonInTree(result, '/package.json');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
SchematicContext,
Tree,
} from '@angular-devkit/schematics';
import { readJsonInTree, updatePackagesInPackageJson } from '@nrwl/workspace';
import {
formatFiles,
readJsonInTree,
updatePackagesInPackageJson,
} from '@nrwl/workspace';
import * as path from 'path';

function displayInformation(host: Tree, context: SchematicContext) {
Expand All @@ -24,7 +28,8 @@ export default function update(): Rule {
displayInformation,
updatePackagesInPackageJson(
path.join(__dirname, '../../../', 'migrations.json'),
'3.2.0'
'3.1.0'
),
formatFiles(),
]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { Tree } from '@angular-devkit/schematics';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import { readJsonInTree, serializeJson } from '@nrwl/workspace';
import { createEmptyWorkspace } from '@nrwl/workspace/testing';
import * as path from 'path';

describe('Update 3.1.0', () => {
let initialTree: Tree;
let schematicRunner: SchematicTestRunner;

beforeEach(async () => {
initialTree = createEmptyWorkspace(Tree.empty());

schematicRunner = new SchematicTestRunner(
'@nxtend/ionic-react',
path.join(__dirname, '../../../migrations.json')
);

initialTree.overwrite(
'package.json',
serializeJson({
devDependencies: {
'@nxtend/ionic-react': '0.0.0',
},
})
);

initialTree.overwrite(
'workspace.json',
serializeJson({
projects: {
'app-one': {
root: 'apps/app-one',
architect: {
build: {
options: {
webpackConfig: '@nxtend/ionic-react/plugins/webpack',
},
},
},
},
'app-one-e2e': {
root: 'apps/app-one-e2e',
architect: {},
},
},
})
);

initialTree.create(
'apps/app-one-e2e/tsconfig.json',
serializeJson({
extends: '../../tsconfig.base.json',
files: [],
include: [],
references: [
{
path: './tsconfig.e2e.json',
},
],
compilerOptions: {
types: ['@types/testing-library__cypress'],
},
})
);

initialTree.create(
'apps/app-one-e2e/tsconfig.e2e.json',
serializeJson({
extends: './tsconfig.json',
compilerOptions: {
sourceMap: false,
outDir: '../../dist/out-tsc',
allowJs: true,
types: ['cypress', 'node'],
},
include: ['src/**/*.ts', 'src/**/*.js'],
})
);
});

it(`should @testing-library/cypress tsconfig types`, async () => {
const result = await schematicRunner
.runSchematicAsync(
'update-testing-library-cypress-tsconfig-types-3.1.0',
{},
initialTree
)
.toPromise();

const appOneTsconfig = readJsonInTree(
result,
'apps/app-one-e2e/tsconfig.json'
);
const appOneTsconfigE2e = readJsonInTree(
result,
'apps/app-one-e2e/tsconfig.e2e.json'
);

expect(appOneTsconfig.compilerOptions.types).toEqual([]);
expect(appOneTsconfigE2e.compilerOptions.types).toContain(
'@testing-library/cypress'
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { chain, Rule, Tree } from '@angular-devkit/schematics';
import { formatFiles, readJsonInTree, updateJsonInTree } from '@nrwl/workspace';
import * as path from 'path';

function getTsconfigFilePaths(
host: Tree
): { tsconfigFilePaths: string[]; tsconfigE2eFilePaths: string[] } {
const tsconfigFilePaths = [];
const tsconfigE2eFilePaths = [];

const workspaceJson = readJsonInTree(host, 'workspace.json');
Object.values<any>(workspaceJson.projects).forEach((project) => {
Object.values<any>(project.architect).forEach((target) => {
if (
target.options?.webpackConfig !== '@nxtend/ionic-react/plugins/webpack'
) {
return;
}

const e2eProjectName = Object.keys(workspaceJson.projects).find(
(key) => workspaceJson.projects[key] === project
);

const e2eProject = workspaceJson.projects[`${e2eProjectName}-e2e`];

if (
host.exists(`${e2eProject.root}/tsconfig.e2e.json`) &&
host.exists(`${e2eProject.root}/tsconfig.json`)
) {
tsconfigFilePaths.push(path.join(e2eProject.root, 'tsconfig.json'));
tsconfigE2eFilePaths.push(`${e2eProject.root}/tsconfig.e2e.json`);
}
});
});

return { tsconfigFilePaths, tsconfigE2eFilePaths };
}

function updateTsconfigFilePaths(tsconfigFilePaths: string[]): Rule[] {
const rules: Rule[] = [];

tsconfigFilePaths.forEach((tsconfigFilePath) => {
rules.push(
updateJsonInTree(tsconfigFilePath, (json) => {
if (json.compilerOptions?.types) {
const index = json.compilerOptions.types.indexOf(
'@types/testing-library__cypress'
);

if (index !== -1) {
json.compilerOptions.types.splice(index, 1);
}
}

return json;
})
);
});

return rules;
}

function updateTsconfigE2eFilePaths(tsconfigE2eFilePaths: string[]): Rule[] {
const rules: Rule[] = [];

tsconfigE2eFilePaths.forEach((tsconfigFilePath) => {
rules.push(
updateJsonInTree(tsconfigFilePath, (json) => {
if (!json.compilerOptions) {
json.compilerOptions = {};
}

if (!json.compilerOptions.types) {
json.compilerOptions.types = [];
}

json.compilerOptions.types.push('@testing-library/cypress');

return json;
})
);
});

return rules;
}

function updateCypressTsconfigTypes(): Rule {
return (host: Tree) => {
let rules: Rule[] = [];
const { devDependencies } = readJsonInTree(host, 'package.json');

if (devDependencies && devDependencies['@nxtend/ionic-react']) {
const { tsconfigFilePaths, tsconfigE2eFilePaths } = getTsconfigFilePaths(
host
);

rules = updateTsconfigFilePaths(tsconfigFilePaths);
rules = [...rules, ...updateTsconfigE2eFilePaths(tsconfigE2eFilePaths)];
}

return chain(rules);
};
}

export default function update(): Rule {
return chain([updateCypressTsconfigTypes(), formatFiles()]);
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function addDependencies(): Rule {
}

export function configureTestingLibraryTypes(options: NormalizedSchema) {
return updateJsonInTree(options.e2eRoot + '/tsconfig.json', (json) => {
return updateJsonInTree(options.e2eRoot + '/tsconfig.e2e.json', (json) => {
if (!json.compilerOptions) {
json.compilerOptions = {};
}
Expand All @@ -22,7 +22,7 @@ export function configureTestingLibraryTypes(options: NormalizedSchema) {
json.compilerOptions.types = [];
}

json.compilerOptions.types.push('@types/testing-library__cypress');
json.compilerOptions.types.push('@testing-library/cypress');
return json;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,11 @@ describe('application', () => {

const tsconfigJson = readJsonInTree(
tree,
`${projectRoot}-e2e/tsconfig.json`
`${projectRoot}-e2e/tsconfig.e2e.json`
);
expect(
tsconfigJson.compilerOptions.types.includes(
'@types/testing-library__cypress'
'@testing-library/cypress'
)
).toBeTruthy();
});
Expand Down

0 comments on commit e28c728

Please sign in to comment.