2024-01-31 03:37:34 -05:00
|
|
|
import * as assert from 'node:assert/strict';
|
|
|
|
import { before, describe, it } from 'node:test';
|
2022-09-26 17:23:47 -05:00
|
|
|
import { parseHTML } from 'linkedom';
|
|
|
|
import { loadFixture } from '../../../astro/test/test-utils.js';
|
|
|
|
|
|
|
|
describe('MDX - Vite env vars', () => {
|
|
|
|
let fixture;
|
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: new URL('./fixtures/mdx-vite-env-vars/', import.meta.url),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Avoids transforming `import.meta.env` outside JSX expressions', async () => {
|
|
|
|
const html = await fixture.readFile('/vite-env-vars/index.html');
|
|
|
|
const { document } = parseHTML(html);
|
2022-09-26 17:25:48 -05:00
|
|
|
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(document.querySelector('h1')?.innerHTML.includes('import.meta.env.SITE'), true);
|
|
|
|
assert.equal(document.querySelector('code')?.innerHTML.includes('import.meta.env.SITE'), true);
|
|
|
|
assert.equal(document.querySelector('pre')?.innerHTML.includes('import.meta.env.SITE'), true);
|
2022-09-26 17:23:47 -05:00
|
|
|
});
|
|
|
|
it('Allows referencing `import.meta.env` in frontmatter', async () => {
|
|
|
|
const { title = '' } = JSON.parse(await fixture.readFile('/frontmatter.json'));
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(title.includes('import.meta.env.SITE'), true);
|
2022-09-26 17:23:47 -05:00
|
|
|
});
|
|
|
|
it('Transforms `import.meta.env` in {JSX expressions}', async () => {
|
|
|
|
const html = await fixture.readFile('/vite-env-vars/index.html');
|
|
|
|
const { document } = parseHTML(html);
|
|
|
|
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(
|
|
|
|
document
|
|
|
|
.querySelector('[data-env-site]')
|
|
|
|
?.innerHTML.includes('https://mdx-is-neat.com/blog/cool-post'),
|
|
|
|
true
|
2022-09-26 17:23:47 -05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
it('Transforms `import.meta.env` in variable exports', async () => {
|
|
|
|
const html = await fixture.readFile('/vite-env-vars/index.html');
|
|
|
|
const { document } = parseHTML(html);
|
|
|
|
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(
|
|
|
|
document.querySelector('[data-env-variable-exports]')?.innerHTML.includes('MODE works'),
|
|
|
|
true
|
2022-09-26 17:23:47 -05:00
|
|
|
);
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(
|
|
|
|
document
|
|
|
|
.querySelector('[data-env-variable-exports-unknown]')
|
2024-03-13 00:52:42 -05:00
|
|
|
?.innerHTML.includes('exports: ""'),
|
2024-01-31 03:37:34 -05:00
|
|
|
true
|
2024-01-10 23:06:14 -05:00
|
|
|
);
|
2022-09-26 17:23:47 -05:00
|
|
|
});
|
|
|
|
it('Transforms `import.meta.env` in HTML attributes', async () => {
|
|
|
|
const html = await fixture.readFile('/vite-env-vars/index.html');
|
|
|
|
const { document } = parseHTML(html);
|
|
|
|
|
|
|
|
const dataAttrDump = document.querySelector('[data-env-dump]');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.notEqual(dataAttrDump, null);
|
2022-09-26 17:23:47 -05:00
|
|
|
|
2024-07-03 16:44:51 -05:00
|
|
|
assert.notEqual(dataAttrDump.getAttribute('data-env-prod'), null);
|
|
|
|
assert.equal(dataAttrDump.getAttribute('data-env-dev'), null);
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(dataAttrDump.getAttribute('data-env-base-url'), '/');
|
|
|
|
assert.equal(dataAttrDump.getAttribute('data-env-mode'), 'production');
|
2022-09-26 17:23:47 -05:00
|
|
|
});
|
|
|
|
});
|