From eed736f2e8f31caf86deca96b1c3320cab551076 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 14 Apr 2020 19:50:05 +0200 Subject: [PATCH 1/2] package/test-utils: move some test utils to separate package so test-utils can depend on core --- packages/core/package.json | 2 +- .../core/src/api/apis/ApiProvider.test.tsx | 2 +- packages/test-utils-core/.npmrc | 1 + packages/test-utils-core/README.md | 15 ++++++++ packages/test-utils-core/package.json | 34 +++++++++++++++++++ packages/test-utils-core/src/index.ts | 16 +++++++++ packages/test-utils-core/src/setupTests.ts | 17 ++++++++++ .../src/testUtils/Keyboard.js | 2 +- .../src/testUtils/Keyboard.test.js | 2 +- .../test-utils-core/src/testUtils/index.tsx | 19 +++++++++++ .../src/testUtils/logCollector.test.ts | 0 .../src/testUtils/logCollector.ts | 0 .../src/testUtils/testingLibrary.ts | 0 packages/test-utils-core/tsconfig.json | 7 ++++ packages/test-utils/package.json | 1 + packages/test-utils/src/index.ts | 1 + .../test-utils/src/testUtils/appWrappers.tsx | 4 --- packages/test-utils/src/testUtils/index.tsx | 3 -- 18 files changed, 115 insertions(+), 11 deletions(-) create mode 100644 packages/test-utils-core/.npmrc create mode 100644 packages/test-utils-core/README.md create mode 100644 packages/test-utils-core/package.json create mode 100644 packages/test-utils-core/src/index.ts create mode 100644 packages/test-utils-core/src/setupTests.ts rename packages/{test-utils => test-utils-core}/src/testUtils/Keyboard.js (99%) rename packages/{test-utils => test-utils-core}/src/testUtils/Keyboard.test.js (98%) create mode 100644 packages/test-utils-core/src/testUtils/index.tsx rename packages/{test-utils => test-utils-core}/src/testUtils/logCollector.test.ts (100%) rename packages/{test-utils => test-utils-core}/src/testUtils/logCollector.ts (100%) rename packages/{test-utils => test-utils-core}/src/testUtils/testingLibrary.ts (100%) create mode 100644 packages/test-utils-core/tsconfig.json diff --git a/packages/core/package.json b/packages/core/package.json index 3dc8150f83..1ec29b0269 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -42,7 +42,7 @@ }, "devDependencies": { "@backstage/cli": "^0.1.1-alpha.3", - "@backstage/test-utils": "^0.1.1-alpha.3", + "@backstage/test-utils-core": "^0.1.1-alpha.3", "@backstage/theme": "^0.1.1-alpha.3", "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.3.2", diff --git a/packages/core/src/api/apis/ApiProvider.test.tsx b/packages/core/src/api/apis/ApiProvider.test.tsx index 5734899f68..f572907c2f 100644 --- a/packages/core/src/api/apis/ApiProvider.test.tsx +++ b/packages/core/src/api/apis/ApiProvider.test.tsx @@ -19,7 +19,7 @@ import ApiProvider, { useApi, withApis } from './ApiProvider'; import ApiRef from './ApiRef'; import ApiRegistry from './ApiRegistry'; import { render } from '@testing-library/react'; -import { withLogCollector } from '@backstage/test-utils'; +import { withLogCollector } from '@backstage/test-utils-core'; describe('ApiProvider', () => { type Api = () => string; diff --git a/packages/test-utils-core/.npmrc b/packages/test-utils-core/.npmrc new file mode 100644 index 0000000000..214c29d139 --- /dev/null +++ b/packages/test-utils-core/.npmrc @@ -0,0 +1 @@ +registry=https://registry.npmjs.org/ diff --git a/packages/test-utils-core/README.md b/packages/test-utils-core/README.md new file mode 100644 index 0000000000..b99250857d --- /dev/null +++ b/packages/test-utils-core/README.md @@ -0,0 +1,15 @@ +# @backstage/test-utils-core + +This package provides utilities for testing the Backstage core packages. + +## Installation + +This package should not be used directly, use `@backstage/test-utils` instead. All exports from this +package are re-exported by `@backstage/test-utils`. + +The reason this package exists is to allow the Backstage core packages to use the testing utils exposed in this package. Since `@backstage/test-utils` needs to depend on `@backstage/core`, core is not able to use those test-utils. We put any test-utils that don't need to depend on other Backstage packages in this package, in order for them to be usable by any other `@backstage` packages. + +## Documentation + +- [Backstage Readme](https://github.com/spotify/backstage/blob/master/README.md) +- [Backstage Documentation](https://github.com/spotify/backstage/blob/master/docs/README.md) diff --git a/packages/test-utils-core/package.json b/packages/test-utils-core/package.json new file mode 100644 index 0000000000..79d36a3525 --- /dev/null +++ b/packages/test-utils-core/package.json @@ -0,0 +1,34 @@ +{ + "name": "@backstage/test-utils-core", + "description": "Utilities to test Backstage core", + "version": "0.1.1-alpha.3", + "private": false, + "publishConfig": { + "access": "public" + }, + "homepage": "https://backstage.io", + "repository": { + "type": "git", + "url": "https://github.com/spotify/backstage", + "directory": "packages/test-utils-core" + }, + "keywords": [ + "backstage" + ], + "license": "Apache-2.0", + "main": "dist/index.cjs.js", + "types": "dist/index.d.ts", + "scripts": { + "build": "backstage-cli plugin:build", + "lint": "backstage-cli lint", + "test": "backstage-cli test" + }, + "dependencies": { + "@testing-library/jest-dom": "^4.2.4", + "@testing-library/react": "^9.3.2", + "@types/jest": "^24.0.0", + "@types/node": "^12.0.0", + "react": "^16.12.0", + "react-dom": "^16.12.0" + } +} diff --git a/packages/test-utils-core/src/index.ts b/packages/test-utils-core/src/index.ts new file mode 100644 index 0000000000..43faf28a4a --- /dev/null +++ b/packages/test-utils-core/src/index.ts @@ -0,0 +1,16 @@ +/* + * 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. + */ +export * from './testUtils'; diff --git a/packages/test-utils-core/src/setupTests.ts b/packages/test-utils-core/src/setupTests.ts new file mode 100644 index 0000000000..8925258421 --- /dev/null +++ b/packages/test-utils-core/src/setupTests.ts @@ -0,0 +1,17 @@ +/* + * 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 '@testing-library/jest-dom/extend-expect'; diff --git a/packages/test-utils/src/testUtils/Keyboard.js b/packages/test-utils-core/src/testUtils/Keyboard.js similarity index 99% rename from packages/test-utils/src/testUtils/Keyboard.js rename to packages/test-utils-core/src/testUtils/Keyboard.js index e3225f2342..8f4a48b0b4 100644 --- a/packages/test-utils/src/testUtils/Keyboard.js +++ b/packages/test-utils-core/src/testUtils/Keyboard.js @@ -23,7 +23,7 @@ const codes = { Esc: 27, }; -export default class Keyboard { +export class Keyboard { static async type(target, input) { await new Keyboard(target).type(input); } diff --git a/packages/test-utils/src/testUtils/Keyboard.test.js b/packages/test-utils-core/src/testUtils/Keyboard.test.js similarity index 98% rename from packages/test-utils/src/testUtils/Keyboard.test.js rename to packages/test-utils-core/src/testUtils/Keyboard.test.js index 31e0885dc1..48b8522c0c 100644 --- a/packages/test-utils/src/testUtils/Keyboard.test.js +++ b/packages/test-utils-core/src/testUtils/Keyboard.test.js @@ -15,7 +15,7 @@ */ import React from 'react'; -import Keyboard from './Keyboard'; +import { Keyboard } from './Keyboard'; import { render } from '@testing-library/react'; describe('testUtils.Keyboard', () => { diff --git a/packages/test-utils-core/src/testUtils/index.tsx b/packages/test-utils-core/src/testUtils/index.tsx new file mode 100644 index 0000000000..7558f32819 --- /dev/null +++ b/packages/test-utils-core/src/testUtils/index.tsx @@ -0,0 +1,19 @@ +/* + * 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. + */ + +export * from './Keyboard'; +export * from './logCollector'; +export * from './testingLibrary'; diff --git a/packages/test-utils/src/testUtils/logCollector.test.ts b/packages/test-utils-core/src/testUtils/logCollector.test.ts similarity index 100% rename from packages/test-utils/src/testUtils/logCollector.test.ts rename to packages/test-utils-core/src/testUtils/logCollector.test.ts diff --git a/packages/test-utils/src/testUtils/logCollector.ts b/packages/test-utils-core/src/testUtils/logCollector.ts similarity index 100% rename from packages/test-utils/src/testUtils/logCollector.ts rename to packages/test-utils-core/src/testUtils/logCollector.ts diff --git a/packages/test-utils/src/testUtils/testingLibrary.ts b/packages/test-utils-core/src/testUtils/testingLibrary.ts similarity index 100% rename from packages/test-utils/src/testUtils/testingLibrary.ts rename to packages/test-utils-core/src/testUtils/testingLibrary.ts diff --git a/packages/test-utils-core/tsconfig.json b/packages/test-utils-core/tsconfig.json new file mode 100644 index 0000000000..7b73db2f0f --- /dev/null +++ b/packages/test-utils-core/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "include": ["src"], + "compilerOptions": { + "baseUrl": "src" + } +} diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index 55b7436aae..ab52ca94a1 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -39,6 +39,7 @@ }, "peerDependencies": { "@backstage/theme": "^0.1.1-alpha.3", + "@backstage/test-utils-core": "^0.1.1-alpha.3", "@material-ui/core": "^4.9.1", "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.3.2", diff --git a/packages/test-utils/src/index.ts b/packages/test-utils/src/index.ts index 43faf28a4a..57b7fd6773 100644 --- a/packages/test-utils/src/index.ts +++ b/packages/test-utils/src/index.ts @@ -14,3 +14,4 @@ * limitations under the License. */ export * from './testUtils'; +export * from '@backstage/test-utils-core'; diff --git a/packages/test-utils/src/testUtils/appWrappers.tsx b/packages/test-utils/src/testUtils/appWrappers.tsx index 6aac9c5542..93f92ec537 100644 --- a/packages/test-utils/src/testUtils/appWrappers.tsx +++ b/packages/test-utils/src/testUtils/appWrappers.tsx @@ -20,10 +20,6 @@ import { MemoryRouter } from 'react-router'; import { Route } from 'react-router-dom'; import { BackstageTheme } from '@backstage/theme'; -export { default as Keyboard } from './Keyboard'; -export { default as mockBreakpoint } from './mockBreakpoint'; -export * from './logCollector'; - export function wrapInTestApp( Component: ComponentType | ReactNode, initialRouterEntries: string[] = ['/'], diff --git a/packages/test-utils/src/testUtils/index.tsx b/packages/test-utils/src/testUtils/index.tsx index 3e8305e0fb..ea33de2f31 100644 --- a/packages/test-utils/src/testUtils/index.tsx +++ b/packages/test-utils/src/testUtils/index.tsx @@ -14,8 +14,5 @@ * limitations under the License. */ -export { default as Keyboard } from './Keyboard'; export { default as mockBreakpoint } from './mockBreakpoint'; export * from './appWrappers'; -export * from './logCollector'; -export * from './testingLibrary'; From 9cf2de05117444998b48391a07ce140901a25339 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 14 Apr 2020 21:17:46 +0200 Subject: [PATCH 2/2] packages/test-utils: added simple test for app wrapper --- .../src/testUtils/appWrappers.test.tsx | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 packages/test-utils/src/testUtils/appWrappers.test.tsx diff --git a/packages/test-utils/src/testUtils/appWrappers.test.tsx b/packages/test-utils/src/testUtils/appWrappers.test.tsx new file mode 100644 index 0000000000..e2f1b6b7cb --- /dev/null +++ b/packages/test-utils/src/testUtils/appWrappers.test.tsx @@ -0,0 +1,35 @@ +/* + * 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 React from 'react'; +import { render } from '@testing-library/react'; +import { wrapInTestApp } from './appWrappers'; +import { Route } from 'react-router'; + +describe('wrapInTestApp', () => { + it('should provide routing', () => { + const rendered = render( + wrapInTestApp( + <> + Route 1 + Route 2 + , + ['/route2'], + ), + ); + expect(rendered.getByText('Route 2')).toBeInTheDocument(); + }); +});