Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lang='tsx' doesn't work for Vue 3 SFC #547

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions e2e/3.x/babel-in-package/components/Tsx.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script lang="tsx">
export default {
setup() {
return () => <div>tsx components</div>
}
}
</script>
4 changes: 4 additions & 0 deletions e2e/3.x/babel-in-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@vue/babel-plugin-jsx": "^1.1.5",
"@vue/vue3-jest": "^29.0.0",
"coffeescript": "^2.3.2",
"jest": "29.x",
Expand All @@ -34,6 +35,9 @@
"babel": {
"presets": [
"@babel/env"
],
"plugins": [
"@vue/babel-plugin-jsx"
]
}
}
6 changes: 6 additions & 0 deletions e2e/3.x/babel-in-package/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createApp, h } from 'vue'
import TypeScript from './components/TypeScript.vue'
import Basic from './components/Basic.vue'
import Coffee from './components/Coffee.vue'
import Tsx from './components/Tsx.vue'

function mount(Component, props, slots) {
document.getElementsByTagName('html')[0].innerHTML = ''
Expand Down Expand Up @@ -34,3 +35,8 @@ test('processes .vue files with lang set to typescript', () => {
expect(document.querySelector('#parent').textContent).toBe('Parent')
expect(document.querySelector('#child').textContent).toBe('Child')
})

test('processes .vue files with lang set to tsx(typescript)', () => {
mount(Tsx)
expect(document.querySelector('div').textContent).toContain('tsx components')
})
2 changes: 1 addition & 1 deletion packages/vue3-jest/lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const vueComponentNamespace = require('./constants').vueComponentNamespace
function resolveTransformer(lang = 'js', vueJestConfig) {
const transformer = getCustomTransformer(vueJestConfig['transform'], lang)
if (/^typescript$|tsx?$/.test(lang)) {
return transformer || typescriptTransformer
return transformer || typescriptTransformer(lang)
} else if (/^coffee$|coffeescript$/.test(lang)) {
return transformer || coffeescriptTransformer
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/vue3-jest/lib/transformers/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
getVueJestConfig
} = require('../utils')

module.exports = {
module.exports = scriptLang => ({
process(scriptContent, filePath, config) {
ensureRequire('typescript', ['typescript'])
const typescript = require('typescript')
Expand All @@ -16,7 +16,7 @@ module.exports = {

const res = typescript.transpileModule(scriptContent, {
...tsconfig,
fileName: filePath
fileName: filePath + (scriptLang === 'tsx' ? '.tsx' : '')
})

res.outputText = stripInlineSourceMap(res.outputText)
Expand All @@ -31,4 +31,4 @@ module.exports = {

return transformer.process(res.outputText, filePath, config)
}
}
})