Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(testing): support for typescript jest configs #6920

Merged
merged 1 commit into from Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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