From a7b07e869efb1945c5e1e77511b4f8b238ba7c31 Mon Sep 17 00:00:00 2001 From: Brent Burgoyne Date: Thu, 21 Mar 2019 08:30:22 -0600 Subject: [PATCH] make jest support files that import handlebars Change-Id: I073ea3f8b54cf46cd2b4c2437aad5ee13c95dc7c Reviewed-on: https://gerrit.instructure.com/186286 Tested-by: Jenkins Reviewed-by: Clay Diffrient QA-Review: Clay Diffrient Product-Review: Brent Burgoyne --- frontend_build/i18nLinerHandlebars.js | 12 +++++++++++- jest.config.js | 5 ++++- jest/handlebarsTransformer.js | 26 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 jest/handlebarsTransformer.js diff --git a/frontend_build/i18nLinerHandlebars.js b/frontend_build/i18nLinerHandlebars.js index e9a817ede14..a6d4c85ca42 100644 --- a/frontend_build/i18nLinerHandlebars.js +++ b/frontend_build/i18nLinerHandlebars.js @@ -147,7 +147,7 @@ const buildPartialRequirements = partialPaths => { return requirements } -module.exports = function i18nLinerHandlebarsLoader(source) { +function i18nLinerHandlebarsLoader(source) { this.cacheable() const name = resourceName(this.resourcePath) const dependencies = ['handlebars/runtime'] @@ -187,3 +187,13 @@ module.exports = function i18nLinerHandlebarsLoader(source) { ) return compiledTemplate } + +module.exports = i18nLinerHandlebarsLoader + +module.exports.compile = (source, path) => { + const context = { + cacheable: () => {}, + resourcePath: path + } + return i18nLinerHandlebarsLoader.call(context, source) +} diff --git a/jest.config.js b/jest.config.js index dbd3706b5a1..5ffbadf87ff 100644 --- a/jest.config.js +++ b/jest.config.js @@ -22,7 +22,9 @@ module.exports = { moduleNameMapper: { '^i18n!(.*$)': '/jest/i18nTransformer.js', '^compiled/(.*)$': '/app/coffeescripts/$1', + '^coffeescripts/(.*)$': '/app/coffeescripts/$1', '^jsx/(.*)$': '/app/jsx/$1', + '^jst/(.*)$': '/app/views/jst/$1', "^timezone$": "/public/javascripts/timezone_core.js", "\\.svg$": "/jest/imageMock.js" }, @@ -48,11 +50,12 @@ module.exports = { coverageDirectory: '/coverage-jest/', - moduleFileExtensions: [...defaults.moduleFileExtensions, 'coffee'], + moduleFileExtensions: [...defaults.moduleFileExtensions, 'coffee', 'handlebars'], transform: { '^i18n': '/jest/i18nTransformer.js', '^.+\\.coffee': '/jest/coffeeTransformer.js', + '^.+\\.handlebars': '/jest/handlebarsTransformer.js', '^.+\\.jsx?$': 'babel-jest' }, } diff --git a/jest/handlebarsTransformer.js b/jest/handlebarsTransformer.js new file mode 100644 index 00000000000..65a290a161b --- /dev/null +++ b/jest/handlebarsTransformer.js @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2017 - present Instructure, Inc. + * + * This file is part of Canvas. + * + * Canvas is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License as published by the Free + * Software Foundation, version 3 of the License. + * + * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more + * details. + * + * You should have received a copy of the GNU Affero General Public License along + * with this program. If not, see . + */ + +const {transform} = require('babel-core') +const {compile} = require('../frontend_build/i18nLinerHandlebars') + +exports.process = (source, path) => { + const amd = compile(source, path) + const cjs = transform(amd, {plugins: ['transform-amd-to-commonjs']}).code + return cjs +} \ No newline at end of file