forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix react native test setup (reworked jest configs)
- Loading branch information
Showing
12 changed files
with
159 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
] | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = { preset: "../../jest-preset.js" }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = { preset: "../../jest-preset.js" }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters