diff --git a/packages/cli/config/jest.js b/packages/cli/config/jest.js new file mode 100644 index 0000000000..d849bf307f --- /dev/null +++ b/packages/cli/config/jest.js @@ -0,0 +1,37 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const fs = require('fs'); +const path = require('path'); + +// If the package has it's own jest config, we use that instead. It will have to +// manually extend @spotify/web-scripts/config/jest.config.js that is desired +if (fs.existsSync('jest.config.js')) { + module.exports = require(path.resolve('jest.config.js')); +} else { + module.exports = { + // We base the jest config on web-scripts, it's pretty flat so we skip any complex merging of config objects + // Config can be found here: https://github.com/spotify/web-scripts/blob/master/packages/web-scripts/config/jest.config.js + ...require('@spotify/web-scripts/config/jest.config.js'), + + // Use src/setupTests.ts as the default location for configuring test env + setupFilesAfterEnv: ['/setupTests.ts'], + + // If the package has a jest object in package.json we merge that config in. This is the recommended + // location for configuring tests. + ...require(path.resolve('package.json')).jest, + }; +} diff --git a/packages/cli/src/commands/app/testCommand.ts b/packages/cli/src/commands/app/testCommand.ts new file mode 100644 index 0000000000..d6ba35224d --- /dev/null +++ b/packages/cli/src/commands/app/testCommand.ts @@ -0,0 +1,36 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Command } from 'commander'; +import { resolve as resolvePath } from 'path'; +import { run } from '../../helpers/run'; + +export default async (cmd: Command) => { + const args = [ + 'test', + '--config', + resolvePath(__dirname, '../../../config/jest.js'), + ]; + + if (cmd.watch) { + args.push('--watch'); + } + if (cmd.coverage) { + args.push('--coverage'); + } + + await run('web-scripts', args, { stdio: 'inherit' }); +}; diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index fc1369cde2..127ca213f6 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -20,6 +20,7 @@ import fs from 'fs'; import createPluginCommand from './commands/createPlugin'; import watch from './commands/watch-deps'; import appLint from './commands/app/lint'; +import appTest from './commands/app/testCommand'; import pluginBuild from './commands/plugin/build'; import pluginLint from './commands/plugin/lint'; import pluginServe from './commands/plugin/serve'; @@ -37,6 +38,13 @@ const main = (argv: string[]) => { .description('Lint an app') .action(actionHandler(appLint)); + program + .command('app:test') + .option('--watch', 'Enable watch mode') + .option('--coverage', 'Report test coverage') + .description('Run all tests for an app') + .action(actionHandler(appTest)); + program .command('create-plugin') .description('Creates a new plugin in the current repository')