Skip to content

Commit

Permalink
Implement Jest's Babel configuration file
Browse files Browse the repository at this point in the history
To write tests using the latest ECMAScript syntax and proposals,
Babel (1) must be in place and set up to transpile the sources before
passing them to Jest to run them.
This step is documented in the sections "Creating a configuration file
for Jest" (2) of the Gatsby guide about the preprocess file and the Jest
docs about "Using Babel" (3).

The file is placed in the base `test` folder within the project root.
It initially uses the `babel-preset-gatsby` (4) preset package which
includes all necessary Babel plugins to write tests for Gatsby based
projects. This includes all plugins mentioned in the Jest Babel setup
guide and additionally adds useful syntax proposal plugins that are also
used in the project sources like e.g.
`babel-plugin-proposal-class-properties` (5) and
`babel-plugin-proposal-optional-chaining` (6).
The the custom transformer is exported using babel-jest's
`createTransformer` function (7) which takes the created Babel config
object as parameter.

References:
  (1) https://babeljs.io
  (2) https://www.gatsbyjs.org/docs/unit-testing/#2-creating-a-configuration-file-for-jest
  (3) https://jestjs.io/docs/en/getting-started#using-babel
  (4) https://www.npmjs.com/package/babel-preset-gatsby
  (5) https://babeljs.io/docs/en/babel-plugin-proposal-class-properties
  (6) https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining
  (7) https://jestjs.io/docs/en/tutorial-react#custom-transformers

GH-39
  • Loading branch information
arcticicestudio committed Nov 24, 2018
1 parent ca8d810 commit 82f2db3
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions test/babel-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 Babel configuration for Jest.
* @author Arctic Ice Studio <[email protected]>
* @author Sven Greb <[email protected]>
* @see https://jestjs.io/docs/en/getting-started#using-babel
* @see https://www.gatsbyjs.org/docs/testing
* @since 0.1.0
*/

const jestBabelConfig = {
presets: ["babel-preset-gatsby"]
};

module.exports = require("babel-jest").createTransformer(jestBabelConfig);

0 comments on commit 82f2db3

Please sign in to comment.