From e8ca7c243be4360671849bf5d67849195c649ff1 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Mon, 14 Mar 2022 03:52:38 -0500 Subject: [PATCH] test: Adding build & watch tests --- packages/cli/tests/build.test.js | 15 ++++++++++++ .../cli/tests/subjects/custom-dotenv/.env | 1 + .../cli/tests/subjects/custom-dotenv/index.js | 1 + .../tests/subjects/custom-dotenv/package.json | 4 ++++ packages/cli/tests/watch.test.js | 23 +++++++++++++++++++ 5 files changed, 44 insertions(+) create mode 100644 packages/cli/tests/subjects/custom-dotenv/.env create mode 100644 packages/cli/tests/subjects/custom-dotenv/index.js create mode 100644 packages/cli/tests/subjects/custom-dotenv/package.json diff --git a/packages/cli/tests/build.test.js b/packages/cli/tests/build.test.js index 76934f1ca..c1f4532a5 100644 --- a/packages/cli/tests/build.test.js +++ b/packages/cli/tests/build.test.js @@ -254,4 +254,19 @@ describe('preact build', () => { expect.stringMatching(getRegExpFromMarkup(images.pushManifest)) ); }); + + it('should use a custom `.env` with prefixed environment variables', async () => { + let dir = await subject('custom-dotenv'); + await build(dir); + + const bundleFile = (await readdir(`${dir}/build`)).find(file => + /bundle\.\w{5}\.js$/.test(file) + ); + const transpiledChunk = await readFile( + `${dir}/build/${bundleFile}`, + 'utf8' + ); + // "Hello World!" should replace 'process.env.PREACT_APP_MY_VARIABLE' + expect(transpiledChunk.includes('console.log("Hello World!")')).toBe(true); + }); }); diff --git a/packages/cli/tests/subjects/custom-dotenv/.env b/packages/cli/tests/subjects/custom-dotenv/.env new file mode 100644 index 000000000..d7f22463c --- /dev/null +++ b/packages/cli/tests/subjects/custom-dotenv/.env @@ -0,0 +1 @@ +PREACT_APP_MY_VARIABLE="Hello World!" diff --git a/packages/cli/tests/subjects/custom-dotenv/index.js b/packages/cli/tests/subjects/custom-dotenv/index.js new file mode 100644 index 000000000..9e532391e --- /dev/null +++ b/packages/cli/tests/subjects/custom-dotenv/index.js @@ -0,0 +1 @@ +console.log(process.env.PREACT_APP_MY_VARIABLE); diff --git a/packages/cli/tests/subjects/custom-dotenv/package.json b/packages/cli/tests/subjects/custom-dotenv/package.json new file mode 100644 index 000000000..cdfa8eb1a --- /dev/null +++ b/packages/cli/tests/subjects/custom-dotenv/package.json @@ -0,0 +1,4 @@ +{ + "private": true, + "name": "preact-custom-dotenv" +} diff --git a/packages/cli/tests/watch.test.js b/packages/cli/tests/watch.test.js index bff7f6945..ac51beb45 100644 --- a/packages/cli/tests/watch.test.js +++ b/packages/cli/tests/watch.test.js @@ -34,6 +34,29 @@ describe('preact', () => { server.close(); }); + + it('should use a custom `.env` with prefixed environment variables', async () => { + let app = await create('default'); + server = await watch(app, 8084); + + let page = await loadPage(chrome, 'http://127.0.0.1:8084/'); + + let header = resolve(app, './src/components/header/index.js'); + let original = await readFile(header, 'utf8'); + let update = original.replace( + '

Preact App

', + '

{process.env.PREACT_APP_MY_VARIABLE}

' + ); + await writeFile(header, update); + + // "Hello World!" should replace 'process.env.PREACT_APP_MY_VARIABLE' + await waitUntilExpression( + page, + `document.querySelector('header > h1').innerText === 'Hello World!'` + ); + + server.close(); + }); }); describe('should determine the correct port', () => {