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..3d4b9c04d1 100644 --- a/scripts/cli-e2e-test.js +++ b/scripts/cli-e2e-test.js @@ -36,8 +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'); - await new Promise(resolve => setTimeout(resolve, 2000)); + + 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'); @@ -59,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`); } @@ -117,7 +137,7 @@ async function waitForPageWithText( browser, path, text, - { intervalMs = 1000, maxAttempts = 60 } = {}, + { intervalMs = 1000, maxAttempts = 120 } = {}, ) { let attempts = 0; for (;;) {