diff --git a/packages/build-scripts/src/service/build.ts b/packages/build-scripts/src/service/build.ts index b69995c..6d68d12 100644 --- a/packages/build-scripts/src/service/build.ts +++ b/packages/build-scripts/src/service/build.ts @@ -1,9 +1,8 @@ import chalk from 'chalk'; +import type webpack from 'webpack'; import Context, { ITaskConfig } from '../core/Context'; import webpackStats from '../utils/webpackStats'; import { IRunOptions } from '../types'; - -import webpack = require('webpack'); import fs = require('fs-extra'); import path = require('path'); import log = require('../utils/log'); diff --git a/packages/build-scripts/src/service/test.ts b/packages/build-scripts/src/service/test.ts index adc81e7..ada2491 100644 --- a/packages/build-scripts/src/service/test.ts +++ b/packages/build-scripts/src/service/test.ts @@ -1,11 +1,12 @@ -import { runCLI } from 'jest'; +import chalk from 'chalk'; import Context, { IJestResult } from '../core/Context'; import fs = require('fs-extra'); import path = require('path'); import log = require('../utils/log'); +import type { runCLI } from 'jest'; -export = async function(context?: Context): Promise { +export = async function(context?: Context): Promise { const { command, commandArgs } = context; const { jestArgv = {} } = commandArgs || {}; const { config, regexForTestFiles, ...restArgv } = jestArgv; @@ -67,28 +68,40 @@ export = async function(context?: Context): Promise { config: jestConfig, }); - const result = await new Promise((resolve, reject): void => { - runCLI( - { - ...restArgv, - config: JSON.stringify(jestConfig), - }, - [ctxRoot], - ) - .then(data => { - const { results } = data; - if (results.success) { - resolve(data); - } else { - reject(new Error('Jest failed')); - } - }) - .catch((err: Error) => { - log.error('JEST', err.stack || err.toString()); - }); - }); - - await applyHook(`after.${command}`, { result }); - - return result as IJestResult; + let run: typeof runCLI; + try { + // eslint-disable-next-line @typescript-eslint/no-var-requires + run = require('jest').runCLI; + } catch (err) { + const messages = [ + 'Cannot find module: jest. Make sure this package is installed.', + '', + `You can install this package by running: ${chalk.bold(`npm install jest -D`)}`, + ]; + console.log(messages.join('\n')); + } + if (run) { + const result = await new Promise((resolve, reject): void => { + (run as typeof runCLI)( + { + ...restArgv, + config: JSON.stringify(jestConfig), + }, + [ctxRoot], + ) + .then(data => { + const { results } = data; + if (results.success) { + resolve(data); + } else { + reject(new Error('Jest failed')); + } + }) + .catch((err: Error) => { + log.error('JEST', err.stack || err.toString()); + }); + }); + await applyHook(`after.${command}`, { result }); + return result as IJestResult; + } };