Skip to content

Commit 2693b56

Browse files
authored
feat: new vite-friendly /utils exports (#595)
* first pass - introduces typescript config - converts a bunch of files to typescript - creates a new export object that exposes utils directly - adds `ts-loader` so webpack can handle typescript - moves to vitest for test runner * chore: see if this import alias will get tests passing * refactor: slightly cleaner test cleanup * chore: fix up export path * chore: tsconfig tweak * chore: convert yet another file to TS * chore: test script tweak * Update vitest.setup.ts * ci: rework build process a bit * chore: try tweaking exports object * Update .eslintignore * chore: remove redundant rimraf * chore: attempt to revert some TS conversions to see if that fixes things * fix: don't optimize deps, transpile only this gets the legacy bundles working (i think) * fix: don't `--write` during test step * chore: reconvert TS since that wasn't actually the problem * chore: shift around for better diff * chore: run it back * Revert "chore: run it back" This reverts commit 3226d1a. * revert: actually we don't need that top-level describe * chore: deps cleanup - use native node.js glob - remove prettier@2 - add in @csstools/css-parser-algorithms so tests can work in CI? * chore!: bump min node version to 22 * chore: remove identity-obj-proxy * chore: remove `defaultProps` to address warning * chore: install knip, rip out a few things * chore: format command * chore: suppress vitest lint warnings
1 parent 280a1dd commit 2693b56

27 files changed

+3361
-1138
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
node_modules/
22
coverage/
33
dist/
4+
dist-utils/
45
public/index*
56
__tests__/__fixtures__/**/sample.*

.eslintrc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{
22
"extends": ["@readme/eslint-config", "@readme/eslint-config/react"],
3-
"root": true
3+
"overrides": [
4+
{
5+
"files": ["**/*.ts", "**/*.tsx"],
6+
"extends": ["@readme/eslint-config/typescript"],
7+
},
8+
],
9+
"root": true,
410
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dist/
2+
dist-utils/
23
node_modules/
34
coverage/

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ __tests__/
33
coverage/
44
.eslint*
55
.prettier*
6-
jest.config.js
6+
vitest*

__tests__/.eslintrc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"extends": [
3-
"@readme/eslint-config/testing/jest",
3+
"@readme/eslint-config/testing/vitest",
44
"@readme/eslint-config/testing/jest-dom",
5-
"@readme/eslint-config/testing/react"
6-
]
5+
"@readme/eslint-config/testing/react",
6+
"@readme/eslint-config/typescript",
7+
],
78
}

__tests__/__snapshots__/codeMirror.test.js.snap renamed to __tests__/__snapshots__/codeMirror.test.ts.snap

Lines changed: 47 additions & 47 deletions
Large diffs are not rendered by default.

__tests__/codeEditor.test.js renamed to __tests__/codeEditor.test.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
import { render, screen, waitFor } from '@testing-library/react';
22
import React from 'react';
3+
import { afterEach, beforeEach, describe, expect, vi, it } from 'vitest';
34

45
import CodeEditor from '../src/codeEditor';
56

