Skip to content

Commit 9ea1783

Browse files
lifeartKent C. Dodds
authored andcommitted
feat: jsx and tsx support (#48)
1 parent 8c38231 commit 9ea1783

7 files changed

Lines changed: 44 additions & 13 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
"devDependencies": {
3333
"@babel/core": "^7.2.0",
3434
"@babel/plugin-proposal-async-generator-functions": "^7.2.0",
35+
"@babel/plugin-syntax-jsx": "^7.2.0",
36+
"@babel/plugin-syntax-typescript": "^7.3.3",
3537
"@babel/plugin-transform-async-to-generator": "^7.2.0",
3638
"@babel/preset-env": "^7.2.0",
3739
"kcd-scripts": "^0.49.0"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"plugins": [
3+
"@babel/plugin-syntax-jsx"
4+
]
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default <div></div>;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"plugins": [
3+
["@babel/plugin-syntax-typescript", { "isTSX" : true }]
4+
]
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default <div></div> as any;

src/__tests__/index.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,14 @@ test('can pass tests in fixtures relative to the filename', async () => {
306306
}),
307307
)
308308
expect(describeSpy).toHaveBeenCalledTimes(5)
309-
expect(itSpy).toHaveBeenCalledTimes(8)
309+
expect(itSpy).toHaveBeenCalledTimes(10)
310+
310311
expect(itSpy.mock.calls).toEqual([
311312
[`changed`, expect.any(Function)],
313+
[`jsx support`, expect.any(Function)],
312314
[`nested a`, expect.any(Function)],
313315
[`nested b`, expect.any(Function)],
316+
[`tsx support`, expect.any(Function)],
314317
[`typescript`, expect.any(Function)],
315318
[`unchanged`, expect.any(Function)],
316319
[`nested with option`, expect.any(Function)],
@@ -342,10 +345,21 @@ test('creates output file for new tests', async () => {
342345
}),
343346
)
344347

345-
expect(writeFileSyncSpy.mock.calls[0]).toEqual([
346-
expect.stringMatching(/(\/|\\)output\.(j|t)s$/),
347-
"'use strict';",
348-
])
348+
const calls = writeFileSyncSpy.mock.calls[0]
349+
const outputMatch = /(\/|\\)output\.(j|t)sx?$/
350+
if (calls[0].endsWith('.jsx')) {
351+
expect(calls).toEqual([
352+
expect.stringMatching(outputMatch),
353+
'export default <div></div>;',
354+
])
355+
} else if (calls[0].endsWith('.tsx')) {
356+
expect(calls).toEqual([
357+
expect.stringMatching(outputMatch),
358+
'export default <div></div> as any;',
359+
])
360+
} else {
361+
expect(calls).toEqual([expect.stringMatching(outputMatch), "'use strict';"])
362+
}
349363
})
350364

351365
test('uses the fixture filename in babelOptions', async () => {
@@ -614,7 +628,7 @@ test('allows formatting fixtures results', async () => {
614628
formatResult: formatResultSpy,
615629
}),
616630
)
617-
expect(formatResultSpy).toHaveBeenCalledTimes(9)
631+
expect(formatResultSpy).toHaveBeenCalledTimes(11)
618632
})
619633

620634
test('works with a formatter adding a empty line', async () => {
@@ -626,7 +640,7 @@ test('works with a formatter adding a empty line', async () => {
626640
formatResult: formatResultSpy,
627641
}),
628642
)
629-
expect(formatResultSpy).toHaveBeenCalledTimes(9)
643+
expect(formatResultSpy).toHaveBeenCalledTimes(11)
630644
})
631645

632646
test('gets options from options.json files when using fixtures', async () => {
@@ -658,7 +672,7 @@ test('gets options from options.json files when using fixtures', async () => {
658672
}),
659673
)
660674

661-
expect(optionRootFoo).toHaveBeenCalledTimes(8)
675+
expect(optionRootFoo).toHaveBeenCalledTimes(10)
662676
expect(optionFoo).toHaveBeenCalledTimes(2)
663677
expect(optionBar).toHaveBeenCalledTimes(1)
664678
})
@@ -703,10 +717,10 @@ test('appends to root plugins array', async () => {
703717
}),
704718
)
705719

706-
expect(optionRootFoo).toHaveBeenCalledTimes(8)
720+
expect(optionRootFoo).toHaveBeenCalledTimes(10)
707721
expect(optionFoo).toHaveBeenCalledTimes(2)
708722
expect(optionBar).toHaveBeenCalledTimes(1)
709-
expect(programVisitor).toHaveBeenCalledTimes(9)
723+
expect(programVisitor).toHaveBeenCalledTimes(11)
710724
})
711725

712726
test('endOfLine - default', async () => {

src/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,14 @@ const createFixtureTests = (fixturesDir, options) => {
265265
const optionsPath = path.join(fixtureDir, 'options.json')
266266
const jsCodePath = path.join(fixtureDir, 'code.js')
267267
const tsCodePath = path.join(fixtureDir, 'code.ts')
268+
const jsxCodePath = path.join(fixtureDir, 'code.jsx')
269+
const tsxCodePath = path.join(fixtureDir, 'code.tsx')
268270
const blockTitle = caseName.split('-').join(' ')
269271
const codePath =
270272
(pathExists.sync(jsCodePath) && jsCodePath) ||
271-
(pathExists.sync(tsCodePath) && tsCodePath)
272-
273+
(pathExists.sync(tsCodePath) && tsCodePath) ||
274+
(pathExists.sync(jsxCodePath) && jsxCodePath) ||
275+
(pathExists.sync(tsxCodePath) && tsxCodePath)
273276
let fixturePluginOptions = {}
274277
if (pathExists.sync(optionsPath)) {
275278
fixturePluginOptions = require(optionsPath)
@@ -289,7 +292,7 @@ const createFixtureTests = (fixturesDir, options) => {
289292
return
290293
}
291294

292-
const ext = /\.ts$/.test(codePath) ? '.ts' : '.js'
295+
const ext = `.${ codePath.split('.').pop() }`;
293296
it(blockTitle, () => {
294297
const {
295298
plugin,

0 commit comments

Comments
 (0)