From 4fb95dc3f13178f32c99f6168b0ee9073d2858b6 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 18 Mar 2020 16:54:10 +0100 Subject: [PATCH 1/2] cli: added app:test command --- packages/cli/config/jest.js | 37 ++++++++++++++++++++ packages/cli/src/commands/app/testCommand.ts | 36 +++++++++++++++++++ packages/cli/src/index.ts | 8 +++++ 3 files changed, 81 insertions(+) create mode 100644 packages/cli/config/jest.js create mode 100644 packages/cli/src/commands/app/testCommand.ts 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') From 8637518f3cf24e42c7d103a42f114d51ae7dcf7d Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 18 Mar 2020 16:54:22 +0100 Subject: [PATCH 2/2] app: use cli app:test + copyright header --- packages/app/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/app/package.json b/packages/app/package.json index ec950df39c..e1f5c68c5f 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -27,7 +27,7 @@ "scripts": { "start": "cross-env EXTEND_ESLINT=true SKIP_PREFLIGHT_CHECK=true backstage-cli watch-deps -- react-scripts start", "build": "cross-env EXTEND_ESLINT=true SKIP_PREFLIGHT_CHECK=true react-scripts build", - "test": "cross-env EXTEND_ESLINT=true SKIP_PREFLIGHT_CHECK=true react-scripts test", + "test": "backstage-cli app:test", "lint": "backstage-cli app:lint" }, "browserslist": {