Skip to content

Commit

Permalink
test: Adding build & watch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Mar 18, 2022
1 parent 43aff22 commit a8ec51e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/cli/tests/build.test.js
Expand Up @@ -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);
});
});
1 change: 1 addition & 0 deletions packages/cli/tests/subjects/custom-dotenv/.env
@@ -0,0 +1 @@
PREACT_APP_MY_VARIABLE="Hello World!"
1 change: 1 addition & 0 deletions packages/cli/tests/subjects/custom-dotenv/index.js
@@ -0,0 +1 @@
console.log(process.env.PREACT_APP_MY_VARIABLE);
4 changes: 4 additions & 0 deletions packages/cli/tests/subjects/custom-dotenv/package.json
@@ -0,0 +1,4 @@
{
"private": true,
"name": "preact-custom-dotenv"
}
23 changes: 23 additions & 0 deletions packages/cli/tests/watch.test.js
Expand Up @@ -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(
'<h1>Preact App</h1>',
'<h1>{process.env.PREACT_APP_MY_VARIABLE}</h1>'
);
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', () => {
Expand Down

0 comments on commit a8ec51e

Please sign in to comment.