2021-04-13 17:04:01 +00:00
|
|
|
import { existsSync, promises as fsPromises } from 'fs';
|
|
|
|
import { join } from 'path';
|
2021-04-14 19:21:25 +00:00
|
|
|
import { fileURLToPath } from 'url';
|
2021-03-23 19:20:03 +00:00
|
|
|
import { suite } from 'uvu';
|
|
|
|
import * as assert from 'uvu/assert';
|
|
|
|
import { createRuntime } from '../lib/runtime.js';
|
2021-04-13 17:04:01 +00:00
|
|
|
import { build } from '../lib/build.js';
|
2021-03-23 19:20:03 +00:00
|
|
|
import { loadConfig } from '../lib/config.js';
|
|
|
|
import { doc } from './test-utils.js';
|
|
|
|
|
2021-04-13 17:04:01 +00:00
|
|
|
const { rmdir, readFile } = fsPromises;
|
|
|
|
|
2021-03-24 15:45:38 +00:00
|
|
|
const Markdown = suite('Astro Markdown');
|
2021-03-23 19:20:03 +00:00
|
|
|
|
2021-04-13 17:04:01 +00:00
|
|
|
let runtime, setupError, fixturePath, astroConfig;
|
2021-03-23 19:20:03 +00:00
|
|
|
|
2021-03-24 15:45:38 +00:00
|
|
|
Markdown.before(async () => {
|
2021-04-14 19:21:25 +00:00
|
|
|
fixturePath = fileURLToPath(new URL('./fixtures/astro-markdown', import.meta.url));
|
2021-04-13 17:04:01 +00:00
|
|
|
|
|
|
|
astroConfig = await loadConfig(fixturePath);
|
2021-03-26 19:14:32 +00:00
|
|
|
|
2021-03-23 19:20:03 +00:00
|
|
|
const logging = {
|
|
|
|
level: 'error',
|
2021-03-26 19:14:32 +00:00
|
|
|
dest: process.stderr,
|
2021-03-23 19:20:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2021-03-30 14:52:09 +00:00
|
|
|
runtime = await createRuntime(astroConfig, { logging });
|
2021-03-26 19:14:32 +00:00
|
|
|
} catch (err) {
|
2021-03-23 19:20:03 +00:00
|
|
|
console.error(err);
|
|
|
|
setupError = err;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-24 15:45:38 +00:00
|
|
|
Markdown.after(async () => {
|
2021-03-26 19:14:32 +00:00
|
|
|
(await runtime) && runtime.shutdown();
|
2021-04-26 21:54:20 +00:00
|
|
|
rmdir(join(fixturePath, 'dist'), { recursive: true });
|
2021-03-23 19:20:03 +00:00
|
|
|
});
|
|
|
|
|
2021-03-24 15:45:38 +00:00
|
|
|
Markdown('No errors creating a runtime', () => {
|
2021-03-23 19:20:03 +00:00
|
|
|
assert.equal(setupError, undefined);
|
|
|
|
});
|
|
|
|
|
2021-03-24 15:45:38 +00:00
|
|
|
Markdown('Can load markdown pages with hmx', async () => {
|
2021-03-23 19:20:03 +00:00
|
|
|
const result = await runtime.load('/post');
|
|
|
|
|
|
|
|
assert.equal(result.statusCode, 200);
|
|
|
|
|
|
|
|
const $ = doc(result.contents);
|
|
|
|
assert.ok($('#first').length, 'There is a div added in markdown');
|
|
|
|
assert.ok($('#test').length, 'There is a div added via a component from markdown');
|
|
|
|
});
|
|
|
|
|
2021-04-01 20:34:11 +00:00
|
|
|
Markdown('Can load more complex jsxy stuff', async () => {
|
|
|
|
const result = await runtime.load('/complex');
|
|
|
|
|
|
|
|
const $ = doc(result.contents);
|
|
|
|
const $el = $('#test');
|
|
|
|
assert.equal($el.text(), 'Hello world');
|
|
|
|
});
|
|
|
|
|
2021-04-13 17:04:01 +00:00
|
|
|
Markdown('Bundles client-side JS for prod', async () => {
|
|
|
|
await build(astroConfig);
|
|
|
|
|
2021-04-26 21:54:20 +00:00
|
|
|
const complexHtml = await readFile(join(fixturePath, './dist/complex/index.html'), 'utf-8');
|
2021-04-13 17:04:01 +00:00
|
|
|
|
|
|
|
assert.match(complexHtml, `import("/_astro/components/Counter.js"`);
|
2021-04-26 21:54:20 +00:00
|
|
|
assert.ok(existsSync(join(fixturePath, `./dist/_astro/components/Counter.js`)), 'Counter.jsx is bundled for prod');
|
2021-04-13 17:04:01 +00:00
|
|
|
});
|
|
|
|
|
2021-03-26 19:14:32 +00:00
|
|
|
Markdown.run();
|