Skip to content

Commit

Permalink
Refactor out generateJestConfig flag logic to reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoBrusa committed Nov 21, 2022
1 parent cf48235 commit b173d0c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 41 deletions.
2 changes: 1 addition & 1 deletion docs/releases/4.0.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ We suggest reading through the following migration guides:

Some noteworthy breaking changes:

- Change to default snapshot options to {escapeString: false,
- Changed default snapshot formatting options to {escapeString: false,
printBasicPrototype: false} as outlined by
[this blogpost](https://jestjs.io/blog/2022/04/25/jest-28#future) - Any
snapshots tests will have to be updated with the new format, or you can
Expand Down
23 changes: 6 additions & 17 deletions packages/modular-scripts/src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import getModularRoot from './utils/getModularRoot';
import execAsync from './utils/execAsync';
import { addFiles, getDiffedFiles, getStagedFiles } from './utils/gitActions';
import * as logger from './utils/logger';
import * as tmp from 'tmp';
import { writeJSONSync } from 'fs-extra';
import { generateJestConfig } from './test/utils';
export interface LintOptions {
all: boolean;
fix: boolean;
Expand Down Expand Up @@ -55,21 +54,11 @@ async function lint(
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
};

// Only used when running on Windows
const tempConfigPath = path.join(
tmp.dirSync().name,
'temp-jest-eslint-config.json',
);

// Windows: pass in configuration as a file as jest won't accept "" around the object and Windows doesn't accept JSON object {} passed directly
// Any other plaform pass config directly to command
let testArgs: string[];
if (process.platform === 'win32') {
writeJSONSync(tempConfigPath, jestEslintConfig);
testArgs = [...regexes, '--config', tempConfigPath];
} else {
testArgs = [...regexes, '--config', JSON.stringify(jestEslintConfig)];
}
const testArgs = [
...regexes,
'--config',
generateJestConfig(jestEslintConfig),
];

const testBin = await resolveAsBin('jest-cli');

Expand Down
27 changes: 5 additions & 22 deletions packages/modular-scripts/src/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import { getAllWorkspaces } from '../utils/getAllWorkspaces';
import { getChangedWorkspaces } from '../utils/getChangedWorkspaces';
import { resolveAsBin } from '../utils/resolveAsBin';
import * as logger from '../utils/logger';
import * as tmp from 'tmp';
import type {
WorkspaceContent,
ModularWorkspacePackage,
} from '@modular-scripts/modular-types';
import { writeJSONSync } from 'fs-extra';
import { generateJestConfig } from './utils';

export interface TestOptions {
ancestors: boolean;
Expand Down Expand Up @@ -89,26 +88,10 @@ async function test(

// pass in jest configuration
const { createJestConfig } = await import('./config');

// Only used when running on Windows
const tempConfigPath = path.join(tmp.dirSync().name, 'temp-jest-config.json');

// Windows: pass in configuration as a file as jest won't accept "" around the object and Windows doesn't accept JSON object {} passed directly
// Any other plaform pass config directly to command
if (process.platform === 'win32') {
writeJSONSync(
tempConfigPath,
createJestConfig({ reporters, testResultsProcessor }),
);
cleanArgv.push('--config', `${tempConfigPath}`);
} else {
cleanArgv.push(
'--config',
`${JSON.stringify(
createJestConfig({ reporters, testResultsProcessor }),
)}`,
);
}
cleanArgv.push(
'--config',
generateJestConfig(createJestConfig({ reporters, testResultsProcessor })),
);

let resolvedEnv;
try {
Expand Down
23 changes: 22 additions & 1 deletion packages/modular-scripts/src/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import _rimraf from 'rimraf';
import execa from 'execa';
import fs from 'fs-extra';
import fs, { writeJSONSync } from 'fs-extra';
import path from 'path';
import * as tmp from 'tmp';
import { promisify } from 'util';

import getModularRoot from '../utils/getModularRoot';
import type { ModularPackageJson } from '@modular-scripts/modular-types';
import { mkdirSync } from 'fs';
import type { Config } from '@jest/types';

const rimraf = promisify(_rimraf);

Expand Down Expand Up @@ -132,3 +133,23 @@ export function mockInstallTemplate(
path.join(tempRepoNodeModules, templateName),
);
}

/**
* Generates the Jest --config flag contents to pass when calling Jest based on config JSON provided
*
* - On Mac, it will provide a stringyfied version of the config JSON provided
*
* - On Windows, it will write the config JSON to a file in a temp directory and provde the path to pass
*/
export function generateJestConfig(jestConfig: Config.InitialOptions): string {
if (process.platform === 'win32') {
const tempConfigPath = path.join(
tmp.dirSync().name,
'temp-jest-config.json',
);
writeJSONSync(tempConfigPath, jestConfig);
return `${tempConfigPath}`;
} else {
return `${JSON.stringify(jestConfig)}`;
}
}

0 comments on commit b173d0c

Please sign in to comment.