Skip to content

Commit 232c4b3

Browse files
authored
feat(tools): add conformance setup migration to migrate-converged-pkg generator (#27668)
1 parent fc13ed7 commit 232c4b3

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

tools/generators/migrate-converged-pkg/index.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
writeJson,
2020
WorkspaceConfiguration,
2121
ProjectConfiguration,
22+
joinPathFragments,
2223
} from '@nrwl/devkit';
2324

2425
import { PackageJson, TsConfig } from '../../types';
@@ -1256,6 +1257,63 @@ describe('migrate-converged-pkg generator', () => {
12561257
});
12571258
});
12581259

1260+
describe(`update conformance setup`, () => {
1261+
const conformanceSetup = stripIndents`
1262+
import { isConformant as baseIsConformant } from '@proj/react-conformance';
1263+
import type { IsConformantOptions, TestObject } from '@proj/react-conformance';
1264+
import griffelTests from '@proj/react-conformance-griffel';
1265+
1266+
export function isConformant<TProps = {}>(
1267+
testInfo: Omit<IsConformantOptions<TProps>, 'componentPath'> & { componentPath?: string },
1268+
) {
1269+
const defaultOptions: Partial<IsConformantOptions<TProps>> = {
1270+
componentPath: require.main?.filename.replace('.test', ''),
1271+
extraTests: griffelTests as TestObject<TProps>,
1272+
testOptions: {
1273+
'make-styles-overrides-win': {
1274+
callCount: 2,
1275+
},
1276+
// TODO: https://github.com/microsoft/fluentui/issues/19618
1277+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1278+
} as any,
1279+
};
1280+
1281+
baseIsConformant(defaultOptions, testInfo);
1282+
}
1283+
`;
1284+
it(`should use tsConfig conformance API`, async () => {
1285+
const projectConfig = readProjectConfiguration(tree, options.name);
1286+
const conformanceSetupPath = joinPathFragments(projectConfig.root as string, 'src/testing/isConformant.ts');
1287+
tree.write(conformanceSetupPath, conformanceSetup);
1288+
await generator(tree, options);
1289+
1290+
expect(tree.read(conformanceSetupPath, 'utf-8')).toMatchInlineSnapshot(`
1291+
"import { isConformant as baseIsConformant } from '@proj/react-conformance';
1292+
import type { IsConformantOptions, TestObject } from '@proj/react-conformance';
1293+
import griffelTests from '@proj/react-conformance-griffel';
1294+
1295+
export function isConformant<TProps = {}>(
1296+
testInfo: Omit<IsConformantOptions<TProps>, 'componentPath'> & { componentPath?: string },
1297+
) {
1298+
const defaultOptions: Partial<IsConformantOptions<TProps>> = {
1299+
tsConfig: { configName: 'tsconfig.spec.json' },
1300+
componentPath: require.main?.filename.replace('.test', ''),
1301+
extraTests: griffelTests as TestObject<TProps>,
1302+
testOptions: {
1303+
'make-styles-overrides-win': {
1304+
callCount: 2,
1305+
},
1306+
// TODO: https://github.com/microsoft/fluentui/issues/19618
1307+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1308+
} as any,
1309+
};
1310+
1311+
baseIsConformant(defaultOptions, testInfo);
1312+
}"
1313+
`);
1314+
});
1315+
});
1316+
12591317
describe(`--stats`, () => {
12601318
it(`should print project names and count of how many have been migrated`, async () => {
12611319
const loggerInfoSpy = jest.spyOn(logger, 'info');

tools/generators/migrate-converged-pkg/index.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ import {
1313
updateProjectConfiguration,
1414
serializeJson,
1515
offsetFromRoot,
16+
applyChangesToString,
17+
ChangeType,
1618
} from '@nrwl/devkit';
1719
import * as path from 'path';
1820
import * as os from 'os';
21+
import * as ts from 'typescript';
1922

2023
import { PackageJson, TsConfig } from '../../types';
2124
import {
@@ -144,6 +147,7 @@ function runMigrationOnProject(tree: Tree, schema: AssertedSchema, _userLog: Use
144147

145148
setupSwcConfig(tree, options);
146149
setupJustConfig(tree, options);
150+
updateConformanceSetup(tree, options);
147151
}
148152

149153
// ==== helpers ====
@@ -978,6 +982,52 @@ function updateLocalJestConfig(tree: Tree, options: NormalizedSchema) {
978982
return tree;
979983
}
980984

985+
function updateConformanceSetup(tree: Tree, options: NormalizedSchema) {
986+
if (!tree.exists(options.paths.conformanceSetup)) {
987+
logger.warn('no conformance setup present. skipping...');
988+
return;
989+
}
990+
991+
const conformanceSetupContent = tree.read(options.paths.conformanceSetup, 'utf-8') as string;
992+
const sourceFile = ts.createSourceFile('is-conformant.ts', conformanceSetupContent, ts.ScriptTarget.Latest, true);
993+
const addition = `\ntsConfig: { configName: 'tsconfig.spec.json' },`;
994+
995+
let start: number | undefined;
996+
getConfigObjectFirstPropertyIndex(sourceFile);
997+
998+
if (!start) {
999+
return;
1000+
}
1001+
1002+
const updatedContent = applyChangesToString(conformanceSetupContent, [
1003+
{
1004+
type: ChangeType.Insert,
1005+
index: start,
1006+
text: addition,
1007+
},
1008+
]);
1009+
1010+
tree.write(options.paths.conformanceSetup, updatedContent);
1011+
1012+
function getConfigObjectFirstPropertyIndex(node: ts.Node) {
1013+
if (ts.isVariableStatement(node)) {
1014+
const defaultOptionsVar = node.declarationList.declarations[0].name.getText();
1015+
if (defaultOptionsVar === 'defaultOptions') {
1016+
const initializer = node.declarationList.declarations[0].initializer;
1017+
if (initializer && ts.isObjectLiteralExpression(initializer)) {
1018+
const firstProp = initializer.properties[0];
1019+
start = firstProp.pos;
1020+
return;
1021+
}
1022+
}
1023+
}
1024+
1025+
ts.forEachChild(node, getConfigObjectFirstPropertyIndex);
1026+
}
1027+
1028+
return tree;
1029+
}
1030+
9811031
function updatedLocalTsConfig(tree: Tree, options: NormalizedSchema) {
9821032
const { configs } = createTsSolutionConfig(tree, options);
9831033

0 commit comments

Comments
 (0)