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(vue3-jest): reduce getTypeScriptConfig calls #491

Merged
merged 2 commits into from
Aug 23, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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