From 4d54f8b82c984fd5d928b737a4eecd888786d2cd Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 16 Apr 2021 12:36:18 -0500 Subject: [PATCH] test(prettier): add simple prettier tests --- test/astro-prettier.test.js | 72 +++++++++---------- test/fixtures/astro-prettier/in/basic.astro | 13 ++++ .../astro-prettier/in/embedded-expr.astro | 23 ++++++ .../astro-prettier/in/frontmatter.astro | 18 +++++ test/fixtures/astro-prettier/out/basic.astro | 12 ++++ .../astro-prettier/out/embedded-expr.astro | 22 ++++++ .../astro-prettier/out/frontmatter.astro | 16 +++++ test/helpers.js | 4 +- 8 files changed, 138 insertions(+), 42 deletions(-) create mode 100644 test/fixtures/astro-prettier/in/basic.astro create mode 100644 test/fixtures/astro-prettier/in/embedded-expr.astro create mode 100644 test/fixtures/astro-prettier/in/frontmatter.astro create mode 100644 test/fixtures/astro-prettier/out/basic.astro create mode 100644 test/fixtures/astro-prettier/out/embedded-expr.astro create mode 100644 test/fixtures/astro-prettier/out/frontmatter.astro diff --git a/test/astro-prettier.test.js b/test/astro-prettier.test.js index aebaefacccec5..1dd1887f4fb3c 100644 --- a/test/astro-prettier.test.js +++ b/test/astro-prettier.test.js @@ -5,48 +5,40 @@ import { setup } from './helpers.js'; const Prettier = suite('Prettier formatting'); -setup(Prettier, './fixtures/astro-expr'); - -Prettier('Can load page', async ({ readSrcFile }) => { - const src = await readSrcFile('/index.astro'); - assert.not.type(src, 'undefined'); - - const result = format(src); - assert.not.type(result, 'undefined'); +setup(Prettier, './fixtures/astro-prettier'); + +/** + * Utility to get `[src, out]` files + * @param name {string} + * @param ctx {any} + */ +const getFiles = async (name, { readFile }) => { + const [src, out] = await Promise.all([readFile(`/in/${name}.astro`), readFile(`/out/${name}.astro`)]); + return [src, out]; +} + +Prettier('can format a basic Astro file', async (ctx) => { + const [src, out] = await getFiles('basic', ctx); + assert.not.equal(src, out); + + const formatted = format(src); + assert.equal(formatted, out); }); -// Prettier('Ignores characters inside of strings', async ({ runtime }) => { -// const result = await runtime.load('/strings'); - -// assert.equal(result.statusCode, 200); - -// const $ = doc(result.contents); - -// for (let col of ['red', 'yellow', 'blue']) { -// assert.equal($('#' + col).length, 1); -// } -// }); - -// Prettier('Ignores characters inside of line comments', async ({ runtime }) => { -// const result = await runtime.load('/line-comments'); -// assert.equal(result.statusCode, 200); - -// const $ = doc(result.contents); - -// for (let col of ['red', 'yellow', 'blue']) { -// assert.equal($('#' + col).length, 1); -// } -// }); - -// Prettier('Ignores characters inside of multiline comments', async ({ runtime }) => { -// const result = await runtime.load('/multiline-comments'); -// assert.equal(result.statusCode, 200); - -// const $ = doc(result.contents); +Prettier('can format an Astro file with frontmatter', async (ctx) => { + const [src, out] = await getFiles('frontmatter', ctx); + assert.not.equal(src, out); + + const formatted = format(src); + assert.equal(formatted, out); +}); -// for (let col of ['red', 'yellow', 'blue']) { -// assert.equal($('#' + col).length, 1); -// } -// }); +Prettier('can format an Astro file with embedded JSX expressions', async (ctx) => { + const [src, out] = await getFiles('embedded-expr', ctx); + assert.not.equal(src, out); + + const formatted = format(src); + assert.equal(formatted, out); +}); Prettier.run(); diff --git a/test/fixtures/astro-prettier/in/basic.astro b/test/fixtures/astro-prettier/in/basic.astro new file mode 100644 index 0000000000000..33be822261f9a --- /dev/null +++ b/test/fixtures/astro-prettier/in/basic.astro @@ -0,0 +1,13 @@ + + + + + + + Document + + +

+ Hello world!

+ + diff --git a/test/fixtures/astro-prettier/in/embedded-expr.astro b/test/fixtures/astro-prettier/in/embedded-expr.astro new file mode 100644 index 0000000000000..8a06dd9e8a323 --- /dev/null +++ b/test/fixtures/astro-prettier/in/embedded-expr.astro @@ -0,0 +1,23 @@ +--- + import Color from '../components/Color.jsx'; + + let title = + 'My Site'; + + const colors = ['red', 'yellow', 'blue',]; +--- + + + + + + + + My site + + +

{title}

+ + {colors.map(color =>
)} + + diff --git a/test/fixtures/astro-prettier/in/frontmatter.astro b/test/fixtures/astro-prettier/in/frontmatter.astro new file mode 100644 index 0000000000000..58cfcf6981684 --- /dev/null +++ b/test/fixtures/astro-prettier/in/frontmatter.astro @@ -0,0 +1,18 @@ +--- + import Color from '../components/Color.jsx'; + + let title = + 'My Site'; + + const colors = ['red', 'yellow', 'blue',]; +--- + + + + + My site + + +

{title}

+ + diff --git a/test/fixtures/astro-prettier/out/basic.astro b/test/fixtures/astro-prettier/out/basic.astro new file mode 100644 index 0000000000000..c77d2735bfa19 --- /dev/null +++ b/test/fixtures/astro-prettier/out/basic.astro @@ -0,0 +1,12 @@ + + + + + + + Document + + +

Hello world!

+ + diff --git a/test/fixtures/astro-prettier/out/embedded-expr.astro b/test/fixtures/astro-prettier/out/embedded-expr.astro new file mode 100644 index 0000000000000..4878e70cce4f2 --- /dev/null +++ b/test/fixtures/astro-prettier/out/embedded-expr.astro @@ -0,0 +1,22 @@ +--- +import Color from "../components/Color.jsx"; + +let title = "My Site"; + +const colors = ["red", "yellow", "blue"]; +--- + + + + My site + + +

{title}

+ + {colors.map((color) => ( +
+ +
+ ))} + + diff --git a/test/fixtures/astro-prettier/out/frontmatter.astro b/test/fixtures/astro-prettier/out/frontmatter.astro new file mode 100644 index 0000000000000..e2a9ad0ba0947 --- /dev/null +++ b/test/fixtures/astro-prettier/out/frontmatter.astro @@ -0,0 +1,16 @@ +--- +import Color from "../components/Color.jsx"; + +let title = "My Site"; + +const colors = ["red", "yellow", "blue"]; +--- + + + + My site + + +

{title}

+ + diff --git a/test/helpers.js b/test/helpers.js index 0ae3cbcaab9db..8ca42ea116bb4 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -24,8 +24,8 @@ export function setup(Suite, fixturePath) { } context.runtime = runtime; - context.readSrcFile = async (path) => { - const resolved = fileURLToPath(new URL(`${fixturePath}/astro/pages${path}`, import.meta.url)); + context.readFile = async (path) => { + const resolved = fileURLToPath(new URL(`${fixturePath}${path}`, import.meta.url)); return readFile(resolved).then(r => r.toString('utf-8')); } });