Skip to content

Commit

Permalink
Make jest-repl allow transformer modules with only createTransformer
Browse files Browse the repository at this point in the history
REPL must allow transformer modules to only implement 'createTransformer'
This aligns it with the jest-transform module
  • Loading branch information
fatso83 committed Feb 17, 2022
1 parent 069c2b4 commit d4ac18f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
28 changes: 21 additions & 7 deletions packages/babel-jest/src/__tests__/index.ts
Expand Up @@ -20,6 +20,8 @@ jest.mock('../loadBabelConfig', () => {
};
});

const defaultBabelJestTransformer = babelJest.createTransformer(null);

//Mock data for all the tests
const sourceString = `
const sum = (a, b) => a+b;
Expand All @@ -38,11 +40,17 @@ beforeEach(() => {
});

test('Returns source string with inline maps when no transformOptions is passed', () => {
const result = babelJest.process(sourceString, 'dummy_path.js', {
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
}) as any;
const result = defaultBabelJestTransformer.process(
sourceString,
'dummy_path.js',
{
cacheFS: new Map<string, string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
transformerConfig: {},
},
) as any;
expect(typeof result).toBe('object');
expect(result.code).toBeDefined();
expect(result.map).toBeDefined();
Expand All @@ -53,13 +61,15 @@ test('Returns source string with inline maps when no transformOptions is passed'
});

test('Returns source string with inline maps when no transformOptions is passed async', async () => {
const result: any = await babelJest.processAsync!(
const result: any = await defaultBabelJestTransformer.processAsync!(
sourceString,
'dummy_path.js',
{
cacheFS: new Map<string, string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
transformerConfig: {},
},
);
expect(typeof result).toBe('object');
Expand Down Expand Up @@ -108,10 +118,12 @@ describe('caller option correctly merges from defaults and options', () => {
},
],
])('%j -> %j', (input, output) => {
babelJest.process(sourceString, 'dummy_path.js', {
defaultBabelJestTransformer.process(sourceString, 'dummy_path.js', {
cacheFS: new Map<string, string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
transformerConfig: {},
...input,
});

Expand All @@ -132,9 +144,11 @@ describe('caller option correctly merges from defaults and options', () => {
test('can pass null to createTransformer', () => {
const transformer = createTransformer(null);
transformer.process(sourceString, 'dummy_path.js', {
cacheFS: new Map<string, string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
transformerConfig: {},
});

expect(loadPartialConfig).toHaveBeenCalledTimes(1);
Expand Down
11 changes: 10 additions & 1 deletion packages/jest-repl/src/cli/repl.ts
Expand Up @@ -78,7 +78,16 @@ if (jestProjectConfig.transform) {
}
}
if (transformerPath) {
transformer = interopRequireDefault(require(transformerPath)).default;
const transformerOrFactory = interopRequireDefault(
require(transformerPath),
).default;

if (typeof transformerOrFactory.createTransformer === 'function') {
transformer = transformerOrFactory.createTransformer(transformerConfig);
} else {
transformer = transformerOrFactory;
}

if (typeof transformer.process !== 'function') {
throw new TypeError(
'Jest: a transformer must export a `process` function.',
Expand Down

0 comments on commit d4ac18f

Please sign in to comment.