Skip to content

Commit

Permalink
fix(testing): fix(testing): support for typescript jest configs (#6920)
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #5394
  • Loading branch information
AgentEnder committed Sep 3, 2021
1 parent 06d2035 commit 25519a9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
51 changes: 46 additions & 5 deletions packages/jest/src/executors/jest/jest.impl.spec.ts
@@ -1,10 +1,21 @@
import { ExecutorContext } from '@nrwl/tao/src/shared/workspace';
import * as fs from 'fs';

let runCLI = jest.fn();
jest.mock('jest', () => ({
runCLI,
}));

// Mocking these 2 functions is required for jest-config
// Otherwise, jest throws with "Can't find a root directory while resolving a config file path"
jest.mock('fs', () => ({
...(jest.requireActual('fs') as typeof fs),
existsSync: () => true,
lstatSync: () => ({
isDirectory: () => false,
}),
}));

import { jestExecutor } from './jest.impl';
import { JestExecutorOptions } from './schema';

Expand Down Expand Up @@ -56,9 +67,7 @@ describe('Jest Executor', () => {
jest.mock(
'/root/jest.config.js',
() => ({
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
transform: {},
}),
{ virtual: true }
);
Expand Down Expand Up @@ -172,7 +181,6 @@ describe('Jest Executor', () => {
verbose: false,
coverageReporters: ['test'],
coverageDirectory: '/test/coverage',
testResultsProcessor: 'results-processor',
updateSnapshot: true,
useStderr: true,
watch: false,
Expand Down Expand Up @@ -204,7 +212,6 @@ describe('Jest Executor', () => {
reporters: ['/test/path'],
coverageReporters: ['test'],
coverageDirectory: '/root/test/coverage',
testResultsProcessor: 'results-processor',
updateSnapshot: true,
useStderr: true,
watch: false,
Expand Down Expand Up @@ -324,4 +331,38 @@ describe('Jest Executor', () => {
});
});
});

describe('when using typescript config file', () => {
beforeEach(() => {
jest.doMock(
'/root/jest.config.ts',
() => ({
transform: {
'^.+\\.[tj]sx?$': 'babel-jest',
},
}),
{ virtual: true }
);
// jest.spyOn(tsNode, 'register').mockReturnValue(null)
});

it('should send appropriate options to jestCLI', async () => {
await jestExecutor(
{
...defaultOptions,
jestConfig: './jest.config.ts',
watch: false,
},
mockContext
);
expect(runCLI).toHaveBeenCalledWith(
expect.objectContaining({
_: [],
testPathPattern: [],
watch: false,
}),
['/root/jest.config.ts']
);
});
});
});
20 changes: 10 additions & 10 deletions packages/jest/src/executors/jest/jest.impl.ts
Expand Up @@ -16,18 +16,18 @@ export async function jestExecutor(
options: JestExecutorOptions,
context: ExecutorContext
): Promise<{ success: boolean }> {
const config = jestConfigParser(options, context);
const config = await jestConfigParser(options, context);

const { results } = await runCLI(config, [options.jestConfig]);

return { success: results.success };
}

export function jestConfigParser(
export async function jestConfigParser(
options: JestExecutorOptions,
context: ExecutorContext,
multiProjects = false
): Config.Argv {
): Promise<Config.Argv> {
let jestConfig:
| {
transform: any;
Expand All @@ -36,12 +36,6 @@ export function jestConfigParser(
}
| undefined;

if (!multiProjects) {
options.jestConfig = path.resolve(context.root, options.jestConfig);

jestConfig = require(options.jestConfig);
}

const config: Config.Argv = {
$0: undefined,
_: [],
Expand Down Expand Up @@ -73,6 +67,12 @@ export function jestConfigParser(
watchAll: options.watchAll,
};

if (!multiProjects) {
options.jestConfig = path.resolve(context.root, options.jestConfig);

jestConfig = (await readConfig(config, options.jestConfig)).projectConfig;
}

// for backwards compatibility
if (options.setupFile && !multiProjects) {
const setupFilesAfterEnvSet = new Set([
Expand Down Expand Up @@ -132,7 +132,7 @@ export async function batchJest(
);

const { globalConfig, results } = await runCLI(
jestConfigParser(overrides, context, true),
await jestConfigParser(overrides, context, true),
[...configPaths]
);

Expand Down

0 comments on commit 25519a9

Please sign in to comment.