Skip to content

Commit

Permalink
✅ fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dudeofawesome committed May 1, 2024
1 parent 47a7204 commit 6e925b2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
42 changes: 18 additions & 24 deletions utils/testing/eslint/tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,33 @@ void describe('tests', () => {
}),
);

void it(`should fail when no rule fails`, async (ctx, a) => {
void it(`should fail when no rule fails`, async (ctx, cb) => {
await rejects(
Promise.resolve(
testRuleFail({
linter,
ruleId: 'no-console',
files: [{ code: `` }],
})(ctx, a),
),
testRuleFail({
linter,
ruleId: 'no-console',
files: [{ code: `` }],
})(ctx, cb)!,
);
});

void it(`should fail when wrong rule fails`, async (ctx, a) => {
void it(`should fail when wrong rule fails`, async (ctx, cb) => {
await rejects(
Promise.resolve(
testRuleFail({
linter,
ruleId: 'prettier/prettier',
files: [{ code: `console.log('');\n` }],
})(ctx, a),
),
testRuleFail({
linter,
ruleId: 'prettier/prettier',
files: [{ code: `console.log('');\n` }],
})(ctx, cb)!,
);
});

void it(`should fail when additional rule fails`, async (ctx, a) => {
void it(`should fail when additional rule fails`, async (ctx, cb) => {
await rejects(
Promise.resolve(
testRuleFail({
linter,
ruleId: 'no-console',
files: [{ code: `console.log('')` }],
})(ctx, a),
),
testRuleFail({
linter,
ruleId: 'no-console',
files: [{ code: `console.log('')` }],
})(ctx, cb)!,
);
});
});
Expand Down
27 changes: 13 additions & 14 deletions utils/testing/eslint/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ export function specificLintMessage(
);
}

function map_files(files: TestNoFailOpts['files']) {
return files.map((file) => ({
code: file.code.endsWith('\n') ? file.code : `${file.code}\n`,
path: file.path ?? filePath(file),
}));
}

interface TestRuleFailOpts extends TestNoFailOpts {
ruleId: string;
}
Expand All @@ -64,20 +71,15 @@ export function testRuleFail({
files,
}: TestRuleFailOpts): TestFn {
return async (ctx) => {
const _files = files.map((file) => ({
code: file.code,
path: file.path ?? filePath(file),
}));
const _files = map_files(files);

if (_files.length === 1 && _files[0] != null) {
const res = await linter.lintText(_files[0].code, {
filePath: _files[0].path,
});
const rules = res
.map((lint_result) => lint_result.messages.map((m) => m.ruleId))
.flat();
ok(rules.length > 0, `No lint failures detected.`);
deepStrictEqual(rules, new Array(rules.length).fill(ruleId));
await it(`${ctx.name} and have lint failures`, () => {
specificLintMessage(res, ruleId);
});
} else {
const created_files = await setup_tmp_dir(ctx, _files);

Expand All @@ -97,10 +99,7 @@ interface TestNoFailOpts {
}
export function testNoFail({ linter, files }: TestNoFailOpts): TestFn {
return async (ctx) => {
const _files = files.map((file) => ({
code: file.code,
path: file.path ?? filePath(file),
}));
const _files = map_files(files);

if (_files.length === 1 && _files[0] != null) {
const res = await linter.lintText(_files[0].code, {
Expand Down Expand Up @@ -145,7 +144,7 @@ export async function setup_tmp_dir(
ctx.before(async () => {
await Promise.all([
symlink(
join(cwd(), '../..', 'node_modules'),
join(cwd(), '..', '..', 'node_modules'),
join(tmp_dir, 'node_modules'),
),
]).catch((err: unknown) => console.error(err));
Expand Down

0 comments on commit 6e925b2

Please sign in to comment.