Skip to content

Commit

Permalink
fix(testing): handle different jest config files in configurations wh…
Browse files Browse the repository at this point in the history
…en migrating to inferred (#26591)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
leosvelperez authored Jun 19, 2024
1 parent 5646b38 commit c15ac1b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,19 @@ const defaultTestProjectOptions: TestProjectOptions = {
function writeJestConfig(
tree: Tree,
projectRoot: string,
jestConfig: any | undefined
jestConfig: any | undefined,
configFileName = 'jest.config.js'
) {
jestConfig ??= {
coverageDirectory: `../../coverage/${projectRoot}`,
};
const jestConfigContents = `module.exports = ${JSON.stringify(jestConfig)};`;

tree.write(`${projectRoot}/jest.config.js`, jestConfigContents);
fs.createFileSync(`${projectRoot}/jest.config.js`, jestConfigContents);
jest.doMock(
join(fs.tempDir, projectRoot, 'jest.config.js'),
() => jestConfig,
{ virtual: true }
);
tree.write(`${projectRoot}/${configFileName}`, jestConfigContents);
fs.createFileSync(`${projectRoot}/${configFileName}`, jestConfigContents);
jest.doMock(join(fs.tempDir, projectRoot, configFileName), () => jestConfig, {
virtual: true,
});
}

function createTestProject(
Expand Down Expand Up @@ -1013,6 +1012,43 @@ describe('Jest - Convert Executors To Plugin', () => {
const updatedProject2 = readProjectConfiguration(tree, project2.name);
expect(updatedProject2.targets.test).toStrictEqual(project2TestTarget);
});

it('should keep the "jestConfig" and set it to "config" when present in configurations', async () => {
const project = createTestProject(tree);
project.targets.test.configurations = {
production: {
jestConfig: `${defaultTestProjectOptions.appRoot}/jest.config.prod.js`,
},
};
updateProjectConfiguration(tree, project.name, project);
writeJestConfig(
tree,
defaultTestProjectOptions.appRoot,
undefined,
'jest.config.prod.js'
);
const project2 = createTestProject(tree, {
appRoot: 'apps/project2',
appName: 'project2',
});
const project2TestTarget = project2.targets.test;

await convertToInferred(tree, {
project: project.name,
skipFormat: true,
});

// assert updated project configuration
const updatedProject = readProjectConfiguration(tree, project.name);
expect(
updatedProject.targets.test.configurations.production
).toStrictEqual({
config: `./jest.config.prod.js`,
});
// assert other projects were not modified
const updatedProject2 = readProjectConfiguration(tree, project2.name);
expect(updatedProject2.targets.test).toStrictEqual(project2TestTarget);
});
});

describe('all projects', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async function postTargetTransformer(
);

if (target.options) {
await updateOptions(
await updateOptionsObject(
target.options,
projectDetails.root,
tree.root,
Expand All @@ -81,7 +81,7 @@ async function postTargetTransformer(

if (target.configurations) {
for (const [configName, config] of Object.entries(target.configurations)) {
await updateOptions(
await updateConfigurationObject(
config,
projectDetails.root,
tree.root,
Expand Down Expand Up @@ -118,7 +118,7 @@ async function postTargetTransformer(

export default convertToInferred;

async function updateOptions(
async function updateOptionsObject(
targetOptions: any,
projectRoot: string,
workspaceRoot: string,
Expand All @@ -130,6 +130,49 @@ async function updateOptions(
delete targetOptions.jestConfig;
delete targetOptions.config;

await updateOptions(
targetOptions,
projectRoot,
workspaceRoot,
jestConfigPath
);
}

async function updateConfigurationObject(
targetOptions: any,
projectRoot: string,
workspaceRoot: string,
defaultJestConfigPath: string | undefined
) {
const jestConfigPath = targetOptions.jestConfig ?? defaultJestConfigPath;

if (targetOptions.jestConfig) {
targetOptions.config = toProjectRelativePath(
targetOptions.jestConfig,
projectRoot
);
delete targetOptions.jestConfig;
} else if (targetOptions.config) {
targetOptions.config = toProjectRelativePath(
targetOptions.config,
projectRoot
);
}

await updateOptions(
targetOptions,
projectRoot,
workspaceRoot,
jestConfigPath
);
}

async function updateOptions(
targetOptions: any,
projectRoot: string,
workspaceRoot: string,
jestConfigPath: string | undefined
) {
// deprecated and unused
delete targetOptions.tsConfig;

Expand Down

0 comments on commit c15ac1b

Please sign in to comment.