8000 feat: support <script setup> in Vue 2.7 (supersedes #483) by kiroushi · Pull Request #489 · vuejs/vue-jest · GitHub
[go: up one dir, main page]

Skip to content

feat: support <script setup> in Vue 2.7 (supersedes #483) #489

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

Merged
merged 12 commits into from
Sep 15, 2022
Prev Previous commit
Next Next commit
fix: make script setup & script blocks work together
  • Loading branch information
kiroushi committed Sep 7, 2022
commit 632622b3bf17a720952a417caaf6ae1638c6d8c4
6 changes: 3 additions & 3 deletions packages/vue2-jest/lib/generate-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ module.exports = function generateCode(
) {
var node = new SourceNode(null, null)

const finalScriptResult = scriptResult || scriptSetupResult
if (finalScriptResult) {
addToSourceMap(node, finalScriptResult)
if (scriptResult || scriptSetupResult) {
scriptResult && addToSourceMap(node, scriptResult)
scriptSetupResult && addToSourceMap(node, scriptSetupResult)
} else {
node.add(
`Object.defineProperty(exports, "__esModule", {\n` +
Expand Down
5 changes: 2 additions & 3 deletions packages/vue2-jest/lib/process-custom-blocks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { getVueJestConfig, getCustomTransformer } = require('./utils')
const vueOptionsNamespace = require('./constants').vueOptionsNamespace

function applyTransformer(
transformer,
Expand All @@ -17,7 +16,7 @@ function groupByType(acc, block) {
return acc
}

module.exports = function(allBlocks, filename, config) {
module.exports = function(allBlocks, filename, componentNamespace, config) {
const blocksByType = allBlocks.reduce(groupByType, {})
const code = []
for (const [type, blocks] of Object.entries(blocksByType)) {
Expand All @@ -29,7 +28,7 @@ module.exports = function(allBlocks, filename, config) {
const codeStr = applyTransformer(
transformer,
blocks,
vueOptionsNamespace,
componentNamespace,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change (bug?) introduced for custom block processors. This import in process.js is just undefined, because ./constants does not have vueComponentNamespace:

const vueComponentNamespace = require('./constants').vueComponentNamespace

This means that the vueOptionsNamespace in the custom processor will always be undefined. This currently breaks vue-i18n-jest for me here:

https://github.com/intlify/vue-i18n-jest/blob/afaa5c3d4dd2e54760a5ef73cd2829672348cf25/src/process.ts#L43

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh, sorry. Can you file an issue with a minimal repro?

Bonus points if you want to make a PR!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: you already made a PR!

filename,
config
)
Expand Down
13 changes: 10 additions & 3 deletions packages/vue2-jest/lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ const loadSrc = require('./utils').loadSrc
const babelTransformer = require('babel-jest').default
const generateCode = require('./generate-code')
const mapLines = require('./map-lines')
const vueComponentNamespace = require('./constants').vueComponentNamespace

let isVue27 = false
let compilerUtils

try {
compilerUtils = require('@vue/compiler-sfc')
compilerUtils = require('vue/compiler-sfc')
isVue27 = true
} catch (e) {
compilerUtils = require('@vue/component-compiler-utils')
Expand Down Expand Up @@ -59,6 +60,7 @@ function processScriptSetup(descriptor, filePath, config) {
const vueJestConfig = getVueJestConfig(conf 8000 ig)
const content = compilerUtils.compileScript(descriptor, {
id: filePath,
reactivityTransform: true,
...vueJestConfig.compilerOptions
})
const contentMap = mapLines(descriptor.scriptSetup.map, content.map)
Expand Down Expand Up @@ -92,6 +94,7 @@ function processTemplate(descriptor, filename, config) {
if (isVue27 && scriptSetup) {
const scriptSetupResult = compilerUtils.compileScript(descriptor, {
id: filename,
reactivityTransform: true,
...vueJestConfig.compilerOptions
})
bindings = scriptSetupResult.bindings
Expand All @@ -107,7 +110,7 @@ function processTemplate(descriptor, filename, config) {
preprocessOptions: vueJestConfig[template.lang],
...userTemplateCompilerOptions,
compilerOptions: {
optimize: false,
...(!isVue27 ? { optimize: false } : {}),
...userTemplateCompilerOptions.compilerOptions
},
...(isVue27 ? { bindings } : {})
Expand Down Expand Up @@ -136,17 +139,21 @@ function processStyle(styles, filename, config) {
module.exports = function(src, filename, config) {
const descriptor = compilerUtils.parse({
source: src,
compiler: VueTemplateCompiler,
compiler: isVue27 ? undefined : VueTemplateCompiler,
filename
})

const componentNamespace =
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 = processStyl 51F7 e(descriptor.styles, filename, config)
const customBlocksResult = processCustomBlocks(
descriptor.customBlocks,
filename,
componentNamespace,
config
)

Expand Down
0