Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

Commit

Permalink
add import.meta.env support to test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Jun 9, 2020
1 parent 1207a1c commit c84d51b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
5 changes: 2 additions & 3 deletions packages/app-scripts-react/jest/babelTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
"use strict";

const babelJest = require("babel-jest");
const importMetaBabelPlugin = require("./importMetaBabelPlugin");

module.exports = babelJest.createTransformer({
presets: [
"babel-preset-react-app",
"@babel/preset-react",
"@babel/preset-typescript",
],
plugins: [
["@babel/plugin-syntax-import-meta"],
],
plugins: [[importMetaBabelPlugin]],
});
31 changes: 31 additions & 0 deletions packages/app-scripts-react/jest/importMetaBabelPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const template = require("@babel/template").default;

const PUBLIC_ENV_REGEX = /^SNOWPACK_PUBLIC_/;
function generateEnvObject(mode) {
const envObject = { ...process.env };
for (const env of Object.keys(envObject)) {
if (!PUBLIC_ENV_REGEX.test(env)) {
delete envObject[env];
}
}
envObject.MODE = mode;
envObject.NODE_ENV = mode;
return envObject;
}

/**
* Add import.meta.env support
* Note: import.meta.url is not supported at this time
*/
module.exports = function () {
const ast = template.ast(`
({env: ${JSON.stringify(generateEnvObject("test"))}})
`);
return {
visitor: {
MetaProperty(path, state) {
path.replaceWith(ast);
},
},
};
};
3 changes: 2 additions & 1 deletion packages/app-scripts-svelte/jest/babelTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"use strict";

const babelJest = require("babel-jest");
const importMetaBabelPlugin = require("./importMetaBabelPlugin");

module.exports = babelJest.createTransformer({
presets: [
Expand All @@ -20,5 +21,5 @@ module.exports = babelJest.createTransformer({
},
],
],
plugins: [],
plugins: [[importMetaBabelPlugin]],
});
31 changes: 31 additions & 0 deletions packages/app-scripts-svelte/jest/importMetaBabelPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const template = require("@babel/template").default;

const PUBLIC_ENV_REGEX = /^SNOWPACK_PUBLIC_/;
function generateEnvObject(mode) {
const envObject = { ...process.env };
for (const env of Object.keys(envObject)) {
if (!PUBLIC_ENV_REGEX.test(env)) {
delete envObject[env];
}
}
envObject.MODE = mode;
envObject.NODE_ENV = mode;
return envObject;
}

/**
* Add import.meta.env support
* Note: import.meta.url is not supported at this time
*/
module.exports = function () {
const ast = template.ast(`
({env: ${JSON.stringify(generateEnvObject("test"))}})
`);
return {
visitor: {
MetaProperty(path, state) {
path.replaceWith(ast);
},
},
};
};

0 comments on commit c84d51b

Please sign in to comment.