Skip to content

Commit

Permalink
chore(tests): upgrade ava from v3 to v6
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhaenisch committed Jul 2, 2024
1 parent b253038 commit 59c5329
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 90 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"devDependencies": {
"@types/node": "20.14.9",
"@vue/language-plugin-pug": "2.0.24",
"ava": "3.15.0",
"ava": "6.1.3",
"prettier": "3.3.2",
"typescript": "5.5.2",
"vue-tsc": "2.0.24"
Expand Down
38 changes: 17 additions & 21 deletions test/_utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const prettier = require('prettier');
const { macro } = require('ava').default;

/**
* @param {string} code
Expand All @@ -10,25 +11,20 @@ module.exports.prettify = async (code, options) =>
/**
* @param {prettier.Options['parser']} parser
*/
module.exports.getMacro = (parser) => {
/**
* @param {import('ava').Assertions} t
* @param {string} input
* @param {string} expected
* @param {object} options
* @param {prettier.Options} [options.options]
* @param {(res: string) => string} [options.transformer]
*/
async function macro(t, input, expected, { options = {}, transformer = (res) => res.split('\n')[0] } = {}) {
const formattedCode = await module.exports.prettify(input, { parser, ...options });
module.exports.getMacro = (parser) =>
macro({
/**
* @param {import('ava').ExecutionContext} t
* @param {string} input
* @param {string} expected
* @param {{ options?: import('prettier').Options; transformer?: (res: string) => string }} [options]
*/
async exec(t, input, expected, { options = {}, transformer = (res) => res.split('\n')[0] } = {}) {
const formattedCode = await module.exports.prettify(input, { parser, ...options });

t.is(transformer(formattedCode), expected);
}

/**
* @param {string} title
*/
macro.title = (title) => `[${parser}] ${title}`;

return macro;
};
t.is(transformer(formattedCode), expected);
},
title(providedTitle = '<missing-title>') {
return `[${parser}] ${providedTitle}`;
},
});
135 changes: 67 additions & 68 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,73 @@
const { getMacro, prettify } = require('./_utils');
const test = require('ava').default;

/**
* @type {import('ava').OneOrMoreMacros<[string, string] | [string, string, { options?: import('prettier').Options, transformer?: (res: string) => string }], unknown>}
*/
const macros = [getMacro('typescript'), getMacro('babel'), getMacro('babel-ts')];

test(
'sorts imports',
macros,
`
import { foo, bar } from "foobar"
export const foobar = foo + bar
`,
'import { bar, foo } from "foobar";',
);

test(
'removes partially unused imports',
macros,
`
import { foo, bar, baz } from "foobar";
const foobar = foo + baz
`,
'import { baz, foo } from "foobar";',
);

test('removes completely unused imports', macros, 'import { foo } from "foobar"', '');

test(
'works with multi-line imports',
macros,
`
import {
foo,
bar,
baz,
} from "foobar";
console.log({ foo, bar, baz });
`,
'import { bar, baz, foo } from "foobar";',
);

test(
'works without a filepath',
macros,
`
import { foo, bar } from "foobar"
export const foobar = foo + bar
`,
'import { bar, foo } from "foobar";',
{ options: { filepath: undefined } },
);

test(
'files with `// organize-imports-ignore` are skipped',
macros,
`
// organize-imports-ignore
import { foo, bar } from "foobar"
export const foobar = foo + bar
`,
'import { foo, bar } from "foobar";',
{ transformer: (res) => res.split('\n')[1] },
);
const macros = ['typescript', 'babel', 'babel-ts'].map((parser) => getMacro(parser));

for (const macro of macros) {
test(
'sorts imports',
macro,
`
import { foo, bar } from "foobar"
export const foobar = foo + bar
`,
'import { bar, foo } from "foobar";',
);

test(
'removes partially unused imports',
macro,
`
import { foo, bar, baz } from "foobar";
const foobar = foo + baz
`,
'import { baz, foo } from "foobar";',
);

test('removes completely unused imports', macro, 'import { foo } from "foobar"', '');

test(
'works with multi-line imports',
macro,
`
import {
foo,
bar,
baz,
} from "foobar";
console.log({ foo, bar, baz });
`,
'import { bar, baz, foo } from "foobar";',
);

test(
'works without a filepath',
macro,
`
import { foo, bar } from "foobar"
export const foobar = foo + bar
`,
'import { bar, foo } from "foobar";',
{ options: { filepath: undefined } },
);

test(
'files with `// organize-imports-ignore` are skipped',
macro,
`
// organize-imports-ignore
import { foo, bar } from "foobar"
export const foobar = foo + bar
`,
'import { foo, bar } from "foobar";',
{ transformer: (res) => res.split('\n')[1] },
);
}

test('skips when formatting a range', async (t) => {
const code = 'import { foo } from "./bar";';
Expand Down

0 comments on commit 59c5329

Please sign in to comment.