Skip to content

Commit

Permalink
Fix react native test setup (reworked jest configs)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Oct 4, 2019
1 parent 616d123 commit 3a924db
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 107 deletions.
37 changes: 25 additions & 12 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
module.exports = {
babelrcRoots: [".", "./website/*"],
presets: [["@babel/env", { loose: true }], "@babel/react"],
plugins: [
"dev-expression",
["@babel/plugin-proposal-class-properties", { loose: true }]
],
env: {
test: {
presets: [["@babel/preset-env", { targets: { node: "current" } }]]
}
}
const loose = true;

module.exports = api => {
return {
presets: [
[
"@babel/preset-env",
{
loose,
...(api.env("test") && { targets: { node: "current" } })
}
],
"@babel/react"
],
plugins: [
"dev-expression",
["@babel/plugin-proposal-class-properties", { loose }]
],
overrides: [
{
test: ["packages/react-router-native", "node_modules/react-native"],
presets: ["module:metro-react-native-babel-preset"]
}
]
};
};
40 changes: 40 additions & 0 deletions jest-preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { jest: lernaAliases } = require("lerna-alias");
const { TEST_ENV } = process.env;

const moduleType = ["cjs", "umd"].includes(TEST_ENV) ? TEST_ENV : "modules";

const mapValues = (obj, mapper) => {
const mapped = {};
Object.keys(obj).forEach(key => {
mapped[key] = mapper(obj[key]);
});
return mapped;
};

const mapAliasPathToSourceEntry = path =>
path.replace("/src/index", `/${moduleType}/index`);

const mapAliasPathToDistEntry = path =>
path.replace(/\/([a-z-]+)\/src\/index/, (match, pkgName) =>
match.replace("/src/index", `/${moduleType}/${pkgName}`)
);

module.exports = {
testRunner: "jest-circus/runner",
restoreMocks: true,
globals: {
__DEV__: true
},
moduleNameMapper: mapValues(
lernaAliases(),
moduleType === "modules"
? mapAliasPathToSourceEntry
: mapAliasPathToDistEntry
),
setupFiles: ["raf/polyfill"],
testMatch: ["**/__tests__/**/*-test.js"],
transform: {
"^.+\\.[jt]sx?$": "babel-jest"
},
testURL: "http://localhost/"
};
26 changes: 0 additions & 26 deletions jest.config.js

This file was deleted.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"build": "node ./scripts/build.js",
"clean": "git clean -fdX .",
"start": "node ./scripts/start.js",
"test": "jest --config jest.config.js"
"test": "jest --projects ./packages/*/"
},
"dependencies": {
"@babel/core": "^7.5.5",
Expand All @@ -14,8 +14,8 @@
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "^7.0.0",
"babel-eslint": "^10.0.3",
"babel-jest": "^24.9.0",
"babel-plugin-dev-expression": "^0.2.2",
"metro-react-native-babel-preset": "^0.56.0",
"eslint": "^6.3.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-react": "^7.14.3",
Expand All @@ -24,21 +24,22 @@
"jest": "^24.9.0",
"jest-circus": "^24.9.0",
"lerna": "^3.13.4",
"lerna-alias": "3.0.3-0",
"metro-react-native-babel-preset": "^0.56.0",
"prettier": "^1.14.3",
"pretty-quick": "^1.4.1",
"raf": "^3.4.1",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-native": "^0.61.1",
"react-test-renderer": "^16.8.3",
"rollup": "^1.20.3",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-size-snapshot": "^0.10.0",
"rollup-plugin-uglify": "^6.0.3",
"babel-jest": "^24.9.0",
"react-native": "^0.61.1",
"react-test-renderer": "^16.8.3"
"rollup-plugin-uglify": "^6.0.3"
},
"workspaces": {
"packages": [
Expand Down
1 change: 1 addition & 0 deletions packages/react-router-config/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { preset: "../../jest-preset.js" };
27 changes: 1 addition & 26 deletions packages/react-router-dom/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,26 +1 @@
let mappedModule;
switch (process.env.TEST_ENV) {
case "cjs":
mappedModule = "<rootDir>/cjs/react-router-dom.js";
break;
case "umd":
mappedModule = "<rootDir>/umd/react-router-dom.js";
break;
default:
mappedModule = "<rootDir>/modules/index.js";
}

module.exports = {
testRunner: "jest-circus/runner",
restoreMocks: true,
globals: {
__DEV__: true
},
moduleNameMapper: {
"^react-router-dom$": mappedModule
},
modulePaths: ["<rootDir>/node_modules"],
setupFiles: ["raf/polyfill"],
testMatch: ["**/__tests__/**/*-test.js"],
testURL: "http://localhost/"
};
module.exports = { preset: "../../jest-preset.js" };
55 changes: 35 additions & 20 deletions packages/react-router-native/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
// Bundle type "cjs" | "umd" | "modules"
const moduleType = process.env.TEST_ENV || "modules";
const preset = require("react-native/jest-preset");
const { jest: lernaAliases } = require("lerna-alias");

const mapValues = (obj, mapper) => {
const mapped = {};
Object.keys(obj).forEach(key => {
mapped[key] = mapper(obj[key]);
});
return mapped;
};

const omitBy = (obj, predicate) => {
const mapped = {};
Object.keys(obj).forEach(key => {
if (predicate(obj[key], key)) {
return;
}
mapped[key] = obj[key];
});
return mapped;
};

const monorepoAliases = lernaAliases();
const transpilableAliases = omitBy(monorepoAliases, (_, key) =>
key.includes("react-router-native")
);

module.exports = {
rootDir: ".",
preset: "react-native",
...preset,
testRunner: "jest-circus/runner",
restoreMocks: true,
moduleNameMapper: {
"^react-router$": `<rootDir>/../react-router/${moduleType}/index.js`
},
modulePaths: ["<rootDir>/node_modules"],
testMatch: ["<rootDir>/__tests__/**/*.js"],
transform: {
"^.+\\.[jt]sx?$": [
"babel-jest",
{
// Add Babel config inline to avoide Babel's file config merge behaviour
rootMode: "root",
presets: ["module:metro-react-native-babel-preset"],
plugins: [["@babel/plugin-proposal-class-properties", { loose: true }]]
}
]
}
moduleNameMapper: mapValues(transpilableAliases, path =>
path.replace("/src/index", `/modules/index`)
),
transform: mapValues(preset.transform, transformer =>
transformer === "babel-jest"
? ["babel-jest", { rootMode: "upward" }]
: transformer
)
};
1 change: 1 addition & 0 deletions packages/react-router/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { preset: "../../jest-preset.js" };
20 changes: 20 additions & 0 deletions website/modules/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"presets": [
[
"@babel/env",
{
"loose": true
}
],
"@babel/react"
],
"plugins": [
"dev-expression",
[
"@babel/proposal-class-properties",
{
"loose": true
}
]
]
}
9 changes: 0 additions & 9 deletions website/modules/.babelrc.js

This file was deleted.

23 changes: 15 additions & 8 deletions website/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ module.exports = {
].concat(
process.env.NODE_ENV === "production"
? [
new SWPrecacheWebpackPlugin({
cacheId: "react-router-website",
staticFileGlobsIgnorePatterns: [/\.map$/]
})
]
new SWPrecacheWebpackPlugin({
cacheId: "react-router-website",
staticFileGlobsIgnorePatterns: [/\.map$/]
})
]
: []
),

