Skip to content

Commit

Permalink
feat(tools): add conformance setup migration to migrate-converged-pkg…
Browse files Browse the repository at this point in the history
… generator
  • Loading branch information
Hotell committed Apr 25, 2023
1 parent 3a6bbb0 commit f89f5b8
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
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' },
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
54 changes: 52 additions & 2 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 @@ -126,11 +129,11 @@ function runMigrationOnProject(tree: Tree, schema: AssertedSchema, _userLog: Use

const optionsWithTsConfigs = { ...options, tsconfigs: configs };

// update package npm scripts
// // update package npm scripts
updatePackageJson(tree, optionsWithTsConfigs);
updateApiExtractor(tree, optionsWithTsConfigs);

// setup storybook
// // setup storybook
setupStorybook(tree, options);

setupCypress(tree, options);
Expand All @@ -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

0 comments on commit f89f5b8

Please sign in to comment.