Skip to content

Commit c6292f3

Browse files
authored
fix: fixed lang='tsx' files not working for vue2-jest (#395)
* test: add tsx file * fix: transpile as tsx when lang='tsx' * feat: remove hardcoded `target`
1 parent 04a3233 commit c6292f3

File tree

8 files changed

+150
-10
lines changed

8 files changed

+150
-10
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script lang="tsx">
2+
import Vue from 'vue'
3+
4+
export default Vue.extend({
5+
computed: {
6+
exclamationMarks(): string {
7+
return 'tsx components'
8+
}
9+
},
10+
render(): VNode {
11+
return <div>{this.exclamationMarks}</div>
12+
}
13+
})
14+
</script>

e2e/2.x/babel-in-package/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
"devDependencies": {
1515
"@babel/core": "^7.9.0",
1616
"@babel/preset-env": "^7.9.0",
17+
"@vue/babel-preset-jsx": "^1.2.4",
1718
"@vue/test-utils": "^1.1.0",
19+
"@vue/vue2-jest": "~27.0.0-alpha.1",
1820
"coffeescript": "^2.3.2",
1921
"jest": "27.x",
2022
"ts-jest": "^27.0.4",
21-
"typescript": "^3.2.2",
22-
"@vue/vue2-jest": "~27.0.0-alpha.1"
23+
"typescript": "^3.2.2"
2324
},
2425
"jest": {
2526
"testEnvironment": "jsdom",
@@ -35,7 +36,8 @@
3536
},
3637
"babel": {
3738
"presets": [
38-
"@babel/env"
39+
"@babel/env",
40+
"@vue/babel-preset-jsx"
3941
]
4042
}
4143
}

e2e/2.x/babel-in-package/test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { mount } from '@vue/test-utils'
22
import TypeScript from './components/TypeScript.vue'
33
import Basic from './components/Basic.vue'
44
import Coffee from './components/Coffee.vue'
5+
import Tsx from './components/Tsx.vue'
56

