Skip to content

Commit

Permalink
fix(vue3-jest): reduce getTypeScriptConfig calls (#491)
Browse files Browse the repository at this point in the history
* fix(vue3-jest): reduce `getTypeScriptConfig` calls

* test(vue3-basic): update snapshot

typescript -> @babel/preset-env
  • Loading branch information
nogic1008 authored Aug 23, 2022
1 parent 6d1eec5 commit 4855375
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 40 deletions.
60 changes: 36 additions & 24 deletions e2e/3.x/basic/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,25 @@ var _default = {
};
exports[\\"default\\"] = _default;
\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
exports.render = void 0;
var vue_1 = require(\\"vue\\");
var _hoisted_1 = { class: \\"hello\\" };
function render(_ctx, _cache) {
return ((0, vue_1.openBlock)(), (0, vue_1.createElementBlock)(\\"div\\", _hoisted_1, [
(0, vue_1.createElementVNode)(\\"h1\\", {
class: (0, vue_1.normalizeClass)(_ctx.headingClasses)
}, (0, vue_1.toDisplayString)(_ctx.msg), 3 /* TEXT, CLASS */)
]));
}
Object.defineProperty(exports, \\"__esModule\\", {
value: true
});
exports.render = render;
;exports.default = {...exports.default, render};;exports.default = {...exports.default, __cssModules: {\\"css\\":{\\"testA\\":\\"testA\\"},\\"$style\\":{\\"testB\\":\\"testB\\"}}}"
var _vue = require(\\"vue\\");
var _hoisted_1 = {
\\"class\\": \\"hello\\"
};
function render(_ctx, _cache) {
return (0, _vue.openBlock)(), (0, _vue.createElementBlock)(\\"div\\", _hoisted_1, [(0, _vue.createElementVNode)(\\"h1\\", {
\\"class\\": (0, _vue.normalizeClass)(_ctx.headingClasses)
}, (0, _vue.toDisplayString)(_ctx.msg), 3
/* TEXT, CLASS */
)]);
};exports.default = {...exports.default, render};;exports.default = {...exports.default, __cssModules: {\\"css\\":{\\"testA\\":\\"testA\\"},\\"$style\\":{\\"testB\\":\\"testB\\"}}}"
`;

exports[`generates source maps using src attributes 1`] = `
Expand Down Expand Up @@ -79,17 +85,23 @@ var _default = {
};
exports[\\"default\\"] = _default;
\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
exports.render = void 0;
var vue_1 = require(\\"vue\\");
var _hoisted_1 = { class: \\"hello\\" };
function render(_ctx, _cache) {
return ((0, vue_1.openBlock)(), (0, vue_1.createElementBlock)(\\"div\\", _hoisted_1, [
(0, vue_1.createElementVNode)(\\"h1\\", {
class: (0, vue_1.normalizeClass)(_ctx.headingClasses)
}, (0, vue_1.toDisplayString)(_ctx.msg), 3 /* TEXT, CLASS */)
]));
}
Object.defineProperty(exports, \\"__esModule\\", {
value: true
});
exports.render = render;
;exports.default = {...exports.default, render};"
var _vue = require(\\"vue\\");
var _hoisted_1 = {
\\"class\\": \\"hello\\"
};
function render(_ctx, _cache) {
return (0, _vue.openBlock)(), (0, _vue.createElementBlock)(\\"div\\", _hoisted_1, [(0, _vue.createElementVNode)(\\"h1\\", {
\\"class\\": (0, _vue.normalizeClass)(_ctx.headingClasses)
}, (0, _vue.toDisplayString)(_ctx.msg), 3
/* TEXT, CLASS */
)]);
};exports.default = {...exports.default, render};"
`;
37 changes: 21 additions & 16 deletions packages/vue3-jest/lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ function processScriptSetup(descriptor, filePath, config) {
return result
}

/**
* Process SFC <template> section.
* @param {import('@vue/compiler-sfc').SFCDescriptor} descriptor
* @param {string} filename
* @param {import('@jest/transform').TransformOptions} config
*/
function processTemplate(descriptor, filename, config) {
const { template, scriptSetup } = descriptor

Expand Down Expand Up @@ -118,24 +124,23 @@ function processTemplate(descriptor, filename, config) {

logResultErrors(result)

const tsconfig = getTypeScriptConfig(vueJestConfig.tsConfig)

if (tsconfig) {
// they are using TypeScript.
const { transpileModule } = require('typescript')
const { outputText } = transpileModule(result.code, { tsconfig })
return { code: outputText }
} else {
// babel
const babelify = transform(result.code, {
filename: 'file.js',
presets: ['@babel/preset-env']
})

return {
code: babelify.code
// TypeScript
if (isTS) {
const tsconfig = getTypeScriptConfig(vueJestConfig.tsConfig)
if (tsconfig) {
const { transpileModule } = require('typescript')
const { outputText } = transpileModule(result.code, { tsconfig })
return { code: outputText }
}
}

// babel
const babelify = transform(result.code, {
filename: 'file.js',
presets: ['@babel/preset-env']
})

return { code: babelify.code }
}

function processStyle(styles, filename, config) {
Expand Down

0 comments on commit 4855375

Please sign in to comment.