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 14, 2022
1 parent fbf983d commit e8ca7c2
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PREACT_APP_MY_VARIABLE="Hello World!"
1 change: 1 addition & 0 deletions packages/cli/tests/subjects/custom-dotenv/index.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"name": "preact-custom-dotenv"
}
23 changes: 23 additions & 0 deletions packages/cli/tests/watch.test.js
Original file line number Diff line number Diff line change
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 e8ca7c2

Please sign in to comment.