Skip to content

Commit

Permalink
Refactor tests for better watch mode (#170)
Browse files Browse the repository at this point in the history
* Refactor tests for better watch mode

* small refactor
  • Loading branch information
antonyfaris authored Apr 7, 2022
1 parent 9b95186 commit ce5b38a
Show file tree
Hide file tree
Showing 170 changed files with 375 additions and 122 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-pots-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-astro': patch
---

Refactor tests for better watch mode
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@typescript-eslint/no-this-alias": "off",
"no-console": "warn",
"no-shadow": "off",
"@typescript-eslint/no-shadow": ["error"]
"@typescript-eslint/no-shadow": ["error"],
"prettier/prettier": "warn"
}
}
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"build": "rollup -c",
"dev": "rollup -c -w",
"test": "vitest run",
"test:w": "vitest",
"test:w": "vitest -w",
"test:ui": "vitest --ui",
"release": "yarn build && changeset publish",
"lint": "eslint .",
"fix": "yarn lint --fix",
Expand All @@ -41,7 +42,7 @@
"@astrojs/compiler": "^0.13.1",
"prettier": "^2.4.1",
"sass-formatter": "^0.7.2",
"synckit": "^0.6.2"
"synckit": "^0.7.0"
},
"devDependencies": {
"@changesets/cli": "^2.16.0",
Expand All @@ -50,7 +51,7 @@
"@types/prettier": "^2.4.1",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.16.0",
"@vitest/ui": "^0.7.7",
"@vitest/ui": "^0.8.4",
"eslint": "^8.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
Expand All @@ -60,6 +61,6 @@
"ts-node": "^10.2.1",
"tslib": "^2.3.1",
"typescript": "^4.4.3",
"vitest": "^0.7.7"
"vitest": "^0.9.0"
}
}
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const require = createRequire(import.meta.url);
// the worker path must be absolute
const parse = createSyncFn(require.resolve('../workers/parse-worker.js'));

// do whatever you want, you will get the result synchronously!

export const languages: Partial<SupportLanguage>[] = [
{
name: 'astro',
Expand Down
2 changes: 0 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ const serialize = createSyncFn(
require.resolve('../workers/serialize-worker.js')
);

// import makeSynchronous from 'make-synchronous';

type ParserOptions = ParserOpts<anyNode>;
type AstPath = AstP<anyNode>;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
85 changes: 40 additions & 45 deletions test/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import prettier from 'prettier';
import { fileURLToPath } from 'url';
import { promises as fs } from 'fs';
import { expect, it } from 'vitest';

// workaround for vitest to watch for the files
const srcFiles = import.meta.glob('/src/*.ts', {
assert: { type: 'raw' },
});

/**
* format the contents of an astro file
*/
Expand Down Expand Up @@ -45,85 +49,76 @@ function markdownFormat(
return '';
}

async function readFile(path: string) {
const res = await fs.readFile(
fileURLToPath(new URL(`./fixtures${path}`, import.meta.url).toString())
);
return res.toString().replace(/\r\n/g, '\n');
}

/**
* Utility to get `[src, out]` files
* Utility to get `[input, output]` files
*/
async function getFiles(name: string) {
const [src, out] = await Promise.all([
readFile(`/${name}/input.astro`),
readFile(`/${name}/output.astro`),
]);
return [src, out];
function getFiles(file: any, str: string, md?: string) {
const ext = md === 'markdown' ? 'md' : 'astro';
let input: string = file[`/test/fixtures/${str}/input.${ext}`];
let output: string = file[`/test/fixtures/${str}/output.${ext}`];
// workaround: normalize end of lines to pass windows ci
input = input.replace(/(\r\n|\r)/gm, '\n');
output = output.replace(/(\r\n|\r)/gm, '\n');
return { input, output };
}

async function getOptions(name: string) {
let options: object;
function getOptions(files: any, folderName: string) {
let opts: object;
try {
options = JSON.parse(await readFile(`/${name}/options.json`));
opts = JSON.parse(files[`/test/fixtures/${folderName}/options.json`]);
} catch (e) {
options = {};
opts = {};
}
return options;
}

async function getMarkdownFiles(name: string) {
const [src, out] = await Promise.all([
readFile(`/${name}/input.md`),
readFile(`/${name}/output.md`),
]);
return [src, out];
return opts;
}

type Mode = {
mode: 'default' | 'unaltered' | 'markdown';
type Options = {
mode?: 'default' | 'unaltered' | 'markdown';
};

/**
* @param {string} name Test name.
* @param {string} folder Folder of the input/output.
* @param {Mode} [{ mode }={ mode: 'default' }]
* @param {any} files Files from import.meta.glob.
* @param {string} folderName Folder name.
* @param {Options} [{ mode }={ mode: 'default' }]
*
* unaltered: input and output should be the same
*
* markdown: for markdown files
*/
export function test(
name: string,
folder: string,
{ mode }: Mode = { mode: 'default' }
files: any,
folderName: string,
{ mode }: Options = { mode: 'default' }
) {
it(name, async () => {
const getFiles_ = mode === 'markdown' ? getMarkdownFiles : getFiles;
const [src, out] = await getFiles_(folder);
const { input, output } = getFiles(files, folderName, mode);

expect(input, 'Missing input file').to.not.be.undefined;
expect(output, 'Missing output file').to.not.be.undefined;

if (mode === 'unaltered') {
expect(
src,
input,
'Unformated file and formated file are not the same'
).to.be.equal(out);
).to.be.equal(output);
} else {
expect(
src,
input,
'Unformated file and formated file are the same'
).to.not.be.equal(out);
).to.not.be.equal(output);
}

const options = await getOptions(folder);

const formatFile = mode === 'markdown' ? markdownFormat : format;

const formatted = formatFile(src, options);
expect(formatted, 'Incorrect formating').toBe(out);
const opts = getOptions(files, folderName);

const formatted = formatFile(input, opts);
expect(formatted, 'Incorrect formating').toBe(output);

// test that our formatting is idempotent
const formattedTwice = formatFile(formatted, options);
const formattedTwice = formatFile(formatted, opts);
expect(formatted, 'Formatting is not idempotent').toBe(formattedTwice);
});
}
9 changes: 7 additions & 2 deletions test/tests/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { test } from '../test-utils';

test('can format a basic astro file', 'basic');
const files = import.meta.glob('/test/fixtures/basic/*/*', {
assert: { type: 'raw' },
});

test('can format a basic astro file', files, 'basic/basic-html');

test(
'can format an Astro file with a single style element',
'single-style-element'
files,
'basic/single-style-element'
);
20 changes: 17 additions & 3 deletions test/tests/markdown.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { test } from '../test-utils';

const files = import.meta.glob('/test/fixtures/markdown/*/*', {
assert: { type: 'raw' },
});

// *** MARKDOWN ***
// test('can format an Astro file containing an Astro file embedded in a codeblock', PrettierMarkdown, 'embedded-in-markdown');
// test(
// 'can format an Astro file containing an Astro file embedded in a codeblock',
// files,
// 'markdown/embedded-in-markdown',
// { mode: 'markdown' }
// );

// test(`Can format an Astro file with a codespan inside <Markdown/>`, 'with-codespans');
test(
'Can format an Astro file with a codespan inside <Markdown/>',
files,
'markdown/with-codespans'
);

test(
'Can format the content of a markdown component as markdown',
'markdown-component-content'
files,
'markdown/markdown-component-content'
);

// test.todo("Don't escape '*' inside markdown");
Expand Down
Loading

0 comments on commit ce5b38a

Please sign in to comment.