From 25519a91ad58a63664e52562b7ea8fc1da6d4597 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Fri, 3 Sep 2021 14:40:56 -0400 Subject: [PATCH] fix(testing): fix(testing): support for typescript jest configs (#6920) ISSUES CLOSED: #5394 --- .../jest/src/executors/jest/jest.impl.spec.ts | 51 +++++++++++++++++-- packages/jest/src/executors/jest/jest.impl.ts | 20 ++++---- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/packages/jest/src/executors/jest/jest.impl.spec.ts b/packages/jest/src/executors/jest/jest.impl.spec.ts index 608b97e01f3dc..d9b644cb7ea02 100644 --- a/packages/jest/src/executors/jest/jest.impl.spec.ts +++ b/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'; @@ -56,9 +67,7 @@ describe('Jest Executor', () => { jest.mock( '/root/jest.config.js', () => ({ - transform: { - '^.+\\.[tj]sx?$': 'ts-jest', - }, + transform: {}, }), { virtual: true } ); @@ -172,7 +181,6 @@ describe('Jest Executor', () => { verbose: false, coverageReporters: ['test'], coverageDirectory: '/test/coverage', - testResultsProcessor: 'results-processor', updateSnapshot: true, useStderr: true, watch: false, @@ -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, @@ -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'] + ); + }); + }); }); diff --git a/packages/jest/src/executors/jest/jest.impl.ts b/packages/jest/src/executors/jest/jest.impl.ts index 9bbf69873db8c..a427d710e6e45 100644 --- a/packages/jest/src/executors/jest/jest.impl.ts +++ b/packages/jest/src/executors/jest/jest.impl.ts @@ -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 { let jestConfig: | { transform: any; @@ -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, _: [], @@ -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([ @@ -132,7 +132,7 @@ export async function batchJest( ); const { globalConfig, results } = await runCLI( - jestConfigParser(overrides, context, true), + await jestConfigParser(overrides, context, true), [...configPaths] );