From ba160c3755b9b438c7a4392bf2a8b055aa56c3d7 Mon Sep 17 00:00:00 2001 From: Jonathan Romano Date: Tue, 16 Aug 2022 11:51:32 -0400 Subject: [PATCH] fix(jest-utils,nx-plugin): Fix running jest config, test:cov nx configuration, and path resolution in tests (#109) * Remove ts-node * Fully reset lockfile * Mark test:cov as cacheable * Use ts-check instead of relying on ts-node, fix absolute path resolution in tests * Add missing semicolon to spec template * Be a bit more robust with determining modulePaths --- packages/jest-utils/src/index.ts | 33 +++++++++++++++++-- packages/nx-plugin/preset.json | 7 +++- ...nfig.ts__tmpl__ => jest.config.js__tmpl__} | 1 + .../ts-iso/files/src/index.spec.ts__tmpl__ | 2 +- .../ts-iso/files/tsconfig.spec.json__tmpl__ | 3 ++ 5 files changed, 42 insertions(+), 4 deletions(-) rename packages/nx-plugin/src/generators/ts-iso/files/{jest.config.ts__tmpl__ => jest.config.js__tmpl__} (87%) diff --git a/packages/jest-utils/src/index.ts b/packages/jest-utils/src/index.ts index 6dd4edb..fbffc50 100644 --- a/packages/jest-utils/src/index.ts +++ b/packages/jest-utils/src/index.ts @@ -1,9 +1,37 @@ import type { Config } from '@jest/types'; +import { existsSync, readFileSync } from 'fs'; + +/** + * Given an object of an unknown type, check if it's an object that contains a particular key + */ +export function inOperator( + k: K, + o: T, +): o is T & Record { + return o && typeof o === 'object' && k in o; +} + +function getModulePaths(tsconfig: string): string[] { + // The user is putting their tsconfig somewhere we don't understand. Instead of + // preventing them from doing it, let them take care of modulePaths themselves + if (!tsconfig || !existsSync(tsconfig)) return []; + + const config: unknown = JSON.parse(readFileSync(tsconfig, 'utf-8')); + // If they don't have a baseUrl set, don't set modulePaths + if (!inOperator('compilerOptions', config)) return []; + const { compilerOptions } = config; + if (!inOperator('baseUrl', compilerOptions)) return []; + const { baseUrl } = compilerOptions; + if (typeof baseUrl !== 'string') return []; + return [baseUrl]; +} // We may want to add other things, so don't default export so that we don't need to // break the API later // eslint-disable-next-line import/prefer-default-export -export function getConfig(mode: 'typescript' | 'vue') { +export function getConfig(mode: 'typescript' | 'vue', options?: { tsconfig?: string }) { + const tsconfig = options?.tsconfig ?? 'tsconfig.spec.json'; + const config: Config.InitialOptions = { testMatch: [ '**/(__tests__|test|tests|spec)/**/*.[jt]s?(x)', @@ -14,9 +42,10 @@ export function getConfig(mode: 'typescript' | 'vue') { '!**/*.(spec|test).(js|mjs|cjs|ts|mts|cts)', '!**/(__tests__|test|tests|spec)/**/*.(js|mjs|cjs|ts|mts|cts)', ], + modulePaths: getModulePaths(tsconfig), globals: { 'ts-jest': { - tsconfig: 'tsconfig.spec.json', + tsconfig, }, }, }; diff --git a/packages/nx-plugin/preset.json b/packages/nx-plugin/preset.json index d797e0d..f45d003 100644 --- a/packages/nx-plugin/preset.json +++ b/packages/nx-plugin/preset.json @@ -13,7 +13,7 @@ "default": { "runner": "nx/tasks-runners/default", "options": { - "cacheableOperations": ["build", "lint", "test", "e2e"] + "cacheableOperations": ["build", "lint", "test", "test:cov", "e2e"] } } }, @@ -45,6 +45,11 @@ "inputs": ["default", "^default", "test"], "dependsOn": ["build"] }, + "test:cov": { + "inputs": ["default", "^default", "test"], + "dependsOn": ["build"], + "outputs": ["./coverage"] + }, "e2e": { "inputs": ["default", "^default"], "dependsOn": ["build"] diff --git a/packages/nx-plugin/src/generators/ts-iso/files/jest.config.ts__tmpl__ b/packages/nx-plugin/src/generators/ts-iso/files/jest.config.js__tmpl__ similarity index 87% rename from packages/nx-plugin/src/generators/ts-iso/files/jest.config.ts__tmpl__ rename to packages/nx-plugin/src/generators/ts-iso/files/jest.config.js__tmpl__ index f644f5b..953c32e 100644 --- a/packages/nx-plugin/src/generators/ts-iso/files/jest.config.ts__tmpl__ +++ b/packages/nx-plugin/src/generators/ts-iso/files/jest.config.js__tmpl__ @@ -1,3 +1,4 @@ +// @ts-check import { getConfig } from '@eternagame/jest-utils'; export default getConfig('typescript'); diff --git a/packages/nx-plugin/src/generators/ts-iso/files/src/index.spec.ts__tmpl__ b/packages/nx-plugin/src/generators/ts-iso/files/src/index.spec.ts__tmpl__ index 26ca66f..2ea2be4 100644 --- a/packages/nx-plugin/src/generators/ts-iso/files/src/index.spec.ts__tmpl__ +++ b/packages/nx-plugin/src/generators/ts-iso/files/src/index.spec.ts__tmpl__ @@ -1 +1 @@ -test.todo('Write tests') +test.todo('Write tests'); diff --git a/packages/nx-plugin/src/generators/ts-iso/files/tsconfig.spec.json__tmpl__ b/packages/nx-plugin/src/generators/ts-iso/files/tsconfig.spec.json__tmpl__ index 95cb951..50bd27e 100644 --- a/packages/nx-plugin/src/generators/ts-iso/files/tsconfig.spec.json__tmpl__ +++ b/packages/nx-plugin/src/generators/ts-iso/files/tsconfig.spec.json__tmpl__ @@ -3,6 +3,9 @@ "references": [ {"path": "./tsconfig.build.json"} ], + "compilerOptions": { + "baseUrl": "src" + }, "include": [ "**/*.spec.ts", "**/*.test.ts",