67
test('processes .vue files', () => {
78
const wrapper = mount(Basic)
@@ -13,7 +14,12 @@ test('processes .vue file with lang set to coffee', () => {
1314
expect(wrapper.vm).toBeTruthy()
1415
})
1516

16-
test('processes .vue files with lang set to typescript', () => {
17+
test('processes .vue files with lang set to ts(typescript)', () => {
1718
const wrapper = mount(TypeScript)
1819
expect(wrapper.vm).toBeTruthy()
1920
})
21+
22+
test('processes .vue files with lang set to tsx(typescript)', () => {
23+
const wrapper = mount(Tsx)
24+
expect(wrapper.text()).toContain('tsx components')
25+
})

e2e/2.x/babel-in-package/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"emitDecoratorMetadata": true,
1515
"suppressImplicitAnyIndexErrors": true,
1616
"allowSyntheticDefaultImports": true,
17-
"sourceMap": true
17+
"sourceMap": true,
18+
"esModuleInterop": true
1819
}
1920
}

packages/vue2-jest/lib/process.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const generateCode = require('./generate-code')
1717
function resolveTransformer(lang = 'js', vueJestConfig) {
1818
const transformer = getCustomTransformer(vueJestConfig['transform'], lang)
1919
if (/^typescript$|tsx?$/.test(lang)) {
20-
return transformer || require('./transformers/typescript')
20+
return transformer || require('./transformers/typescript')(lang)
2121
} else if (/^coffee$|coffeescript$/.test(lang)) {
2222
return transformer || coffeescriptTransformer
2323
} else {

packages/vue2-jest/lib/transformers/typescript.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
getVueJestConfig
88
} = require('../utils')
99

10-
module.exports = {
10+
module.exports = scriptLang => ({
1111
process(scriptContent, filePath, config) {
1212
ensureRequire('typescript', ['typescript'])
1313
const typescript = require('typescript')
@@ -16,7 +16,7 @@ module.exports = {
1616

1717
const res = typescript.transpileModule(scriptContent, {
1818
...tsconfig,
19-
fileName: filePath
19+
fileName: filePath + (scriptLang === 'tsx' ? '.tsx' : '')
2020
})
2121

2222
res.outputText = stripInlineSourceMap(res.outputText)
@@ -32,4 +32,4 @@ module.exports = {
3232

3333
return transformer.process(res.outputText, filePath, config)
3434
}
35-
}
35+
})

packages/vue2-jest/lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const getTsJestConfig = function getTsJestConfig(config) {
7272
const configSet = new ConfigSet(config.config)
7373
var tsConfig = configSet.typescript || configSet.parsedTsConfig
7474
return {
75-
compilerOptions: { ...tsConfig.options, target: 'es5', module: 'commonjs' }
75+
compilerOptions: { ...tsConfig.options, module: 'commonjs' }
7676
}
7777
}
7878

yarn.lock

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@
140140
dependencies:
141141
"@babel/types" "^7.15.0"
142142

143+
"@babel/helper-module-imports@^7.0.0":
144+
version "7.15.4"
145+
resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.15.4.tgz#e18007d230632dea19b47853b984476e7b4e103f"
146+
integrity sha512-jeAHZbzUwdW/xHgHQ3QmWR4Jg6j15q4w/gCfwZvtqOxoo5DKtLHk8Bsf4c5RZRC7NmLEs+ohkdq8jFefuvIxAA==
147+
dependencies:
148+
"@babel/types" "^7.15.4"
149+
143150
"@babel/helper-module-imports@^7.12.13", "@babel/helper-module-imports@^7.14.5":
144151
version "7.14.5"
145152
resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.14.5.tgz#6d1a44df6a38c957aa7c312da076429f11b422f3"
@@ -454,6 +461,13 @@
454461
dependencies:
455462
"@babel/helper-plugin-utils" "^7.8.0"
456463

464+
"@babel/plugin-syntax-jsx@^7.2.0":
465+
version "7.14.5"
466+
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-jsx/-/plugin-syntax-jsx-7.14.5.tgz#000e2e25d8673cce49300517a3eda44c263e4201"
467+
integrity sha512-ohuFIsOMXJnbOMRfX7/w7LocdR6R7whhuRD4ax8IipLcLPlZGJKkBxgHp++U4N/vKyU16/YDQr2f5seajD3jIw==
468+
dependencies:
469+
"@babel/helper-plugin-utils" "^7.14.5"
470+
457471
"@babel/plugin-syntax-logical-assignment-operators@^7.10.4", "@babel/plugin-syntax-logical-assignment-operators@^7.8.3":
458472
version "7.10.4"
459473
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-logical-assignment-operators/-/plugin-syntax-logical-assignment-operators-7.10.4.tgz#ca91ef46303530448b906652bac2e9fe9941f699"
@@ -894,6 +908,14 @@
894908
"@babel/helper-validator-identifier" "^7.14.9"
895909
to-fast-properties "^2.0.0"
896910

911+
"@babel/types@^7.15.4":
912+
version "7.15.6"
913+
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.15.6.tgz#99abdc48218b2881c058dd0a7ab05b99c9be758f"
914+
integrity sha512-BPU+7QhqNjmWyDO0/vitH/CuhpV8ZmK1wpKva8nuyNF5MJfuRNWMc+hc14+u9xT93kvykMdncrJT19h74uB1Ig==
915+
dependencies:
916+
"@babel/helper-validator-identifier" "^7.14.9"
917+
to-fast-properties "^2.0.0"
918+
897919
"@babel/types@^7.6.1", "@babel/types@^7.9.6":
898920
version "7.15.4"
899921
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.15.4.tgz#74eeb86dbd6748d2741396557b9860e57fce0a0d"
@@ -1639,6 +1661,86 @@
16391661
dependencies:
16401662
"@types/yargs-parser" "*"
16411663

1664+
"@vue/babel-helper-vue-jsx-merge-props@^1.2.1":
1665+
version "1.2.1"
1666+
resolved "https://registry.yarnpkg.com/@vue/babel-helper-vue-jsx-merge-props/-/babel-helper-vue-jsx-merge-props-1.2.1.tgz#31624a7a505fb14da1d58023725a4c5f270e6a81"
1667+
integrity sha512-QOi5OW45e2R20VygMSNhyQHvpdUwQZqGPc748JLGCYEy+yp8fNFNdbNIGAgZmi9e+2JHPd6i6idRuqivyicIkA==
1668+
1669+
"@vue/babel-plugin-transform-vue-jsx@^1.2.1":
1670+
version "1.2.1"
1671+
resolved "https://registry.yarnpkg.com/@vue/babel-plugin-transform-vue-jsx/-/babel-plugin-transform-vue-jsx-1.2.1.tgz#646046c652c2f0242727f34519d917b064041ed7"
1672+
integrity sha512-HJuqwACYehQwh1fNT8f4kyzqlNMpBuUK4rSiSES5D4QsYncv5fxFsLyrxFPG2ksO7t5WP+Vgix6tt6yKClwPzA==
1673+
dependencies:
1674+
"@babel/helper-module-imports" "^7.0.0"
1675+
"@babel/plugin-syntax-jsx" "^7.2.0"
1676+
"@vue/babel-helper-vue-jsx-merge-props" "^1.2.1"
1677+
html-tags "^2.0.0"
1678+
lodash.kebabcase "^4.1.1"
1679+
svg-tags "^1.0.0"
1680+
1681+
"@vue/babel-preset-jsx@^1.2.4":
1682+
version "1.2.4"
1683+
resolved "https://registry.yarnpkg.com/@vue/babel-preset-jsx/-/babel-preset-jsx-1.2.4.tgz#92fea79db6f13b01e80d3a0099e2924bdcbe4e87"
1684+
integrity sha512-oRVnmN2a77bYDJzeGSt92AuHXbkIxbf/XXSE3klINnh9AXBmVS1DGa1f0d+dDYpLfsAKElMnqKTQfKn7obcL4w==
1685+
dependencies:
1686+
"@vue/babel-helper-vue-jsx-merge-props" "^1.2.1"
1687+
"@vue/babel-plugin-transform-vue-jsx" "^1.2.1"
1688+
"@vue/babel-sugar-composition-api-inject-h" "^1.2.1"
1689+
"@vue/babel-sugar-composition-api-render-instance" "^1.2.4"
1690+
"@vue/babel-sugar-functional-vue" "^1.2.2"
1691+
"@vue/babel-sugar-inject-h" "^1.2.2"
1692+
"@vue/babel-sugar-v-model" "^1.2.3"
1693+
"@vue/babel-sugar-v-on" "^1.2.3"
1694+
1695+
"@vue/babel-sugar-composition-api-inject-h@^1.2.1":
1696+
version "1.2.1"
1697+
resolved "https://registry.yarnpkg.com/@vue/babel-sugar-composition-api-inject-h/-/babel-sugar-composition-api-inject-h-1.2.1.tgz#05d6e0c432710e37582b2be9a6049b689b6f03eb"
1698+
integrity sha512-4B3L5Z2G+7s+9Bwbf+zPIifkFNcKth7fQwekVbnOA3cr3Pq71q71goWr97sk4/yyzH8phfe5ODVzEjX7HU7ItQ==
1699+
dependencies:
1700+
"@babel/plugin-syntax-jsx" "^7.2.0"
1701+
1702+
"@vue/babel-sugar-composition-api-render-instance@^1.2.4":
1703+
version "1.2.4"
1704+
resolved "https://registry.yarnpkg.com/@vue/babel-sugar-composition-api-render-instance/-/babel-sugar-composition-api-render-instance-1.2.4.tgz#e4cbc6997c344fac271785ad7a29325c51d68d19"
1705+
integrity sha512-joha4PZznQMsxQYXtR3MnTgCASC9u3zt9KfBxIeuI5g2gscpTsSKRDzWQt4aqNIpx6cv8On7/m6zmmovlNsG7Q==
1706+
dependencies:
1707+
"@babel/plugin-syntax-jsx" "^7.2.0"
1708+
1709+
"@vue/babel-sugar-functional-vue@^1.2.2":
1710+
version "1.2.2"
1711+
resolved "https://registry.yarnpkg.com/@vue/babel-sugar-functional-vue/-/babel-sugar-functional-vue-1.2.2.tgz#267a9ac8d787c96edbf03ce3f392c49da9bd2658"
1712+
integrity sha512-JvbgGn1bjCLByIAU1VOoepHQ1vFsroSA/QkzdiSs657V79q6OwEWLCQtQnEXD/rLTA8rRit4rMOhFpbjRFm82w==
1713+
dependencies:
1714+
"@babel/plugin-syntax-jsx" "^7.2.0"
1715+
1716+
"@vue/babel-sugar-inject-h@^1.2.2":
1717+
version "1.2.2"
1718+
resolved "https://registry.yarnpkg.com/@vue/babel-sugar-inject-h/-/babel-sugar-inject-h-1.2.2.tgz#d738d3c893367ec8491dcbb669b000919293e3aa"
1719+
integrity sha512-y8vTo00oRkzQTgufeotjCLPAvlhnpSkcHFEp60+LJUwygGcd5Chrpn5480AQp/thrxVm8m2ifAk0LyFel9oCnw==
1720+
dependencies:
1721+
"@babel/plugin-syntax-jsx" "^7.2.0"
1722+
1723+
"@vue/babel-sugar-v-model@^1.2.3":
1724+
version "1.2.3"
1725+
resolved "https://registry.yarnpkg.com/@vue/babel-sugar-v-model/-/babel-sugar-v-model-1.2.3.tgz#fa1f29ba51ebf0aa1a6c35fa66d539bc459a18f2"
1726+
integrity sha512-A2jxx87mySr/ulAsSSyYE8un6SIH0NWHiLaCWpodPCVOlQVODCaSpiR4+IMsmBr73haG+oeCuSvMOM+ttWUqRQ==
1727+
dependencies:
1728+
"@babel/plugin-syntax-jsx" "^7.2.0"
1729+
"@vue/babel-helper-vue-jsx-merge-props" "^1.2.1"
1730+
"@vue/babel-plugin-transform-vue-jsx" "^1.2.1"
1731+
camelcase "^5.0.0"
1732+
html-tags "^2.0.0"
1733+
svg-tags "^1.0.0"
1734+
1735+
"@vue/babel-sugar-v-on@^1.2.3":
1736+
version "1.2.3"
1737+
resolved "https://registry.yarnpkg.com/@vue/babel-sugar-v-on/-/babel-sugar-v-on-1.2.3.tgz#342367178586a69f392f04bfba32021d02913ada"
1738+
integrity sha512-kt12VJdz/37D3N3eglBywV8GStKNUhNrsxChXIV+o0MwVXORYuhDTHJRKPgLJRb/EY3vM2aRFQdxJBp9CLikjw==
1739+
dependencies:
1740+
"@babel/plugin-syntax-jsx" "^7.2.0"
1741+
"@vue/babel-plugin-transform-vue-jsx" "^1.2.1"
1742+
camelcase "^5.0.0"
1743+
16421744
16431745
version "3.2.19"
16441746
resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.2.19.tgz#b537dd377ce51fdb64e9b30ebfbff7cd70a64cb9"
@@ -4987,6 +5089,11 @@ html-escaper@^2.0.0:
49875089
resolved "https://registry.yarnpkg.com/html-escaper/-/html-escaper-2.0.2.tgz#dfd60027da36a36dfcbe236262c00a5822681453"
49885090
integrity sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==
49895091

5092+
html-tags@^2.0.0:
5093+
version "2.0.0"
5094+
resolved "https://registry.yarnpkg.com/html-tags/-/html-tags-2.0.0.tgz#10b30a386085f43cede353cc8fa7cb0deeea668b"
5095+
integrity sha1-ELMKOGCF9Dzt41PMj6fLDe7qZos=
5096+
49905097
http-cache-semantics@^3.8.1:
49915098
version "3.8.1"
49925099
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-3.8.1.tgz#39b0e16add9b605bf0a9ef3d9daaf4843b4cacd2"
@@ -7144,6 +7251,11 @@ lodash.isstring@^4.0.1:
71447251
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
71457252
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=
71467253

7254+
lodash.kebabcase@^4.1.1:
7255+
version "4.1.1"
7256+
resolved "https://registry.yarnpkg.com/lodash.kebabcase/-/lodash.kebabcase-4.1.1.tgz#8489b1cb0d29ff88195cceca448ff6d6cc295c36"
7257+
integrity sha1-hImxyw0p/4gZXM7KRI/21swpXDY=
7258+
71477259
lodash.set@^4.3.2:
71487260
version "4.3.2"
71497261
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
@@ -10288,6 +10400,11 @@ supports-hyperlinks@^2.0.0:
1028810400
has-flag "^4.0.0"
1028910401
supports-color "^7.0.0"
1029010402

10403+
svg-tags@^1.0.0:
10404+
version "1.0.0"
10405+
resolved "https://registry.yarnpkg.com/svg-tags/-/svg-tags-1.0.0.tgz#58f71cee3bd519b59d4b2a843b6c7de64ac04764"
10406+
integrity sha1-WPcc7jvVGbWdSyqEO2x95krAR2Q=
10407+
1029110408
symbol-observable@^1.1.0:
1029210409
version "1.2.0"
1029310410
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"

0 commit comments

Comments
 (0)