Skip to content

Commit

Permalink
Merge pull request #42 from arcticicestudio/feature/gh-39-basic-testt…
Browse files Browse the repository at this point in the history
…ing-setup

Basic Testting Setup
  • Loading branch information
arcticicestudio authored Nov 24, 2018
2 parents d2e72e3 + fe7278e commit 7a4d356
Show file tree
Hide file tree
Showing 16 changed files with 2,616 additions and 141 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @author Sven Greb <[email protected]>
* @see https://github.com/babel/eslint-plugin-babel#rules
* @see https://github.com/tc39/proposal-optional-chaining
* @see https://eslint.org/docs/user-guide/configuring#specifying-environments
*/

const { resolve } = require("path");
Expand All @@ -23,6 +24,7 @@ module.exports = {
parser: "babel-eslint",
env: {
browser: true,
jest: true,
node: true
},
settings: {
Expand Down
76 changes: 41 additions & 35 deletions .gatsby/onCreateBabelConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
* @since 0.1.0
*/
const onCreateBabelConfig = ({ actions }) => {
const r = m => require.resolve(m);
const isProductionMode = () => process.env.NODE_ENV === "production";

/*
* Allows to use the "ES Class Fields & Static Properties" proposal to transforms static class properties as well as
* properties declared with the experimental property initializer syntax.
*
* References:
* - https://github.com/tc39/proposal-class-fields
* - https://babeljs.io/docs/en/babel-plugin-proposal-class-properties
* @see https://github.com/tc39/proposal-class-fields
* @see https://babeljs.io/docs/en/babel-plugin-proposal-class-properties
*/
actions.setBabelPlugin({
name: "@babel/plugin-proposal-class-properties",
Expand All @@ -47,19 +46,17 @@ const onCreateBabelConfig = ({ actions }) => {
/*
* Allows to use the experimental `export { default} from "mod"` proposal syntax.
*
* References:
* - https://github.com/tc39/proposal-export-default-from
* - https://babeljs.io/docs/en/babel-plugin-proposal-export-default-from
* @see https://github.com/tc39/proposal-export-default-from
* @see https://babeljs.io/docs/en/babel-plugin-proposal-export-default-from
*/
actions.setBabelPlugin({ name: "@babel/plugin-proposal-export-default-from" });

/*
* Allows to use the "Nullish Coalescing" proposal trhough the experimental `??` operator to combine with the
* "Optional Chaining" proposal operator.
*
* References:
* - https://github.com/tc39/proposal-nullish-coalescing
* - https://babeljs.io/docs/en/babel-plugin-proposal-nullish-coalescing-operator
* @see https://github.com/tc39/proposal-nullish-coalescing
* @see https://babeljs.io/docs/en/babel-plugin-proposal-nullish-coalescing-operator
*/
actions.setBabelPlugin({
name: "@babel/plugin-proposal-nullish-coalescing-operator",
Expand All @@ -72,9 +69,8 @@ const onCreateBabelConfig = ({ actions }) => {
* Allows to use the "Optional Chaining" proposal through the experimental `?.` operator in combination with the
* "Nullish Coalescing" proposal operator.
*
* References:
* - https://github.com/tc39/proposal-optional-chaining
* - https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining
* @see https://github.com/tc39/proposal-optional-chaining
* @see https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining
*/
actions.setBabelPlugin({
name: "@babel/plugin-proposal-optional-chaining",
Expand All @@ -83,29 +79,39 @@ const onCreateBabelConfig = ({ actions }) => {
}
});

/*
* Removes unnecessary React `propTypes` from production builds.
*
* @see https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types
*/
actions.setBabelOptions({
options: {},
config: {
env: {
production: {
plugins: [
[
r("babel-plugin-transform-react-remove-prop-types"),
{
removeImport: true,
ignoreFilenames: ["node_modules"]
}
]
]
}
if (isProductionMode) {
/*
* Removes unnecessary React `propTypes` from production builds to reduce bundle sizes and save bandwidth.
*
* @see https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types
*/
actions.setBabelPlugin({
name: "babel-plugin-transform-react-remove-prop-types",
options: {
removeImport: true,
ignoreFilenames: ["node_modules"]
}
}
});
});

/*
* Removes unnecessary React properties from production builds to reduce bundle sizes and save bandwidth.
*
* @see https://github.com/oliviertassinari/babel-plugin-react-remove-properties
*/
actions.setBabelPlugin({
name: "babel-plugin-react-remove-properties",
options: {
properties: [
/*
* Remove all test IDs applied for `react-testing-library` to query elements in tests.
*
* @see https://github.com/kentcdodds/react-testing-library#getbytestid
*/
"data-testid"
]
}
});
}
};

module.exports = onCreateBabelConfig;
6 changes: 4 additions & 2 deletions .gatsby/onCreateWebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const GitRevisionPlugin = require("git-revision-webpack-plugin");
/* eslint-enable import/no-extraneous-dependencies */

const { BASE_DIR_BUILD_REPORTS } = require("../src/config/internal/constants");

const r = m => resolvePath(__dirname, m);

/**
Expand All @@ -36,8 +38,8 @@ const bundleAnalyzerPluginConfig = {
analyzerMode: "static",
generateStatsFile: true,
openAnalyzer: false,
reportFilename: r("../build/reports/webpack-bundle-analyzer/index.html"),
statsFilename: r("../build/reports/webpack-bundle-analyzer/stats.json")
reportFilename: r(`../${BASE_DIR_BUILD_REPORTS}/webpack-bundle-analyzer/index.html`),
statsFilename: r(`../${BASE_DIR_BUILD_REPORTS}/webpack-bundle-analyzer/stats.json`)
};

/**
Expand Down
113 changes: 113 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (C) 2018-present Arctic Ice Studio <[email protected]>
* Copyright (C) 2018-present Sven Greb <[email protected]>
*
* Project: Nord Docs
* Repository: https://github.com/arcticicestudio/nord-docs
* License: MIT
*/

/**
* @file The Jest configuration.
* @author Arctic Ice Studio <[email protected]>
* @author Sven Greb <[email protected]>
* @see https://jestjs.io/docs/en/configuration
* @see https://www.gatsbyjs.org/docs/testing
* @since 0.1.0
*/

const { BASE_DIR_BUILD_REPORTS_COVERAGE } = require("./src/config/internal/constants");

module.exports = {
/*
* The directory where Jest should output its coverage files.
*/
coverageDirectory: `<rootDir>/${BASE_DIR_BUILD_REPORTS_COVERAGE}`,

/*
* To ensure only relevant files are included in the coverage stats this array of glob patterns matches files for
* which coverage information should be collected.
*/
collectCoverageFrom: ["**/src/**/*.{js,jsx}", "!**/src/{config,data}/**", "!**/.cache/**", "!**/node_modules/**"],

/*
* A set of global variables that need to be available in all test environments. The `__PATH_PREFIX__` variable is
* specific to Gatsby based projects which is necessary for some components.
*/
globals: {
__PATH_PREFIX__: ""
},

/*
* A map from regular expressions to module names that allow to stub out resources, like images or styles with a
* single module. It is also used to set up "resolve aliases" that reflect the same setup like the ones configured for
* Gatsby's Webpack configuration.
*/
moduleNameMapper: {
/* Reflect Webpack's `resolve.alias` configuration. */
"^assets(.*)$": "<rootDir>/src/assets$1",
"^atoms(.*)$": "<rootDir>/src/components/atoms$1",
"^config(.*)$": "<rootDir>/src/config$1",
"^containers(.*)$": "<rootDir>/src/components/containers$1",
"^data(.*)$": "<rootDir>/src/data$1",
"^layouts(.*)$": "<rootDir>/src/components/layouts$1",
"^molecules(.*)$": "<rootDir>/src/components/molecules$1",
"^organisms(.*)$": "<rootDir>/src/components/organisms$1",
"^pages(.*)$": "<rootDir>/src/pages$1",
"^stores(.*)$": "<rootDir>/src/stores$1",
"^styles(.*)$": "<rootDir>/src/styles$1",
"^templates(.*)$": "<rootDir>/src/components/templates$1",
"^utils(.*)$": "<rootDir>/src/utils$1",
/* Map all import stylesheets to the "identity object proxy" module. */
".+\\.(css|styl|less|sass|scss)$": "identity-obj-proxy",
".+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
"<rootDir>/test/__mocks__/file.js"
},

/*
* An array with paths to additional locations to search when resolving modules. This is useful to define test
* specific utils which can then be imported like an aliased module.
*/
modulePaths: ["<rootDir>/test/__utils__"],

/*
* The paths to modules that run some code to configure or set up the testing environment before each test.
* The `___loader` shim is a global function used by internal Gatsby APIs.
* Note that this is executed BEFORE the `setupTestFrameworkScriptFile` option!
*/
setupFiles: ["<rootDir>/test/__shims__/___loader.js"],

/*
* The path to the module that runs to configure or set up the testing framework before each test.
* Note that this is executed AFTER the `setupFiles` option!
*/
setupTestFrameworkScriptFile: require.resolve("./test/setup.js"),

/*
* An array of regexp pattern strings that are matched against all test paths before executing the test. If the test
* path matches any of the patterns, it will be skipped.
*/
testPathIgnorePatterns: ["node_modules", ".cache"],

/*
* To write tests using the latest ECMAScript syntax and
* proposals, Babel must be in place and set up to transpile the sources before they are processed by Jest to run
* them. This mapping from regular expressions to paths of transformers will transpile matching files with the
* specified Babel config.
* See the linked Jest documentation about how to use Jest with Babel for more details.
*
* @see https://jestjs.io/docs/en/getting-started#using-babel
*/
transform: {
"^.+\\.jsx?$": "<rootDir>/test/babel-config.js"
},

/*
* This is an important and required option for Gatsby based projects since Gastby includes un-transpiled ES6 code
* and by default Jest doesn't try to transform code inside `node_modules`, therefore the` gatsby-browser-entry.js`
* file isn't transpiled before running Jest so the `gatsby` module must be excluded.
* This array of regexp pattern strings that are matched against all source file paths before transformation. If the
* test path matches any of the patterns, it will not be transformed.
*/
transformIgnorePatterns: ["node_modules/(?!(gatsby)/)"]
};
Loading

0 comments on commit 7a4d356

Please sign in to comment.