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

(feat): support custom jest config paths via --config #526

Merged
merged 4 commits into from
Mar 9, 2020
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"build": "tsc -p tsconfig.json",
"lint": "yarn build && yarn lint:post-build",
"lint:post-build": "node dist/index.js lint src test --ignore-pattern 'test/tests/lint'",
"test": "yarn build && jest --config ./test/jest.config.json",
"test": "yarn build && yarn test:post-build",
"test:post-build": "node dist/index.js test --config ./test/jest.config.json",
"watch": "chokidar \"./package.json\" \"./src/**/*.ts\" \"node_modules\\@jaredpalmer\\rollup-plugin-preserve-shebang\\dist\\index.js\" -c \"yarn build && echo Success\"",
"start": "tsc -p tsconfig.json --watch"
},
Expand Down
8 changes: 6 additions & 2 deletions src/createJestConfig.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Config } from '@jest/types';

export type JestConfigOptions = Partial<Config.InitialOptions>;

export function createJestConfig(
_: (relativePath: string) => void,
rootDir: string
) {
const config = {
): JestConfigOptions {
const config: JestConfigOptions = {
transform: {
'.(ts|tsx)$': require.resolve('ts-jest/dist'),
'.(js|jsx)$': require.resolve('babel-jest'), // jest's default
Expand Down
31 changes: 24 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import semver from 'semver';
import { paths } from './constants';
import * as Messages from './messages';
import { createBuildConfigs } from './createBuildConfigs';
import { createJestConfig } from './createJestConfig';
import { createJestConfig, JestConfigOptions } from './createJestConfig';
import { createEslintConfig } from './createEslintConfig';
import {
resolveApp,
Expand Down Expand Up @@ -493,7 +493,7 @@ prog
.describe(
'Run jest test runner in watch mode. Passes through all flags directly to Jest'
)
.action(async () => {
.action(async (opts: { config?: string }) => {
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'test';
process.env.NODE_ENV = 'test';
Expand All @@ -505,21 +505,38 @@ prog
});

const argv = process.argv.slice(2);
let jestConfig = {
let jestConfig: JestConfigOptions = {
...createJestConfig(
relativePath => path.resolve(__dirname, '..', relativePath),
paths.appRoot
opts.config ? path.dirname(opts.config) : paths.appRoot
),
...appPackageJson.jest,
};

// Allow overriding with jest.config
const jestConfigExists = await fs.pathExists(paths.jestConfig);
if (jestConfigExists) {
const jestConfigContents = require(paths.jestConfig);
const defaultPathExists = await fs.pathExists(paths.jestConfig);
if (opts.config || defaultPathExists) {
const jestConfigPath = resolveApp(opts.config || paths.jestConfig);
const jestConfigContents: JestConfigOptions = require(jestConfigPath);
jestConfig = { ...jestConfig, ...jestConfigContents };
}

// if custom path, delete the arg as it's already been merged
if (opts.config) {
let configIndex = argv.indexOf('--config');
if (configIndex !== -1) {
// case of "--config path", delete both args
argv.splice(configIndex, 2);
} else {
// case of "--config=path", only one arg to delete
const configRegex = /--config=.+/;
configIndex = argv.findIndex(arg => arg.match(configRegex));
if (configIndex !== -1) {
argv.splice(configIndex, 1);
}
}
}

argv.push(
'--config',
JSON.stringify({
Expand Down