Skip to content

Commit

Permalink
Implement Jest's main configuration file
Browse files Browse the repository at this point in the history
This commit implements the main configuration file of Jest. It follows
the "Setting up your environment" (1) guide of Gatsby and the Jest docs
about how to get started (2). The file is placed in the project root and
initially contains the following options:

- `coverageDirectory` - The directory where Jest should output its
  coverage files.
- `collectCoverageFrom` - 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.
- `globals` - 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.
- `moduleNameMapper` - 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.
- `modulePaths` - 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.
- `setupFiles` - 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!
- `setupTestFrameworkScriptFile` - 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!
- `testPathIgnorePatterns` - 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.
- `transform` - 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 Jest documentation (3)
  about how to use Jest with Babel for more details.
- `transformIgnorePatterns` - 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. The 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.

References:
  (1) https://www.gatsbyjs.org/docs/unit-testing/#setting-up-your-environment
  (2) https://jestjs.io/docs/en/getting-started
  (3) https://jestjs.io/docs/en/getting-started#using-babel

GH-39
  • Loading branch information
arcticicestudio committed Nov 24, 2018
1 parent 80a33c9 commit ca8d810
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 8 deletions.
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)/)"]
};
39 changes: 33 additions & 6 deletions src/config/internal/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,71 @@
*/

/**
* The absolute path of the content base directory starting from the project root.
* The relative path of the content base directory starting from the project root.
*
* @constant {string}
* @since 0.1.0
*/
const BASE_DIR_CONTENT = "content";

/**
* The absolute path of the sources base directory starting from the project root.
* The relative path of the sources base directory starting from the project root.
*
* @constant {string}
* @since 0.1.0
*/
const BASE_DIR_SRC = "src";

/**
* The absolute path of the assets base directory starting from the project root.
* The relative path of the assets base directory starting from the project root.
*
* @constant {string}
* @since 0.1.0
*/
const BASE_DIR_ASSETS = `${BASE_DIR_SRC}/assets`;

/**
* The absolute path of the assets directory for images starting from the project root.
* The relative path of the assets directory for images starting from the project root.
*
* @constant {string}
* @since 0.1.0
*/
const BASE_DIR_ASSETS_IMAGES = `${BASE_DIR_ASSETS}/images`;

/**
* The absolute path of the config base directory starting from the project root.
* The relative path of the build base directory starting from the project root.
*
* @constant {string}
* @since 0.1.0
*/
const BASE_DIR_BUILD = "build";

/**
* The relative path of the build base directory for reports starting from the project root.
*
* @constant {string}
* @since 0.1.0
*/
const BASE_DIR_BUILD_REPORTS = `${BASE_DIR_BUILD}/reports`;

/**
* The relative path of the build base directory for coverage reports starting from the project root.
*
* @constant {string}
* @since 0.1.0
*/
const BASE_DIR_BUILD_REPORTS_COVERAGE = `${BASE_DIR_BUILD_REPORTS}/coverage`;

/**
* The relative path of the config base directory starting from the project root.
*
* @constant {string}
* @since 0.1.0
*/
const BASE_DIR_CONFIG = `${BASE_DIR_SRC}/config`;

/**
* The absolute path of the pages base directory starting from the project root.
* The relative path of the pages base directory starting from the project root.
*
* @constant {string}
* @since 0.1.0
Expand All @@ -82,6 +106,9 @@ const REGEX_BLOG_POST_DATE = /([0-9]+)\/([0-9]+)\/([0-9]+)\/(.+)/;
module.exports = {
BASE_DIR_ASSETS,
BASE_DIR_ASSETS_IMAGES,
BASE_DIR_BUILD,
BASE_DIR_BUILD_REPORTS,
BASE_DIR_BUILD_REPORTS_COVERAGE,
BASE_DIR_CONFIG,
BASE_DIR_CONTENT,
BASE_DIR_PAGES,
Expand Down

0 comments on commit ca8d810

Please sign in to comment.