diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/astro-prettier.test.js | 44 | ||||
-rw-r--r-- | test/fixtures/astro-prettier/in/basic.astro | 13 | ||||
-rw-r--r-- | test/fixtures/astro-prettier/in/embedded-expr.astro | 26 | ||||
-rw-r--r-- | test/fixtures/astro-prettier/in/frontmatter.astro | 18 | ||||
-rw-r--r-- | test/fixtures/astro-prettier/out/basic.astro | 12 | ||||
-rw-r--r-- | test/fixtures/astro-prettier/out/embedded-expr.astro | 24 | ||||
-rw-r--r-- | test/fixtures/astro-prettier/out/frontmatter.astro | 16 | ||||
-rw-r--r-- | test/helpers.js | 7 | ||||
-rw-r--r-- | test/test-utils.js | 13 |
9 files changed, 172 insertions, 1 deletions
diff --git a/test/astro-prettier.test.js b/test/astro-prettier.test.js new file mode 100644 index 000000000..1dd1887f4 --- /dev/null +++ b/test/astro-prettier.test.js @@ -0,0 +1,44 @@ +import { suite } from 'uvu'; +import * as assert from 'uvu/assert'; +import { format } from './test-utils.js'; +import { setup } from './helpers.js'; + +const Prettier = suite('Prettier formatting'); + +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('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); +}); + +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 000000000..33be82226 --- /dev/null +++ b/test/fixtures/astro-prettier/in/basic.astro @@ -0,0 +1,13 @@ +<!DOCTYPE html> + <html lang="en"> +<head> + <meta charset="UTF-8"> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Document</title> +</head> + <body> + <h1> + Hello world!</h1> + </body> +</html> 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 000000000..842da6578 --- /dev/null +++ b/test/fixtures/astro-prettier/in/embedded-expr.astro @@ -0,0 +1,26 @@ +--- + import Color from '../components/Color.jsx'; + + let title = + 'My Site'; + + const colors = ['red', 'yellow', 'blue',]; +--- + + + + + +<html lang="en"> + <head> + <title>My site</title> + </head> + <body> + <h1>{title}</h1> + + {"I'm some super long text and oh boy I sure do hope this formatter doesn't break me!"} + + {colors.map(color => ( + <div><Color name={color} /></div>))} + </body> +</html> diff --git a/test/fixtures/astro-prettier/in/frontmatter.astro b/test/fixtures/astro-prettier/in/frontmatter.astro new file mode 100644 index 000000000..58cfcf698 --- /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',]; +--- + + +<html lang="en"> + <head> + <title>My site</title> + </head> + <body> + <h1>{title}</h1> + </body> +</html> diff --git a/test/fixtures/astro-prettier/out/basic.astro b/test/fixtures/astro-prettier/out/basic.astro new file mode 100644 index 000000000..c77d2735b --- /dev/null +++ b/test/fixtures/astro-prettier/out/basic.astro @@ -0,0 +1,12 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="UTF-8" /> + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <title>Document</title> + </head> + <body> + <h1>Hello world!</h1> + </body> +</html> 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 000000000..0ae01ff3f --- /dev/null +++ b/test/fixtures/astro-prettier/out/embedded-expr.astro @@ -0,0 +1,24 @@ +--- +import Color from "../components/Color.jsx"; + +let title = "My Site"; + +const colors = ["red", "yellow", "blue"]; +--- + +<html lang="en"> + <head> + <title>My site</title> + </head> + <body> + <h1>{title}</h1> + + {"I'm some super long text and oh boy I sure do hope this formatter doesn't break me!"} + + {colors.map((color) => ( + <div> + <Color name={color} /> + </div> + ))} + </body> +</html> diff --git a/test/fixtures/astro-prettier/out/frontmatter.astro b/test/fixtures/astro-prettier/out/frontmatter.astro new file mode 100644 index 000000000..e2a9ad0ba --- /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"]; +--- + +<html lang="en"> + <head> + <title>My site</title> + </head> + <body> + <h1>{title}</h1> + </body> +</html> diff --git a/test/helpers.js b/test/helpers.js index 6d6904004..8ca42ea11 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -1,7 +1,8 @@ import { fileURLToPath } from 'url'; import { build as astroBuild } from '../lib/build.js'; -import { loadConfig } from '../lib/config.js'; +import { readFile } from 'fs/promises'; import { createRuntime } from '../lib/runtime.js'; +import { loadConfig } from '../lib/config.js'; import * as assert from 'uvu/assert'; /** setup fixtures for tests */ export function setup(Suite, fixturePath) { @@ -23,6 +24,10 @@ export function setup(Suite, fixturePath) { } context.runtime = runtime; + context.readFile = async (path) => { + const resolved = fileURLToPath(new URL(`${fixturePath}${path}`, import.meta.url)); + return readFile(resolved).then(r => r.toString('utf-8')); + } }); Suite.after(async () => { diff --git a/test/test-utils.js b/test/test-utils.js index 93bf28788..5d5182636 100644 --- a/test/test-utils.js +++ b/test/test-utils.js @@ -1,5 +1,18 @@ import cheerio from 'cheerio'; +import prettier from 'prettier'; +import { fileURLToPath } from 'url'; /** load html */ export function doc(html) { return cheerio.load(html); } + +/** + * format the contents of an astro file + * @param contents {string} + */ +export function format(contents) { + return prettier.format(contents, { + parser: 'astro', + plugins: [fileURLToPath(new URL('../prettier-plugin-astro', import.meta.url))] + }) +} |