Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tools): add conformance setup migration to migrate-converged-pkg generator #27668

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions tools/generators/migrate-converged-pkg/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
writeJson,
WorkspaceConfiguration,
ProjectConfiguration,
joinPathFragments,
} from '@nrwl/devkit';

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

describe(`update conformance setup`, () => {
const conformanceSetup = stripIndents`
import { isConformant as baseIsConformant } from '@proj/react-conformance';
import type { IsConformantOptions, TestObject } from '@proj/react-conformance';
import griffelTests from '@proj/react-conformance-griffel';

export function isConformant<TProps = {}>(
testInfo: Omit<IsConformantOptions<TProps>, 'componentPath'> & { componentPath?: string },
) {
const defaultOptions: Partial<IsConformantOptions<TProps>> = {
componentPath: require.main?.filename.replace('.test', ''),
extraTests: griffelTests as TestObject<TProps>,
testOptions: {
'make-styles-overrides-win': {
callCount: 2,
},
// TODO: https://github.com/microsoft/fluentui/issues/19618
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
};

baseIsConformant(defaultOptions, testInfo);
}
`;
it(`should use tsConfig conformance API`, async () => {
const projectConfig = readProjectConfiguration(tree, options.name);
const conformanceSetupPath = joinPathFragments(projectConfig.root as string, 'src/testing/isConformant.ts');
tree.write(conformanceSetupPath, conformanceSetup);
await generator(tree, options);

expect(tree.read(conformanceSetupPath, 'utf-8')).toMatchInlineSnapshot(`
"import { isConformant as baseIsConformant } from '@proj/react-conformance';
import type { IsConformantOptions, TestObject } from '@proj/react-conformance';
import griffelTests from '@proj/react-conformance-griffel';

export function isConformant<TProps = {}>(
testInfo: Omit<IsConformantOptions<TProps>, 'componentPath'> & { componentPath?: string },
) {
const defaultOptions: Partial<IsConformantOptions<TProps>> = {
tsConfig: { configName: 'tsconfig.spec.json' },
Hotell marked this conversation as resolved.
Show resolved Hide resolved
componentPath: require.main?.filename.replace('.test', ''),
extraTests: griffelTests as TestObject<TProps>,
testOptions: {
'make-styles-overrides-win': {
callCount: 2,
},
// TODO: https://github.com/microsoft/fluentui/issues/19618
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
};

baseIsConformant(defaultOptions, testInfo);
}"
`);
});
});

describe(`--stats`, () => {
it(`should print project names and count of how many have been migrated`, async () => {
const loggerInfoSpy = jest.spyOn(logger, 'info');
Expand Down
50 changes: 50 additions & 0 deletions tools/generators/migrate-converged-pkg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import {
updateProjectConfiguration,
serializeJson,
offsetFromRoot,
applyChangesToString,
ChangeType,
} from '@nrwl/devkit';
import * as path from 'path';
import * as os from 'os';
import * as ts from 'typescript';

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

setupSwcConfig(tree, options);
setupJustConfig(tree, options);
updateConformanceSetup(tree, options);
}

// ==== helpers ====
Expand Down Expand Up @@ -978,6 +982,52 @@ function updateLocalJestConfig(tree: Tree, options: NormalizedSchema) {
return tree;
}

function updateConformanceSetup(tree: Tree, options: NormalizedSchema) {
if (!tree.exists(options.paths.conformanceSetup)) {
logger.warn('no conformance setup present. skipping...');
return;
}

const conformanceSetupContent = tree.read(options.paths.conformanceSetup, 'utf-8') as string;
const sourceFile = ts.createSourceFile('is-conformant.ts', conformanceSetupContent, ts.ScriptTarget.Latest, true);
const addition = `\ntsConfig: { configName: 'tsconfig.spec.json' },`;

let start: number | undefined;
getConfigObjectFirstPropertyIndex(sourceFile);

if (!start) {
return;
}

const updatedContent = applyChangesToString(conformanceSetupContent, [
{
type: ChangeType.Insert,
index: start,
text: addition,
},
]);

tree.write(options.paths.conformanceSetup, updatedContent);

function getConfigObjectFirstPropertyIndex(node: ts.Node) {
if (ts.isVariableStatement(node)) {
const defaultOptionsVar = node.declarationList.declarations[0].name.getText();
if (defaultOptionsVar === 'defaultOptions') {
const initializer = node.declarationList.declarations[0].initializer;
if (initializer && ts.isObjectLiteralExpression(initializer)) {
const firstProp = initializer.properties[0];
start = firstProp.pos;
return;
}
}
}

ts.forEachChild(node, getConfigObjectFirstPropertyIndex);
}

return tree;
}

function updatedLocalTsConfig(tree: Tree, options: NormalizedSchema) {
const { configs } = createTsSolutionConfig(tree, options);

Expand Down