Skip to content

Commit

Permalink
chore: Replace eslint-expect-type with vitest type tests (#4323)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 authored Dec 25, 2024
1 parent c879058 commit e4ecc99
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 86 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@
"overrides": [
{
"files": "*.ts",
"plugins": ["expect-type"],
"extends": [
"plugin:expect-type/recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"plugin:@typescript-eslint/stylistic-type-checked",
Expand Down
55 changes: 0 additions & 55 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@
"@vitest/coverage-v8": "^2.1.8",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-expect-type": "^0.6.2",
"eslint-plugin-jsdoc": "^50.6.1",
"eslint-plugin-n": "^17.15.1",
"eslint-plugin-unicorn": "^56.0.1",
Expand Down
117 changes: 89 additions & 28 deletions src/api/extract.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from 'vitest';
import { describe, it, expect, expectTypeOf } from 'vitest';
import * as fixtures from '../__fixtures__/fixtures.js';
import { load } from '../load-parse.js';

Expand All @@ -15,57 +15,86 @@ interface RedSelMultipleObject {
describe('$.extract', () => {
it('() : should extract values for selectors', () => {
const $ = load(fixtures.eleven);
const $root = load(fixtures.eleven).root();
// An empty object should lead to an empty extraction.
const $root = $.root();

// $ExpectType ExtractedMap<{}>
// An empty object should lead to an empty extraction.
expectTypeOf($root.extract({})).toEqualTypeOf<Record<never, never>>();
const emptyExtract = $root.extract({});
expect(emptyExtract).toStrictEqual({});
// Non-existent values should be undefined.

// $ExpectType ExtractedMap<{ foo: string; }>
// Non-existent values should be undefined.
expectTypeOf($root.extract({ foo: 'bar' })).toEqualTypeOf<{
foo: string | undefined;
}>();
const simpleExtract = $root.extract({ foo: 'bar' });
expect(simpleExtract).toStrictEqual({ foo: undefined });

// Existing values should be extracted.
expect<{ red: string | undefined }>(
$root.extract({ red: '.red' }),
).toStrictEqual({
red: 'Four',
});
expect<RedSelObject>(
expectTypeOf($root.extract({ red: '.red' })).toEqualTypeOf<{
red: string | undefined;
}>();
expect($root.extract({ red: '.red' })).toStrictEqual({ red: 'Four' });

expectTypeOf(
$root.extract({ red: '.red', sel: '.sel' }),
).toStrictEqual({
).toEqualTypeOf<RedSelObject>();
expect($root.extract({ red: '.red', sel: '.sel' })).toStrictEqual({
red: 'Four',
sel: 'Three',
});
// Descriptors for extractions should be supported
expect<RedSelObject>(

// Descriptors for extractions should be supported.
expectTypeOf(
$root.extract({
red: { selector: '.red' },
sel: { selector: '.sel' },
}),
).toEqualTypeOf<RedSelObject>();
expect(
$root.extract({
red: { selector: '.red' },
sel: { selector: '.sel' },
}),
).toStrictEqual({ red: 'Four', sel: 'Three' });
// Should support extraction of multiple values.

// $ExpectType ExtractedMap<{ red: [string]; sel: [string]; }>
// Should support extraction of multiple values.
expectTypeOf(
$root.extract({
red: ['.red'],
sel: ['.sel'],
}),
).toEqualTypeOf<{ red: string[]; sel: string[] }>();
const multipleExtract = $root.extract({
red: ['.red'],
sel: ['.sel'],
});
expect<RedSelMultipleObject>(multipleExtract).toStrictEqual({
expectTypeOf(multipleExtract).toEqualTypeOf<RedSelMultipleObject>();
expect(multipleExtract).toStrictEqual({
red: ['Four', 'Five', 'Nine'],
sel: ['Three', 'Nine', 'Eleven'],
});

// Should support custom `prop`s.
expect<RedSelObject>(
expectTypeOf(
$root.extract({
red: { selector: '.red', value: 'outerHTML' },
sel: { selector: '.sel', value: 'tagName' },
}),
).toEqualTypeOf<RedSelObject>();
expect(
$root.extract({
red: { selector: '.red', value: 'outerHTML' },
sel: { selector: '.sel', value: 'tagName' },
}),
).toStrictEqual({ red: '<li class="red">Four</li>', sel: 'LI' });

// Should support custom `prop`s for multiple values.
expect<{ red: string[] }>(
expectTypeOf(
$root.extract({
red: [{ selector: '.red', value: 'outerHTML' }],
}),
).toEqualTypeOf<{ red: string[] }>();
expect(
$root.extract({
red: [{ selector: '.red', value: 'outerHTML' }],
}),
Expand All @@ -76,17 +105,37 @@ describe('$.extract', () => {
'<li class="red sel">Nine</li>',
],
});

// Should support custom extraction functions.
expect<{ red: string | undefined }>(
expectTypeOf(
$root.extract({
red: {
selector: '.red',
value: (el, key) => `${key}=${$(el).text()}`,
},
}),
).toEqualTypeOf<{ red: string | undefined }>();
expect(
$root.extract({
red: {
selector: '.red',
value: (el, key) => `${key}=${$(el).text()}`,
},
}),
).toStrictEqual({ red: 'red=Four' });

// Should support custom extraction functions for multiple values.
expect<{ red: string[] }>(
expectTypeOf(
$root.extract({
red: [
{
selector: '.red',
value: (el, key) => `${key}=${$(el).text()}`,
},
],
}),
).toEqualTypeOf<{ red: string[] }>();
expect(
$root.extract({
red: [
{
Expand All @@ -96,9 +145,21 @@ describe('$.extract', () => {
],
}),
).toStrictEqual({ red: ['red=Four', 'red=Five', 'red=Nine'] });
// Should support extraction objects

// $ExpectType ExtractedMap<{ section: { selector: string; value: { red: string; sel: string; }; }; }>
// Should support extraction objects.
expectTypeOf(
$root.extract({
section: {
selector: 'ul:nth(1)',
value: {
red: '.red',
sel: '.blue',
},
},
}),
).toEqualTypeOf<{
section: { red: string | undefined; sel: string | undefined } | undefined;
}>();
const subExtractObject = $root.extract({
section: {
selector: 'ul:nth(1)',
Expand All @@ -108,10 +169,10 @@ describe('$.extract', () => {
},
},
});

expect<{ section: RedSelObject | undefined }>(
subExtractObject,
).toStrictEqual({
expectTypeOf(subExtractObject).toEqualTypeOf<{
section: RedSelObject | undefined;
}>();
expect(subExtractObject).toStrictEqual({
section: {
red: 'Five',
sel: 'Seven',
Expand Down
4 changes: 4 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const config: ViteUserConfig = defineConfig({
'*.config.ts',
],
},
typecheck: {
enabled: true,
include: ['src/api/extract.spec.ts'],
},
},
});

Expand Down

0 comments on commit e4ecc99

Please sign in to comment.