From 9d977ccb9c3e8cd4a7ccc55dc4b494a17e572828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Mon, 23 Mar 2020 08:47:12 +0100 Subject: [PATCH 1/2] Fix act() warnings on newly created plugin test run --- packages/cli/templates/default-plugin/package.json.hbs | 3 ++- .../ExampleComponent/ExampleComponent.test.tsx.hbs | 2 ++ .../ExampleFetchComponent.test.tsx.hbs | 6 ++++-- packages/cli/templates/default-plugin/src/setupTests.ts | 1 + scripts/cli-e2e-test.js | 3 ++- 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/cli/templates/default-plugin/package.json.hbs b/packages/cli/templates/default-plugin/package.json.hbs index c6587f326e..20b78a268b 100644 --- a/packages/cli/templates/default-plugin/package.json.hbs +++ b/packages/cli/templates/default-plugin/package.json.hbs @@ -12,7 +12,8 @@ }, "devDependencies": { "@spotify-backstage/cli": "^{{version}}", - "@types/testing-library__jest-dom": "5.0.2" + "@types/testing-library__jest-dom": "5.0.2", + "jest-fetch-mock": "^3.0.3" }, "dependencies": { "@material-ui/lab": "4.0.0-alpha.45" diff --git a/packages/cli/templates/default-plugin/src/components/ExampleComponent/ExampleComponent.test.tsx.hbs b/packages/cli/templates/default-plugin/src/components/ExampleComponent/ExampleComponent.test.tsx.hbs index 69884c6a44..360055dc3d 100644 --- a/packages/cli/templates/default-plugin/src/components/ExampleComponent/ExampleComponent.test.tsx.hbs +++ b/packages/cli/templates/default-plugin/src/components/ExampleComponent/ExampleComponent.test.tsx.hbs @@ -16,12 +16,14 @@ import React from 'react'; import { render } from '@testing-library/react'; +import mockFetch from 'jest-fetch-mock'; import ExampleComponent from './ExampleComponent'; import { ThemeProvider } from '@material-ui/core'; import { BackstageTheme } from '@spotify-backstage/core'; describe('ExampleComponent', () => { it('should render', () => { + mockFetch.mockResponse(() => new Promise(() => {})); const rendered = render( diff --git a/packages/cli/templates/default-plugin/src/components/ExampleFetchComponent/ExampleFetchComponent.test.tsx.hbs b/packages/cli/templates/default-plugin/src/components/ExampleFetchComponent/ExampleFetchComponent.test.tsx.hbs index 7dff1e6ccc..7fecdc6f11 100644 --- a/packages/cli/templates/default-plugin/src/components/ExampleFetchComponent/ExampleFetchComponent.test.tsx.hbs +++ b/packages/cli/templates/default-plugin/src/components/ExampleFetchComponent/ExampleFetchComponent.test.tsx.hbs @@ -16,11 +16,13 @@ import React from 'react'; import { render } from '@testing-library/react'; +import mockFetch from 'jest-fetch-mock'; import ExampleFetchComponent from './ExampleFetchComponent'; describe('ExampleFetchComponent', () => { - it('should render', () => { + it('should render', async () => { + mockFetch.mockResponse(() => new Promise(() => {})); const rendered = render(); - expect(rendered.getByTestId('progress')).toBeInTheDocument(); + expect(await rendered.findByTestId('progress')).toBeInTheDocument(); }); }); diff --git a/packages/cli/templates/default-plugin/src/setupTests.ts b/packages/cli/templates/default-plugin/src/setupTests.ts index 8925258421..1a907ab8e6 100644 --- a/packages/cli/templates/default-plugin/src/setupTests.ts +++ b/packages/cli/templates/default-plugin/src/setupTests.ts @@ -15,3 +15,4 @@ */ import '@testing-library/jest-dom/extend-expect'; +require('jest-fetch-mock').enableMocks(); diff --git a/scripts/cli-e2e-test.js b/scripts/cli-e2e-test.js index 10be71b1c4..b495a37440 100644 --- a/scripts/cli-e2e-test.js +++ b/scripts/cli-e2e-test.js @@ -37,7 +37,8 @@ async function main() { const createPlugin = spawnPiped(['yarn', 'create-plugin']); createPlugin.stdin.write('test-plugin\n'); - await new Promise(resolve => setTimeout(resolve, 2000)); + // TODO: Add code to await the right prompts from create-plugin stdout instead + await new Promise(resolve => setTimeout(resolve, 5000)); createPlugin.stdin.write('@someuser\n'); print('Waiting for plugin create script to be done'); From 1d3a8c110cf30a8630e35c5973abca00c9b49180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Mon, 23 Mar 2020 11:53:42 +0100 Subject: [PATCH 2/2] Better waiting --- scripts/cli-e2e-test.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/scripts/cli-e2e-test.js b/scripts/cli-e2e-test.js index b495a37440..3d4b9c04d1 100644 --- a/scripts/cli-e2e-test.js +++ b/scripts/cli-e2e-test.js @@ -36,9 +36,16 @@ async function main() { print('Backstage loaded correctly, creating plugin'); const createPlugin = spawnPiped(['yarn', 'create-plugin']); + + let stdout = ''; + createPlugin.stdout.on('data', data => { + stdout = stdout + data.toString('utf8'); + }); + + await waitFor(() => stdout.includes('Enter an ID for the plugin')); createPlugin.stdin.write('test-plugin\n'); - // TODO: Add code to await the right prompts from create-plugin stdout instead - await new Promise(resolve => setTimeout(resolve, 5000)); + + await waitFor(() => stdout.includes('Enter the owner(s) of the plugin')); createPlugin.stdin.write('@someuser\n'); print('Waiting for plugin create script to be done'); @@ -60,6 +67,18 @@ async function main() { process.exit(0); } +function waitFor(fn) { + return new Promise(resolve => { + const handle = setInterval(() => { + if (fn()) { + clearInterval(handle); + resolve(); + return; + } + }, 100); + }); +} + function print(msg) { return process.stdout.write(`${msg}\n`); } @@ -118,7 +137,7 @@ async function waitForPageWithText( browser, path, text, - { intervalMs = 1000, maxAttempts = 60 } = {}, + { intervalMs = 1000, maxAttempts = 120 } = {}, ) { let attempts = 0; for (;;) {