67
describe('<CodeEditor/>', () => {
7-
const getClientRectSpy = jest.fn(() => ({ width: 100 }));
8-
Range.prototype.getBoundingClientRect = getClientRectSpy;
9-
Range.prototype.getClientRects = getClientRectSpy;
8+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9+
let originalGetBoundingClientRect: any;
10+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11+
let originalGetClientRects: any;
12+
// eslint-disable-next-line @vitest/require-mock-type-parameters
13+
const getClientRectSpy = vi.fn(() => ({ width: 100 }));
14+
15+
beforeEach(() => {
16+
originalGetBoundingClientRect = Range.prototype.getBoundingClientRect;
17+
originalGetClientRects = Range.prototype.getClientRects;
18+
// @ts-expect-error mock types
19+
Range.prototype.getBoundingClientRect = getClientRectSpy;
20+
// @ts-expect-error mock types
21+
Range.prototype.getClientRects = getClientRectSpy;
22+
});
23+
24+
afterEach(() => {
25+
Range.prototype.getBoundingClientRect = originalGetBoundingClientRect;
26+
Range.prototype.getClientRects = originalGetClientRects;
27+
getClientRectSpy.mockRestore();
28+
});
1029

1130
it('should display a <CodeEditor> element', () => {
1231
render(<CodeEditor code="console.log('Hello, world.');" lang="javascript" />);
@@ -35,7 +54,7 @@ describe('<CodeEditor/>', () => {
3554
expect(screen.getByText('console')).toHaveClass('cm-variable');
3655
});
3756

38-
it.skip('should set new language via props', () => {
57+
it.todo('should set new language via props', () => {
3958
const { rerender } = render(<CodeEditor code="console.log('Hello, world.');" />);
4059
expect(screen.getByText("console.log('Hello, world.');")).toBeVisible();
4160

__tests__/codeMirror.test.js renamed to __tests__/codeMirror.test.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* eslint-disable testing-library/no-node-access */
22
/* eslint-disable testing-library/no-container */
3-
import { promises as fs } from 'fs';
3+
import { promises as fs, globSync } from 'fs';
44
import path from 'path';
55

66
// eslint-disable-next-line testing-library/no-manual-cleanup
7-
import { render, screen, cleanup } from '@testing-library/react';
8-
import { globSync } from 'glob';
7+
import { cleanup, render, screen } from '@testing-library/react';
8+
import { beforeAll, beforeEach, describe, expect, it, vi, test } from 'vitest';
99

1010
import syntaxHighlighter, { uppercase, canonical } from '../src';
1111

@@ -51,6 +51,7 @@ test('should work with modes', () => {
5151
});
5252

5353
test('should keep trailing json bracket if highlightMode is enabled', () => {
54+
// @ts-expect-error this component's types are currently ill-defined
5455
render(syntaxHighlighter('{ "a": 1 }', 'json', { highlightMode: true }));
5556

5657
expect(screen.getByTestId('CodeMirror').outerHTML).toBe(
@@ -59,27 +60,32 @@ test('should keep trailing json bracket if highlightMode is enabled', () => {
5960
});
6061

6162
test('should have a dark theme', () => {
63+
// @ts-expect-error this component's types are currently ill-defined
6264
render(syntaxHighlighter('{ "a": 1 }', 'json', { dark: true }));
6365
expect(screen.getByTestId('SyntaxHighlighter')).toHaveClass('cm-s-material-palenight');
6466
});
6567

6668
describe('variable substitution', () => {
6769
it('should tokenize variables (double quotes)', () => {
70+
// @ts-expect-error this component's types are currently ill-defined
6871
render(syntaxHighlighter('"<<apiKey>>"', 'json', { tokenizeVariables: true }));
6972
expect(screen.getByText('APIKEY')).toBeVisible();
7073
});
7174

7275
it('should tokenize variables (single quotes)', () => {
76+
// @ts-expect-error this component's types are currently ill-defined
7377
render(syntaxHighlighter("'<<apiKey>>'", 'json', { tokenizeVariables: true }));
7478
expect(screen.getByText('APIKEY')).toBeVisible();
7579
});
7680

7781
it('should keep enclosing characters around the variable', () => {
82+
// @ts-expect-error this component's types are currently ill-defined
7883
render(syntaxHighlighter("'<<apiKey>>'", 'json', { tokenizeVariables: true }));
7984
expect(screen.getByTestId('SyntaxHighlighter')).toHaveTextContent("'APIKEY'");
8085
});
8186

8287
it('should tokenize variables outside of quotes', () => {
88+
// @ts-expect-error this component's types are currently ill-defined
8389
render(syntaxHighlighter('<<apiKey>>', 'json', { tokenizeVariables: true }));
8490
expect(screen.getByText('APIKEY')).toBeVisible();
8591
});
@@ -92,38 +98,45 @@ describe('variable substitution', () => {
9298
fetch({ foo, bar, baz: <<token>> });
9399
`;
94100

101+
// @ts-expect-error this component's types are currently ill-defined
95102
render(syntaxHighlighter(codeBlock, 'json', { tokenizeVariables: true }));
96103
expect(screen.getByTestId('SyntaxHighlighter').textContent).toMatchSnapshot();
97104
});
98105

99106
it('should tokenize multiple variables per line', () => {
107+
// @ts-expect-error this component's types are currently ill-defined
100108
render(syntaxHighlighter('<<apiKey>> <<name>>', 'json', { tokenizeVariables: true }));
101109
expect(screen.getByTestId('SyntaxHighlighter')).toHaveTextContent('APIKEY NAME');
102110
});
103111

104112
it.each(['\\<<wat>>', '<<wat\\>>', '\\<<wat\\>>'])('should NOT tokenize escaped variables %s', code => {
113+
// @ts-expect-error this component's types are currently ill-defined
105114
render(syntaxHighlighter(code, 'json', { tokenizeVariables: true }));
106115
expect(screen.getByTestId('SyntaxHighlighter')).toHaveTextContent('<<wat>>');
107116
});
108117
});
109118

110119
describe('variable substitution { mdx: true }', () => {
111120
it('should tokenize variables (double quotes)', () => {
121+
// @ts-expect-error this component's types are currently ill-defined
112122
render(syntaxHighlighter('"{user.apiKey}"', 'json', { tokenizeVariables: true }, { mdx: true }));
113123
expect(screen.getByText('APIKEY')).toBeVisible();
114124
});
115125

116126
it('should tokenize variables (single quotes)', () => {
127+
// @ts-expect-error this component's types are currently ill-defined
117128
render(syntaxHighlighter("'{user.apiKey}'", 'json', { tokenizeVariables: true }, { mdx: true }));
118129
expect(screen.getByText('APIKEY')).toBeVisible();
119130
});
120131

121132
it('should keep enclosing characters around the variable', () => {
133+
// @ts-expect-error this component's types are currently ill-defined
122134
render(syntaxHighlighter("'{user.apiKey}'", 'json', { tokenizeVariables: true }, { mdx: true }));
123135
expect(screen.getByTestId('SyntaxHighlighter')).toHaveTextContent("'APIKEY'");
124136
});
125137

126138
it('should tokenize variables outside of quotes', () => {
139+
// @ts-expect-error this component's types are currently ill-defined
127140
render(syntaxHighlighter('{user.apiKey}', 'json', { tokenizeVariables: true }, { mdx: true }));
128141
expect(screen.getByText('APIKEY')).toBeVisible();
129142
});
@@ -136,6 +149,7 @@ describe('variable substitution { mdx: true }', () => {
136149
fetch({ foo, bar, baz: {user.token} });
137150
`;
138151

152+
// @ts-expect-error this component's types are currently ill-defined
139153
render(syntaxHighlighter(codeBlock, 'json', { tokenizeVariables: true }, { mdx: true }));
140154
expect(screen.getByTestId('SyntaxHighlighter').textContent).toMatchInlineSnapshot(`
141155
"
@@ -148,16 +162,19 @@ describe('variable substitution { mdx: true }', () => {
148162
});
149163

150164
it('should tokenize multiple variables per line', () => {
165+
// @ts-expect-error this component's types are currently ill-defined
151166
render(syntaxHighlighter('{user.apiKey} {user.name}', 'json', { tokenizeVariables: true }, { mdx: true }));
152167
expect(screen.getByTestId('SyntaxHighlighter')).toHaveTextContent('APIKEY NAME');
153168
});
154169

155170
it('should not tokenize bracket style', () => {
171+
// @ts-expect-error this component's types are currently ill-defined
156172
render(syntaxHighlighter('<<wat>>', 'json', { tokenizeVariables: true }, { mdx: true }));
157173
expect(screen.getByTestId('SyntaxHighlighter')).toHaveTextContent('<<wat>>');
158174
});
159175

160176
it.each(['\\{user.wat}', '{user.wat\\}', '\\{user.wat\\}'])('should NOT tokenize escaped variables %s', code => {
177+
// @ts-expect-error this component's types are currently ill-defined
161178
render(syntaxHighlighter(code, 'json', { tokenizeVariables: true }, { mdx: true }));
162179
expect(screen.getByTestId('SyntaxHighlighter')).toHaveTextContent('{user.wat}');
163180
});
@@ -169,9 +186,9 @@ describe('Supported languages', () => {
169186
});
170187

171188
describe.each(languages)('%s', (language, fixtureDir) => {
172-
let testCase;
189+
let testCase: string;
173190

174-
// eslint-disable-next-line global-require, import/no-dynamic-require
191+
// eslint-disable-next-line global-require, import/no-dynamic-require, @typescript-eslint/no-require-imports
175192
const instructions = require(path.join(fixtureDir, 'index.js'));
176193

177194
beforeEach(async () => {
@@ -253,6 +270,7 @@ describe('highlight mode', () => {
253270
render(
254271
syntaxHighlighter(code, 'curl', {
255272
dark: true,
273+
// @ts-expect-error this component's types are currently ill-defined
256274
highlightMode: true,
257275
tokenizeVariables: true,
258276
ranges: [
@@ -293,6 +311,7 @@ describe('runmode', () => {
293311
render(
294312
syntaxHighlighter(code, 'c', {
295313
dark: true,
314+
// @ts-expect-error this component's types are currently ill-defined
296315
highlightMode: true,
297316
tokenizeVariables: true,
298317
ranges: [
@@ -315,9 +334,12 @@ describe('code folding', () => {
315334
document.createRange = () => {
316335
const range = new Range();
317336

318-
range.getBoundingClientRect = jest.fn();
337+
// eslint-disable-next-line @vitest/require-mock-type-parameters
338+
range.getBoundingClientRect = vi.fn();
319339

320-
range.getClientRects = jest.fn(() => ({
340+
// @ts-expect-error this component's types are currently ill-defined
341+
// eslint-disable-next-line @vitest/require-mock-type-parameters
342+
range.getClientRects = vi.fn(() => ({
321343
item: () => null,
322344
length: 0,
323345
}));
@@ -328,6 +350,7 @@ describe('code folding', () => {
328350

329351
it('renders folders in the gutter', () => {
330352
const { container } = render(
353+
// @ts-expect-error this component's types are currently ill-defined
331354
syntaxHighlighter('{ "a": { "b": { "c": 1 } }', 'json', { foldGutter: true, readOnly: true }),
332355
);
333356

__tests__/jest.setup.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

__tests__/uppercase.test.js renamed to __tests__/uppercase.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { expect, test } from 'vitest';
2+
13
import { uppercase } from '../src';
24

35
test('should uppercase known languages', () => {

0 commit comments

Comments
 (0)