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(jest-utils,nx-plugin): Fix running jest config, test:cov nx configuration, and path resolution in tests #109

Merged
merged 8 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

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