Skip to content

Commit

Permalink
fix(jest-utils,nx-plugin): Fix running jest config, test:cov nx confi…
Browse files Browse the repository at this point in the history
…guration, 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
  • Loading branch information
luxaritas authored Aug 16, 2022
1 parent 206e4b4 commit ba160c3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
33 changes: 31 additions & 2 deletions packages/jest-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 extends string, T>(
k: K,
o: T,
): o is T & Record<K, unknown> {
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)',
Expand All @@ -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,
},
},
};
Expand Down
7 changes: 6 additions & 1 deletion packages/nx-plugin/preset.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"default": {
"runner": "nx/tasks-runners/default",
"options": {
"cacheableOperations": ["build", "lint", "test", "e2e"]
"cacheableOperations": ["build", "lint", "test", "test:cov", "e2e"]
}
}
},
Expand Down Expand Up @@ -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"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import { getConfig } from '@eternagame/jest-utils';

export default getConfig('typescript');
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test.todo('Write tests')
test.todo('Write tests');
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"references": [
{"path": "./tsconfig.build.json"}
],
"compilerOptions": {
"baseUrl": "src"
},
"include": [
"**/*.spec.ts",
"**/*.test.ts",
Expand Down

0 comments on commit ba160c3

Please sign in to comment.