Skip to content

Commit ab57b3f

Browse files
committed
test: add oxc plugin unit tests
1 parent 1e7760e commit ab57b3f

File tree

10 files changed

+194
-17
lines changed

10 files changed

+194
-17
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react-jsx"
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "preserve"
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"jsxFactory": "g",
4+
"jsxFragmentFactory": "foo",
5+
"jsxImportSource": "baz"
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2021"
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2022"
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext"
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"useDefineForClassFields": false
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"useDefineForClassFields": true
4+
}
5+
}

packages/vite/src/node/__tests__/plugins/oxc.spec.ts

Lines changed: 151 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { expect, test } from 'vitest'
1+
import path from 'node:path'
2+
import { describe, expect, test } from 'vitest'
23
import type { InternalModuleFormat } from 'rolldown'
34
import { resolveConfig } from '../../config'
4-
import { buildOxcPlugin } from '../../plugins/oxc'
5+
import { buildOxcPlugin, transformWithOxc } from '../../plugins/oxc'
56
import { PartialEnvironment } from '../../baseEnvironment'
67

78
async function createBuildOxcPluginRenderChunk(target: string) {
@@ -26,10 +27,142 @@ async function createBuildOxcPluginRenderChunk(target: string) {
2627
}
2728
}
2829

29-
test('should inject helper for worker iife from esm', async () => {
30-
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
31-
const result = await renderChunk(
32-
`(function() {
30+
describe('transformWithOxc', () => {
31+
test('correctly overrides TS configuration and applies automatic transform', async () => {
32+
const jsxImportSource = 'bar'
33+
const result = await transformWithOxc(
34+
'const foo = () => <></>',
35+
path.resolve(
36+
import.meta.dirname,
37+
'./fixtures/oxc-tsconfigs/jsx-preserve/baz.jsx',
38+
),
39+
{
40+
jsx: {
41+
runtime: 'automatic',
42+
importSource: jsxImportSource,
43+
},
44+
},
45+
)
46+
expect(result?.code).toContain(`${jsxImportSource}/jsx-runtime`)
47+
expect(result?.code).toContain('/* @__PURE__ */')
48+
})
49+
50+
test('correctly overrides TS configuration and preserves code', async () => {
51+
const foo = 'const foo = () => <></>'
52+
const result = await transformWithOxc(
53+
foo,
54+
path.resolve(
55+
import.meta.dirname,
56+
'./fixtures/oxc-tsconfigs/jsx-react-jsx/baz.jsx',
57+
),
58+
{
59+
jsx: 'preserve',
60+
},
61+
)
62+
expect(result?.code).toContain(foo)
63+
})
64+
65+
test('correctly overrides TS configuration and transforms code', async () => {
66+
const jsxFactory = 'h',
67+
jsxFragment = 'bar'
68+
const result = await transformWithOxc(
69+
'const foo = () => <></>',
70+
path.resolve(
71+
import.meta.dirname,
72+
'./fixtures/oxc-tsconfigs/jsx-complex-options/baz.jsx',
73+
),
74+
{
75+
jsx: {
76+
runtime: 'classic',
77+
pragma: jsxFactory,
78+
pragmaFrag: jsxFragment,
79+
},
80+
},
81+
)
82+
expect(result?.code).toContain(
83+
`/* @__PURE__ */ ${jsxFactory}(${jsxFragment}, null)`,
84+
)
85+
})
86+
87+
describe('useDefineForClassFields', async () => {
88+
const transformClassCode = async (target: string, tsconfigDir: string) => {
89+
const result = await transformWithOxc(
90+
`
91+
class foo {
92+
bar = 'bar'
93+
}
94+
`,
95+
path.resolve(import.meta.dirname, tsconfigDir, './bar.ts'),
96+
{ target },
97+
)
98+
return result?.code
99+
}
100+
101+
const [
102+
defineForClassFieldsTrueTransformedCode,
103+
defineForClassFieldsTrueLowerTransformedCode,
104+
defineForClassFieldsFalseTransformedCode,
105+
] = await Promise.all([
106+
transformClassCode('esnext', './fixtures/oxc-tsconfigs/use-define-true'),
107+
transformClassCode('es2021', './fixtures/oxc-tsconfigs/use-define-true'),
108+
transformClassCode('esnext', './fixtures/oxc-tsconfigs/use-define-false'),
109+
])
110+
111+
test('target: esnext and tsconfig.target: esnext => true', async () => {
112+
const actual = await transformClassCode(
113+
'esnext',
114+
'./fixtures/oxc-tsconfigs/target-esnext',
115+
)
116+
expect(actual).toBe(defineForClassFieldsTrueTransformedCode)
117+
})
118+
119+
test('target: es2021 and tsconfig.target: esnext => true', async () => {
120+
const actual = await transformClassCode(
121+
'es2021',
122+
'./fixtures/oxc-tsconfigs/target-esnext',
123+
)
124+
expect(actual).toBe(defineForClassFieldsTrueLowerTransformedCode)
125+
})
126+
127+
test('target: es2021 and tsconfig.target: es2021 => false', async () => {
128+
const actual = await transformClassCode(
129+
'es2021',
130+
'./fixtures/oxc-tsconfigs/target-es2021',
131+
)
132+
expect(actual).toBe(defineForClassFieldsFalseTransformedCode)
133+
})
134+
135+
test('target: esnext and tsconfig.target: es2021 => false', async () => {
136+
const actual = await transformClassCode(
137+
'esnext',
138+
'./fixtures/oxc-tsconfigs/target-es2021',
139+
)
140+
expect(actual).toBe(defineForClassFieldsFalseTransformedCode)
141+
})
142+
143+
test('target: es2022 and tsconfig.target: es2022 => true', async () => {
144+
const actual = await transformClassCode(
145+
'es2022',
146+
'./fixtures/oxc-tsconfigs/target-es2022',
147+
)
148+
expect(actual).toBe(defineForClassFieldsTrueTransformedCode)
149+
})
150+
151+
test('target: es2022 and tsconfig.target: undefined => false', async () => {
152+
const actual = await transformClassCode(
153+
'es2022',
154+
'./fixtures/oxc-tsconfigs/empty',
155+
)
156+
expect(actual).toBe(defineForClassFieldsFalseTransformedCode)
157+
})
158+
})
159+
})
160+
161+
describe('renderChunk', () => {
162+
test('should inject helper for worker iife from esm', async () => {
163+
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
164+
const result = await renderChunk(
165+
`(function() {
33166
34167
"use strict";
35168
@@ -41,9 +174,9 @@ test('should inject helper for worker iife from esm', async () => {
41174
42175
//#endregion
43176
})();`,
44-
'iife',
45-
)
46-
expect(result).toMatchInlineSnapshot(`
177+
'iife',
178+
)
179+
expect(result).toMatchInlineSnapshot(`
47180
"(function() {
48181
"use strict";var babelHelpers=function(exports){function t(e,t,n,r,i,a,o){try{var s=e[a](o),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,i)}function n(e){return function(){var n=this,r=arguments;return new Promise(function(i,a){var o=e.apply(n,r);function s(e){t(o,i,a,s,c,\`next\`,e)}function c(e){t(o,i,a,s,c,\`throw\`,e)}s(void 0)})}}return exports.asyncToGenerator=n,exports}({});
49182
@@ -56,12 +189,12 @@ test('should inject helper for worker iife from esm', async () => {
56189
})();
57190
"
58191
`)
59-
})
192+
})
60193