Expand Down Expand Up @@ -65,7 +65,10 @@ module.exports = {
{
test: /\.js$/,
exclude: /node_modules|examples/,
loader: "babel-loader"
use: {
loader: "babel-loader",
options: { rootMode: "upward" }
}
},
{
test: /\.js$/,
Expand All @@ -78,7 +81,10 @@ module.exports = {
lazy: true
}
},
{ loader: "babel-loader" }
{
loader: "babel-loader",
options: { rootMode: "upward" }
}
]
},
{
Expand Down Expand Up @@ -114,7 +120,8 @@ module.exports = {
test: /\.md(\?(.+))?$/,
loader: "markdown-loader",
options: {
basename: process.env.NODE_ENV === "production" ? "/react-router" : undefined
basename:
process.env.NODE_ENV === "production" ? "/react-router" : undefined
}
},
{
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4728,6 +4728,13 @@ get-caller-file@^2.0.1:
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==

get-lerna-packages@^0.1.1:
version "0.1.1"
resolved "https://registry.npmjs.org/get-lerna-packages/-/get-lerna-packages-0.1.1.tgz#37aaaac36fd14f00082f17b1096dd6c69290cef9"
integrity sha512-venxkvga57gOUZoTmXZlJ0zHc8cmvsx7Y0GTMcoEK/7OCEK3PLpG5gVTbUUMJaLxbzK13AWQdKPf1LSkuEfWqA==
dependencies:
glob "^7.1.2"

get-pkg-repo@^1.0.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/get-pkg-repo/-/get-pkg-repo-1.4.0.tgz#c73b489c06d80cc5536c2c853f9e05232056972d"
Expand Down Expand Up @@ -6241,6 +6248,13 @@ left-pad@^1.3.0:
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e"
integrity sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==

lerna-alias@3.0.3-0:
version "3.0.3-0"
resolved "https://registry.npmjs.org/lerna-alias/-/lerna-alias-3.0.3-0.tgz#d681842deed66644ab9f38a7b365658cfe52dd43"
integrity sha512-IX0c2QH3v4rkgE+iG7Ro0FSEWKqqrMhvbGbB1txrnaJ8YbcrNbJ0IPpcJi+uiiPLV3olNX74EO08x9hGZ+J5ig==
dependencies:
get-lerna-packages "^0.1.1"

lerna@^3.13.4:
version "3.16.4"
resolved "https://registry.yarnpkg.com/lerna/-/lerna-3.16.4.tgz#158cb4f478b680f46f871d5891f531f3a2cb31ec"
Expand Down

0 comments on commit 3a924db

Please sign in to comment.