From 618fc87a4be06d2c5fadc94d4142c3abafdbc394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Brauer?= Date: Tue, 15 Feb 2022 15:39:48 +0000 Subject: [PATCH] test: wait for compilation Co-authored-by: Robert Kowalski --- packages/jest-environment/helpers.js | 21 ++++++++++++------- packages/jest-environment/index.js | 8 +++++-- .../__tests__/object-config.js | 6 +++++- .../__tests__/string-config.js | 6 +++++- .../esbuild-typescript/__tests__/develop.js | 9 +++++++- .../integration/esbuild/__tests__/develop.js | 9 +++++++- .../fast-refresh/__tests__/develop.js | 9 +++++++- .../graphql-apollo3/__tests__/develop.js | 6 +++++- .../graphql-mock-server/__tests__/develop.js | 7 ++++++- .../graphql-no-ssr/__tests__/develop.js | 7 ++++++- .../hot-module-reload/__tests__/develop.js | 6 +++++- .../integration/postcss/__tests__/develop.js | 6 +++++- .../integration/react/__tests__/develop.js | 6 +++++- .../redux-without-ssr/__tests__/develop.js | 6 +++++- .../integration/redux/__tests__/develop.js | 6 +++++- .../integration/redux/__tests__/mocked.js | 6 +++++- .../styled-components/__tests__/develop.js | 6 +++++- .../__tests__/develop.js | 6 +++++- .../typescript/__tests__/develop.js | 6 +++++- 19 files changed, 116 insertions(+), 26 deletions(-) diff --git a/packages/jest-environment/helpers.js b/packages/jest-environment/helpers.js index 174083e86..84db35299 100644 --- a/packages/jest-environment/helpers.js +++ b/packages/jest-environment/helpers.js @@ -45,11 +45,10 @@ const build = ({ cwd, env = {}, argv = [] }) => const startServer = ({ cwd, command, env = {}, argv = [] }) => { const teardownPromise = new EProm(); const urlPromise = new EProm(); + const hasFinishedPromise = new EProm(); let stdout = ''; let stderr = ''; - const success1 = "bundling 'develop' finished"; - const success2 = "bundling 'node' finished"; const hopsBin = resolveFrom(cwd, 'hops/bin'); const args = [hopsBin, command].concat(argv); @@ -64,7 +63,12 @@ const startServer = ({ cwd, command, env = {}, argv = [] }) => { return teardownPromise; }; - let serverUrl = ''; + let linesToFinish = []; + const hasFinished = (lines) => { + linesToFinish.push(...lines); + return hasFinishedPromise; + }; + started.stdout.on('data', (data) => { const line = data.toString('utf-8'); debug('stdout >', line); @@ -72,12 +76,15 @@ const startServer = ({ cwd, command, env = {}, argv = [] }) => { const [, url] = line.match(/listening at (.*)/i) || []; if (url) { - serverUrl = url; debug('found match:', url); + urlPromise.resolve(url); } - if (stdout.includes(success1) && stdout.includes(success2) && serverUrl) { - urlPromise.resolve(serverUrl); + if ( + linesToFinish.length > 0 && + linesToFinish.every((lineToFinish) => stdout.includes(lineToFinish)) + ) { + hasFinishedPromise.resolve(); } }); @@ -99,7 +106,7 @@ const startServer = ({ cwd, command, env = {}, argv = [] }) => { stopServer(); }); - return { getUrl: () => urlPromise, stopServer }; + return { getUrl: () => urlPromise, stopServer, hasFinished }; }; const isPuppeteerDisabled = (pragmas) => { diff --git a/packages/jest-environment/index.js b/packages/jest-environment/index.js index 649063e55..3f5ce009d 100644 --- a/packages/jest-environment/index.js +++ b/packages/jest-environment/index.js @@ -112,7 +112,11 @@ class FixtureEnvironment extends NodeEnvironment { ); } const { env, argv } = getHopsCommandModifications(args); - const { getUrl, stopServer: killServer } = startServer({ + const { + getUrl, + stopServer: killServer, + hasFinished, + } = startServer({ cwd: that.cwd, command: 'start', env, @@ -124,7 +128,7 @@ class FixtureEnvironment extends NodeEnvironment { delete that.killServer; return killServer(); }; - return { getUrl, stopServer }; + return { getUrl, stopServer, hasFinished }; }, }; } diff --git a/packages/spec/integration/development-proxy/__tests__/object-config.js b/packages/spec/integration/development-proxy/__tests__/object-config.js index 4f942fcb5..7695512f1 100644 --- a/packages/spec/integration/development-proxy/__tests__/object-config.js +++ b/packages/spec/integration/development-proxy/__tests__/object-config.js @@ -25,8 +25,12 @@ describe('development proxy object config', () => { }; fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); - const { getUrl } = HopsCLI.start('--fast-dev'); + const { getUrl, hasFinished } = HopsCLI.start('--fast-dev'); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('proxies with proxy config set as object', async () => { diff --git a/packages/spec/integration/development-proxy/__tests__/string-config.js b/packages/spec/integration/development-proxy/__tests__/string-config.js index a982783ab..aac07ee02 100644 --- a/packages/spec/integration/development-proxy/__tests__/string-config.js +++ b/packages/spec/integration/development-proxy/__tests__/string-config.js @@ -19,8 +19,12 @@ describe('development proxy string config', () => { packageJson.hops.proxy = `http://localhost:${PORT}/proxy`; fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); - const { getUrl } = HopsCLI.start('--fast-dev'); + const { getUrl, hasFinished } = HopsCLI.start('--fast-dev'); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('proxies with proxy config set as string', async () => { diff --git a/packages/spec/integration/esbuild-typescript/__tests__/develop.js b/packages/spec/integration/esbuild-typescript/__tests__/develop.js index 862870a58..edbef7381 100644 --- a/packages/spec/integration/esbuild-typescript/__tests__/develop.js +++ b/packages/spec/integration/esbuild-typescript/__tests__/develop.js @@ -4,8 +4,15 @@ describe('typescript development server', () => { let url; beforeAll(async () => { - const { getUrl } = HopsCLI.start('--fast-dev', '--experimental-esbuild'); + const { getUrl, hasFinished } = HopsCLI.start( + '--fast-dev', + '--experimental-esbuild' + ); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('renders a simple jsx site', async () => { diff --git a/packages/spec/integration/esbuild/__tests__/develop.js b/packages/spec/integration/esbuild/__tests__/develop.js index 83b1dce3d..ccfafad14 100644 --- a/packages/spec/integration/esbuild/__tests__/develop.js +++ b/packages/spec/integration/esbuild/__tests__/develop.js @@ -4,8 +4,15 @@ describe('esbuild - develop', () => { let url, page, getInnerText; beforeAll(async () => { - const { getUrl } = HopsCLI.start('--fast-dev', '--experimental-esbuild'); + const { getUrl, hasFinished } = HopsCLI.start( + '--fast-dev', + '--experimental-esbuild' + ); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); const pptr = await createPage(); page = pptr.page; getInnerText = pptr.getInnerText; diff --git a/packages/spec/integration/fast-refresh/__tests__/develop.js b/packages/spec/integration/fast-refresh/__tests__/develop.js index 32ff6a3c8..d3ceed365 100644 --- a/packages/spec/integration/fast-refresh/__tests__/develop.js +++ b/packages/spec/integration/fast-refresh/__tests__/develop.js @@ -16,8 +16,15 @@ describe('react fast refresh', () => { let url; beforeAll(async () => { - const { getUrl } = HopsCLI.start('--fast-dev', '--fast-refresh'); + const { getUrl, hasFinished } = HopsCLI.start( + '--fast-dev', + '--fast-refresh' + ); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('renders caption only once', async () => { diff --git a/packages/spec/integration/graphql-apollo3/__tests__/develop.js b/packages/spec/integration/graphql-apollo3/__tests__/develop.js index b7d9c5b41..b54061c47 100644 --- a/packages/spec/integration/graphql-apollo3/__tests__/develop.js +++ b/packages/spec/integration/graphql-apollo3/__tests__/develop.js @@ -8,8 +8,12 @@ describe('graphql development client', () => { let url; beforeAll(async () => { - const { getUrl } = HopsCLI.start('--fast-dev'); + const { getUrl, hasFinished } = HopsCLI.start('--fast-dev'); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); describe('/invalid-response (HTML)', () => { diff --git a/packages/spec/integration/graphql-mock-server/__tests__/develop.js b/packages/spec/integration/graphql-mock-server/__tests__/develop.js index e6958841d..e62525b71 100644 --- a/packages/spec/integration/graphql-mock-server/__tests__/develop.js +++ b/packages/spec/integration/graphql-mock-server/__tests__/develop.js @@ -4,8 +4,13 @@ describe('graphql mock server', () => { let url; beforeAll(async () => { - const { getUrl } = HopsCLI.start('--fast-dev'); + const { getUrl, hasFinished } = HopsCLI.start('--fast-dev'); url = await getUrl(); + await hasFinished([ + "bundling 'graphql-mock-server' finished", + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('renders a mocked quote', async () => { diff --git a/packages/spec/integration/graphql-no-ssr/__tests__/develop.js b/packages/spec/integration/graphql-no-ssr/__tests__/develop.js index 7fa89ba49..40fc3ba8d 100644 --- a/packages/spec/integration/graphql-no-ssr/__tests__/develop.js +++ b/packages/spec/integration/graphql-no-ssr/__tests__/develop.js @@ -4,8 +4,13 @@ describe('graphql mock server without SSR', () => { let url; beforeAll(async () => { - const { getUrl } = HopsCLI.start('--fast-dev'); + const { getUrl, hasFinished } = HopsCLI.start('--fast-dev'); url = await getUrl(); + await hasFinished([ + "bundling 'graphql-mock-server' finished", + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('requests data on the client & not on the server', async () => { diff --git a/packages/spec/integration/hot-module-reload/__tests__/develop.js b/packages/spec/integration/hot-module-reload/__tests__/develop.js index e3c69b254..ab8271124 100644 --- a/packages/spec/integration/hot-module-reload/__tests__/develop.js +++ b/packages/spec/integration/hot-module-reload/__tests__/develop.js @@ -19,8 +19,12 @@ describe.skip('hot module reload', () => { let url; beforeAll(async () => { - const { getUrl } = HopsCLI.start('--fast-dev'); + const { getUrl, hasFinished } = HopsCLI.start('--fast-dev'); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('reflects changes automatically', async () => { diff --git a/packages/spec/integration/postcss/__tests__/develop.js b/packages/spec/integration/postcss/__tests__/develop.js index 733cf7b50..2d0169b28 100644 --- a/packages/spec/integration/postcss/__tests__/develop.js +++ b/packages/spec/integration/postcss/__tests__/develop.js @@ -4,8 +4,12 @@ describe('react-postcss', () => { let url; beforeAll(async () => { - const { getUrl } = HopsCLI.start('--fast-dev'); + const { getUrl, hasFinished } = HopsCLI.start('--fast-dev'); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('styles when served in development mode', async () => { diff --git a/packages/spec/integration/react/__tests__/develop.js b/packages/spec/integration/react/__tests__/develop.js index 1ddc9b9a6..8dd11dedf 100644 --- a/packages/spec/integration/react/__tests__/develop.js +++ b/packages/spec/integration/react/__tests__/develop.js @@ -6,8 +6,12 @@ describe('react development server', () => { let url; beforeAll(async () => { - const { getUrl } = HopsCLI.start('--fast-dev'); + const { getUrl, hasFinished } = HopsCLI.start('--fast-dev'); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('renders home', async () => { diff --git a/packages/spec/integration/redux-without-ssr/__tests__/develop.js b/packages/spec/integration/redux-without-ssr/__tests__/develop.js index 12c210cc6..785fd0e2a 100644 --- a/packages/spec/integration/redux-without-ssr/__tests__/develop.js +++ b/packages/spec/integration/redux-without-ssr/__tests__/develop.js @@ -4,8 +4,12 @@ describe('redux development server', () => { let url; beforeAll(async () => { - const { getUrl } = HopsCLI.start('--fast-dev'); + const { getUrl, hasFinished } = HopsCLI.start('--fast-dev'); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('increments the counter on page load', async () => { diff --git a/packages/spec/integration/redux/__tests__/develop.js b/packages/spec/integration/redux/__tests__/develop.js index 4e96c2e80..8c0db829f 100644 --- a/packages/spec/integration/redux/__tests__/develop.js +++ b/packages/spec/integration/redux/__tests__/develop.js @@ -5,13 +5,17 @@ describe('redux development server', () => { let url; beforeAll(async () => { - const { getUrl } = HopsCLI.start( + const { getUrl, hasFinished } = HopsCLI.start( { PORT: '8950', }, '--fast-dev' ); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('has default state', async () => { diff --git a/packages/spec/integration/redux/__tests__/mocked.js b/packages/spec/integration/redux/__tests__/mocked.js index ebde43cbf..7142984cd 100644 --- a/packages/spec/integration/redux/__tests__/mocked.js +++ b/packages/spec/integration/redux/__tests__/mocked.js @@ -70,7 +70,7 @@ describe('development server with msw mocks', () => { let baseUrl; beforeAll(async () => { - const { getUrl } = HopsCLI.start( + const { getUrl, hasFinished } = HopsCLI.start( { ENABLE_MSW: 'true', PORT: '8949', @@ -78,6 +78,10 @@ describe('development server with msw mocks', () => { '--fast-dev' ); baseUrl = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); describe('Adding up to 12', () => { diff --git a/packages/spec/integration/styled-components/__tests__/develop.js b/packages/spec/integration/styled-components/__tests__/develop.js index ed6402b09..1a1f24fed 100644 --- a/packages/spec/integration/styled-components/__tests__/develop.js +++ b/packages/spec/integration/styled-components/__tests__/develop.js @@ -4,8 +4,12 @@ describe('styled-components development server', () => { let url; beforeAll(async () => { - const { getUrl } = HopsCLI.start('--fast-dev'); + const { getUrl, hasFinished } = HopsCLI.start('--fast-dev'); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('allows to use styled components', async () => { diff --git a/packages/spec/integration/typescript-styled-components/__tests__/develop.js b/packages/spec/integration/typescript-styled-components/__tests__/develop.js index 2ac1a356c..a1c133986 100644 --- a/packages/spec/integration/typescript-styled-components/__tests__/develop.js +++ b/packages/spec/integration/typescript-styled-components/__tests__/develop.js @@ -4,8 +4,12 @@ describe('typescript-styled-components development server', () => { let url; beforeAll(async () => { - const { getUrl } = HopsCLI.start('--fast-dev'); + const { getUrl, hasFinished } = HopsCLI.start('--fast-dev'); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('allows to use the css-props', async () => { diff --git a/packages/spec/integration/typescript/__tests__/develop.js b/packages/spec/integration/typescript/__tests__/develop.js index 30c722146..a821b55f5 100644 --- a/packages/spec/integration/typescript/__tests__/develop.js +++ b/packages/spec/integration/typescript/__tests__/develop.js @@ -4,8 +4,12 @@ describe('typescript development server', () => { let url; beforeAll(async () => { - const { getUrl } = HopsCLI.start('--fast-dev'); + const { getUrl, hasFinished } = HopsCLI.start('--fast-dev'); url = await getUrl(); + await hasFinished([ + "bundling 'develop' finished", + "bundling 'node' finished", + ]); }); it('renders a simple jsx site', async () => {