61-
test('should inject helper for worker iife from cjs', async () => {
62-
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
63-
const result = await renderChunk(
64-
`(function() {
194+
test('should inject helper for worker iife from cjs', async () => {
195+
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
196+
const result = await renderChunk(
197+
`(function() {
65198
66199
67200
//#region src/index.js
@@ -72,9 +205,9 @@ test('should inject helper for worker iife from cjs', async () => {
72205
73206
//#endregion
74207
})();`,
75-
'iife',
76-
)
77-
expect(result).toMatchInlineSnapshot(`
208+
'iife',
209+
)
210+
expect(result).toMatchInlineSnapshot(`
78211
"(function() {var babelHelpers=function(exports){function t(e,t,n,r,i,a,o){try{var s=e[a](o),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,i)}function n(e){return function(){var n=this,r=arguments;return new Promise(function(i,a){var o=e.apply(n,r);function s(e){t(o,i,a,s,c,\`next\`,e)}function c(e){t(o,i,a,s,c,\`throw\`,e)}s(void 0)})}}return exports.asyncToGenerator=n,exports}({});
79212
80213
//#region src/index.js
@@ -86,4 +219,5 @@ test('should inject helper for worker iife from cjs', async () => {
86219
})();
87220
"
88221
`)
222+
})
89223
})

0 commit comments

Comments
 (0)