2021-04-21 16:14:44 +00:00
|
|
|
import { suite } from 'uvu';
|
|
|
|
import * as assert from 'uvu/assert';
|
|
|
|
import { format } from './test-utils.js';
|
2021-04-30 21:33:35 +00:00
|
|
|
import { promises as fs } from 'fs';
|
2021-05-03 18:26:10 +00:00
|
|
|
import { fileURLToPath } from 'url';
|
2021-04-21 16:14:44 +00:00
|
|
|
const Prettier = suite('Prettier formatting');
|
|
|
|
|
2021-05-14 16:03:41 +00:00
|
|
|
const readFile = (path) => fs.readFile(fileURLToPath(new URL(`./fixtures${path}`, import.meta.url))).then((res) => res.toString().replace(/\r\n/g, '\n'));
|
2021-04-21 16:14:44 +00:00
|
|
|
|
2021-04-22 19:10:06 +00:00
|
|
|
/**
|
|
|
|
* Utility to get `[src, out]` files
|
|
|
|
* @param name {string}
|
|
|
|
* @param ctx {any}
|
|
|
|
*/
|
2021-04-30 21:33:35 +00:00
|
|
|
const getFiles = async (name) => {
|
2021-04-21 16:14:44 +00:00
|
|
|
const [src, out] = await Promise.all([readFile(`/in/${name}.astro`), readFile(`/out/${name}.astro`)]);
|
|
|
|
return [src, out];
|
2021-04-22 19:10:06 +00:00
|
|
|
};
|
2021-04-21 16:14:44 +00:00
|
|
|
|
2021-04-30 21:33:35 +00:00
|
|
|
Prettier('can format a basic Astro file', async () => {
|
|
|
|
const [src, out] = await getFiles('basic');
|
2021-06-22 14:06:30 +00:00
|
|
|
assert.not.fixture(src, out);
|
2021-04-21 16:14:44 +00:00
|
|
|
|
|
|
|
const formatted = format(src);
|
2021-06-22 14:06:30 +00:00
|
|
|
assert.fixture(formatted, out);
|
2021-04-21 16:14:44 +00:00
|
|
|
});
|
|
|
|
|
2021-04-30 21:33:35 +00:00
|
|
|
Prettier('can format an Astro file with frontmatter', async () => {
|
|
|
|
const [src, out] = await getFiles('frontmatter');
|
2021-06-22 14:06:30 +00:00
|
|
|
assert.not.fixture(src, out);
|
2021-04-22 19:10:06 +00:00
|
|
|
|
2021-04-21 16:14:44 +00:00
|
|
|
const formatted = format(src);
|
2021-06-22 14:06:30 +00:00
|
|
|
assert.fixture(formatted, out);
|
2021-04-21 16:14:44 +00:00
|
|
|
});
|
|
|
|
|
2021-06-25 22:21:03 +00:00
|
|
|
Prettier.skip('can format an Astro file with embedded JSX expressions', async () => {
|
2021-04-30 21:33:35 +00:00
|
|
|
const [src, out] = await getFiles('embedded-expr');
|
2021-06-22 14:06:30 +00:00
|
|
|
assert.not.fixture(src, out);
|
2021-04-22 19:10:06 +00:00
|
|
|
|
2021-04-21 16:14:44 +00:00
|
|
|
const formatted = format(src);
|
2021-06-22 14:06:30 +00:00
|
|
|
assert.fixture(formatted, out);
|
2021-04-21 16:14:44 +00:00
|
|
|
});
|
|
|
|
|
2021-06-25 22:21:03 +00:00
|
|
|
// This is currently failing! See: https://github.com/snowpackjs/astro/issues/478
|
|
|
|
Prettier.skip('can format an Astro file with a JSX expression in an attribute', async () => {
|
|
|
|
const [src, out] = await getFiles('attribute-with-embedded-expr');
|
|
|
|
assert.not.fixture(src, out);
|
|
|
|
|
|
|
|
const formatted = format(src);
|
|
|
|
assert.fixture(formatted, out);
|
|
|
|
});
|
|
|
|
|
2021-04-21 16:14:44 +00:00
|
|
|
Prettier.run();
|