Skip to content

Commit

Permalink
Added simultaneous support of jest v26 & v27 for transformer (#548)
Browse files Browse the repository at this point in the history
* Added simultaneous support of jest v26 & v27 for transformer

* Fixed compat with require of non-default-exported members

* Slight adjustments of codestyle and arguments rename

* Backmerged with master to work against yarn v3 + Jest types should be definitely bumped to v27

* Backmerged master and resolved lock file issues

Co-authored-by: Vadym Okun <[email protected]>
  • Loading branch information
cbmd and cbmd authored Sep 3, 2021
1 parent 49f9ffc commit caa137d
Show file tree
Hide file tree
Showing 4 changed files with 742 additions and 1,082 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
"@graphql-codegen/typescript-resolvers": "1.19.1",
"@graphql-tools/graphql-file-loader": "6.2.7",
"@graphql-tools/import": "6.3.1",
"@jest/transform": "26.6.2",
"@jest/types": "26.6.2",
"@jest/transform": "^27.1.0",
"@jest/types": "^27.1.0",
"@types/babel-plugin-macros": "2.8.5",
"@types/babel__core": "7.1.15",
"@types/babel__preset-env": "7.9.2",
Expand All @@ -76,7 +76,7 @@
"@types/diff": "5.0.1",
"@types/eslint": "7.2.8",
"@types/eslint-plugin-prettier": "3.1.0",
"@types/jest": "26.0.24",
"@types/jest": "^27.0.1",
"@types/loader-utils": "2.0.3",
"@types/lodash.pick": "4.4.6",
"@types/memory-fs": "0.3.3",
Expand All @@ -94,7 +94,7 @@
"@typescript-eslint/eslint-plugin": "4.26.0",
"@typescript-eslint/parser": "4.28.3",
"@typescript/vfs": "1.3.4",
"babel-jest": "26.6.3",
"babel-jest": "^27.1.0",
"babel-loader": "8.2.2",
"babel-plugin-macros": "3.1.0",
"babel-plugin-module-resolver": "4.1.0",
Expand All @@ -110,7 +110,7 @@
"graphql": "14.7.0",
"graphql-tag": "2.12.5",
"husky": "6.0.0",
"jest": "26.6.3",
"jest": "^27.1.0",
"lint-staged": "10.5.4",
"lodash.pick": "4.4.0",
"memory-fs": "0.5.0",
Expand All @@ -124,7 +124,7 @@
"remark-toc": "7.2.0",
"rimraf": "3.0.2",
"terminate": "2.1.2",
"ts-jest": "26.5.6",
"ts-jest": "^27.0.5",
"tslib": "2.3.1",
"typescript": "4.2.3",
"wait-on": "5.3.0",
Expand Down
8 changes: 5 additions & 3 deletions src/jestTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import compiler from './lib/__tools/compile';
import { prepareFixtures } from './lib/__tools/file';

let cwd: string;
let jestConfig: Config.ProjectConfig;
let config: Partial<Config.ProjectConfig>;

describe('graphql-let/jestTransformer', () => {
beforeAll(async () => {
[cwd] = await prepareFixtures(__dirname, '__fixtures/jestTransformer');
jestConfig = { rootDir: cwd } as Config.ProjectConfig;
config = { rootDir: cwd };
});

test('transforms .graphql', async () => {
Expand All @@ -22,10 +22,12 @@ describe('graphql-let/jestTransformer', () => {
.filter(Boolean);

const fullPath = join(cwd, fileName);
// @ts-expect-error
const { code: transformedContent } = jestTransformer.process(
fileData!,
fullPath,
jestConfig,
// @ts-expect-error
{ config },
) as { code: string };
expect(removeSourcemapReference(transformedContent)).toMatchSnapshot();
});
Expand Down
43 changes: 25 additions & 18 deletions src/jestTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ type JestTransformerOptions = {
subsequentTransformer?: string;
};

function getDefaultExportIfExists(moduleName: string) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const mod = require(moduleName);

return (mod?.default || mod) as typeof mod;
}

function getOption(jestConfig: ProjectConfig): JestTransformerOptions {
if (!Array.isArray(jestConfig.transform)) return {};
for (const [, entryPoint, opts] of jestConfig.transform) {
Expand All @@ -20,40 +27,40 @@ function getOption(jestConfig: ProjectConfig): JestTransformerOptions {
return {};
}

const jestTransformer: Transformer = {
getCacheKey(fileData, filename, configString) {
return createHash(fileData + filename + configString + 'graphql-let');
const jestTransformer: Transformer<JestTransformerOptions> = {
getCacheKey(sourceText, sourcePath, options) {
const configString = options?.configString || options;

return createHash(sourceText + sourcePath + configString + 'graphql-let');
},
process(input, filePath, jestConfig, transformOptions) {
process(sourceText, sourcePath, ...rest) {
// jest v26 vs v27 changes to support both formats: start
const [__compatJestConfig] = rest;
const jestConfig = __compatJestConfig?.config ?? __compatJestConfig;
// jest v26 vs v27 changes to support both formats: end
const { rootDir: cwd } = jestConfig;
const { configFile, subsequentTransformer } = getOption(jestConfig);
const [config, configHash] = loadConfigSync(cwd, configFile);
const { execContext } = createExecContextSync(cwd, config, configHash);

const { tsxFullPath } = createPaths(execContext, relative(cwd, filePath));
const { tsxFullPath } = createPaths(execContext, relative(cwd, sourcePath));
const tsxContent = readFileSync(tsxFullPath, 'utf-8');

// Let users customize a subsequent transformer
if (subsequentTransformer) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require(subsequentTransformer).process(
tsxContent,
tsxFullPath,
jestConfig,
transformOptions,
const _subsequentTransformer = getDefaultExportIfExists(
subsequentTransformer,
);
return _subsequentTransformer.process(tsxContent, tsxFullPath, ...rest);
}

// jest v26 vs v27 changes to support both formats: start
// "babel-jest" by default
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { createTransformer } = require('babel-jest');
const { createTransformer } = getDefaultExportIfExists('babel-jest');
// jest v26 vs v27 changes to support both formats: end
const babelTransformer = createTransformer({ cwd: cwd });
return babelTransformer.process(
tsxContent,
tsxFullPath,
jestConfig,
transformOptions,
);
return babelTransformer.process(tsxContent, tsxFullPath, ...rest);
},
};

Expand Down
Loading

0 comments on commit caa137d

Please sign in to comment.