Skip to content

Commit

Permalink
rebsae
Browse files Browse the repository at this point in the history
  • Loading branch information
lmiller1990 committed Aug 6, 2023
2 parents 07e2f69 + d7a9494 commit 0575748
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 12 deletions.
23 changes: 23 additions & 0 deletions e2e/2.x/basic/components/ScriptAndScriptSetup.vue
@@ -0,0 +1,23 @@
<template>
<div>
<button @click="increase">Products: {{ products }}</button>
<Basic />
<span>{{ msg }}</span>
</div>
</template>

<script>
import Basic from './Basic.vue'
</script>

<script setup>
import { ref } from 'vue'
const products = ref(10)
const greet = () => console.log('greet')
const increase = () => {
greet()
num.value++
}
const msg = 'hello world'
</script>
7 changes: 7 additions & 0 deletions e2e/2.x/basic/test.js
Expand Up @@ -21,6 +21,7 @@ import Jsx from './components/Jsx.vue'
import Constructor from './components/Constructor.vue'
import { compileStyle } from '@vue/component-compiler-utils'
import ScriptSetup from './components/ScriptSetup'
import ScriptAndScriptSetup from './components/ScriptAndScriptSetup'
import ExtendedTsConfig from './components/ExtendedTsConfig.vue'

jest.mock('@vue/component-compiler-utils', () => ({
Expand Down Expand Up @@ -165,6 +166,12 @@ test('processes SFC with <script setup>', () => {
expect(wrapper.html()).toContain('Welcome to Your Vue.js App')
})

test('processes SFC with both <script> and <script setup> in same component file', () => {
const wrapper = mount(ScriptAndScriptSetup)
expect(wrapper.html()).toContain('Products: 10')
expect(wrapper.html()).toContain('Welcome to Your Vue.js App')
})

test('handles extended tsconfig.json files', () => {
const wrapper = mount(ExtendedTsConfig)
expect(wrapper.element.tagName).toBe('DIV')
Expand Down
7 changes: 7 additions & 0 deletions e2e/3.x/babel-in-package/components/Tsx.vue
@@ -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
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
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')
})
23 changes: 23 additions & 0 deletions e2e/3.x/basic/components/ScriptAndScriptSetup.vue
@@ -0,0 +1,23 @@
<template>
<div>
<button @click="increase">Products: {{ products }}</button>
<Basic />
<span>{{ msg }}</span>
</div>
</template>

<script>
import Basic from './Basic.vue'
</script>

<script setup>
import { ref } from 'vue'
const products = ref(10)
const greet = () => console.log('greet')
const increase = () => {
greet()
num.value++
}
const msg = 'hello world'
</script>
7 changes: 7 additions & 0 deletions e2e/3.x/basic/test.js
Expand Up @@ -24,6 +24,7 @@ import ScriptSetupSugarRef from './components/ScriptSetupSugarRef.vue'
import FunctionalRenderFn from './components/FunctionalRenderFn.vue'
import CompilerDirective from './components/CompilerDirective.vue'
import ExtendedTsConfig from './components/ExtendedTsConfig.vue'
import ScriptAndScriptSetup from './components/ScriptAndScriptSetup.vue'

// TODO: JSX for Vue 3? TSX?
import Jsx from './components/Jsx.vue'
Expand Down Expand Up @@ -214,3 +215,9 @@ test('handles extended tsconfig.json files', () => {
const elm = document.querySelector('div')
expect(elm).toBeDefined()
})

test('processes SFC with both <script> and <script setup> in same component file', () => {
mount(ScriptAndScriptSetup)
expect(document.body.outerHTML).toContain('Products: 10')
expect(document.body.outerHTML).toContain('Welcome to Your Vue.js App')
})
9 changes: 7 additions & 2 deletions packages/vue2-jest/lib/process.js
Expand Up @@ -144,15 +144,20 @@ module.exports = function(src, filename, config) {
})

const templateResult = processTemplate(descriptor, filename, config)
const scriptResult = processScript(descriptor.script, filename, config)
const scriptSetupResult = processScriptSetup(descriptor, filename, config)
const stylesResult = processStyle(descriptor.styles, filename, config)
const customBlocksResult = processCustomBlocks(
descriptor.customBlocks,
filename,
config
)

let scriptResult
const scriptSetupResult = processScriptSetup(descriptor, filename, config)

if (!scriptSetupResult) {
scriptResult = processScript(descriptor.script, filename, config)
}

const isFunctional =
(descriptor.template &&
descriptor.template.attrs &&
Expand Down
2 changes: 1 addition & 1 deletion packages/vue2-jest/package.json
@@ -1,6 +1,6 @@
{
"name": "@vue/vue2-jest",
"version": "29.2.2",
"version": "29.2.5",
"description": "Jest transformer for Vue 2",
"main": "lib/index.js",
"files": [
Expand Down
12 changes: 9 additions & 3 deletions packages/vue3-jest/lib/process.js
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 Expand Up @@ -164,15 +164,21 @@ module.exports = function(src, filename, config) {
getVueJestConfig(config)['componentNamespace'] || vueComponentNamespace

const templateResult = processTemplate(descriptor, filename, config)
const scriptResult = processScript(descriptor.script, filename, config)
const scriptSetupResult = processScriptSetup(descriptor, filename, config)
const stylesResult = processStyle(descriptor.styles, filename, config)
const customBlocksResult = processCustomBlocks(
descriptor.customBlocks,
filename,
componentNamespace,
config
)

let scriptResult
const scriptSetupResult = processScriptSetup(descriptor, filename, config)

if (!scriptSetupResult) {
scriptResult = processScript(descriptor.script, filename, config)
}

const output = generateCode({
scriptResult,
scriptSetupResult,
Expand Down
6 changes: 3 additions & 3 deletions packages/vue3-jest/lib/transformers/typescript.js
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)
}
}
})
2 changes: 1 addition & 1 deletion packages/vue3-jest/package.json
@@ -1,6 +1,6 @@
{
"name": "@vue/vue3-jest",
"version": "29.2.2",
"version": "29.2.5",
"description": "Jest Vue transform",
"main": "lib/index.js",
"files": [
Expand Down

0 comments on commit 0575748

Please sign in to comment.