-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostProcess.test.js
More file actions
115 lines (86 loc) · 3.05 KB
/
Copy pathpostProcess.test.js
File metadata and controls
115 lines (86 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import {describe, expect, it} from 'vitest';
import {postProcessFile} from '../src/postProcess.js';
describe('postProcess', () => {
it('should format TypeScript with prettier', async () => {
const unformatted = `const x={a:1,b:2};function hello(){return"world"}`;
const result = await postProcessFile('test.ts', unformatted, {
prettier: true,
});
expect(result).toMatchSnapshot();
});
it('should transpile TypeScript to JavaScript', async () => {
const typescript = `const greet = (name: string): string => {
return \`Hello, \${name}!\`;
};`;
const result = await postProcessFile('utils.ts', typescript, {
transpileToJS: true,
});
expect(result).toMatchSnapshot();
});
it('should transpile TSX to JSX', async () => {
const tsx = `import React from 'react';
export const Component: React.FC<{title: string}> = ({title}) => {
return <div>{title}</div>;
};`;
const result = await postProcessFile('Component.tsx', tsx, {
transpileToJS: true,
});
expect(result).toMatchSnapshot();
});
it('should preserve JSX child call expressions when transpiling TSX', async () => {
const tsx = `const player = useValue('currentPlayer', STORE_ID) as () => string;
export const Component = () => {
return <span>{player()}</span>;
};`;
const result = await postProcessFile('Component.tsx', tsx, {
transpileToJS: true,
});
expect(result).toMatchSnapshot();
});
it('should format and transpile together', async () => {
const unformattedTS = `const add=(a:number,b:number):number=>{return a+b;}`;
const result = await postProcessFile('math.ts', unformattedTS, {
prettier: true,
transpileToJS: true,
});
expect(result).toMatchSnapshot();
});
it('should handle non-TS files without transpiling', async () => {
const css = 'body{margin:0;padding:0;}';
const result = await postProcessFile('styles.css', css, {
prettier: true,
transpileToJS: true,
});
expect(result).toMatchSnapshot();
});
it('should format Svelte files with prettier', async () => {
const svelte = `<script lang="ts">const count=0;</script>
<button on:click={()=>count++}>{count}</button>`;
const result = await postProcessFile('Counter.svelte', svelte, {
prettier: true,
});
expect(result).toMatchSnapshot();
});
it('should preserve blank lines when transpiling TypeScript to JavaScript', async () => {
const typescript = `import './styles.css';
import {createButton} from './button';
import {createInput} from './input';
export const createTodoInput = (store) => {
const container = document.createElement('div');
container.id = 'todoInput';
const input = createInput('What needs to be done?');
const addTodo = () => {
const text = input.value.trim();
if (text) {
store.addRow('todos', {text, completed: false});
input.value = '';
input.focus();
}
};
};`;
const result = await postProcessFile('todoInput.ts', typescript, {
transpileToJS: true,
});
expect(result).toMatchSnapshot();
});
});