Skip to content

Commit

Permalink
Add jest config
Browse files Browse the repository at this point in the history
  • Loading branch information
samajammin committed Dec 9, 2020
1 parent e7ff4a5 commit cf65fbb
Show file tree
Hide file tree
Showing 7 changed files with 2,273 additions and 31 deletions.
1 change: 1 addition & 0 deletions __mocks__/file-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "test-file-stub"
27 changes: 27 additions & 0 deletions __mocks__/gatsby.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const React = require("react")
const gatsby = jest.requireActual("gatsby")

module.exports = {
...gatsby,
graphql: jest.fn(),
Link: jest.fn().mockImplementation(
// these props are invalid for an `a` tag
({
activeClassName,
activeStyle,
getProps,
innerRef,
partiallyActive,
ref,
replace,
to,
...rest
}) =>
React.createElement("a", {
...rest,
href: to,
})
),
StaticQuery: jest.fn(),
useStaticQuery: jest.fn(),
}
8 changes: 8 additions & 0 deletions jest-preprocess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Configuration file for Jest testing
// https://www.gatsbyjs.com/docs/unit-testing/

const babelOptions = {
presets: ["babel-preset-gatsby"],
}

module.exports = require("babel-jest").createTransformer(babelOptions)
16 changes: 16 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
transform: {
"^.+\\.jsx?$": `<rootDir>/jest-preprocess.js`,
},
moduleNameMapper: {
".+\\.(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>/__mocks__/file-mock.js`,
},
testPathIgnorePatterns: [`node_modules`, `\\.cache`, `<rootDir>.*/public`],
transformIgnorePatterns: [`node_modules/(?!(gatsby)/)`],
globals: {
__PATH_PREFIX__: ``,
},
testURL: `http://localhost`,
setupFiles: [`<rootDir>/loadershim.js`],
}
3 changes: 3 additions & 0 deletions loadershim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global.___loader = {
enqueue: jest.fn(),
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@
"styled-system": "^5.1.5"
},
"devDependencies": {
"babel-jest": "^26.6.3",
"babel-preset-gatsby": "^0.8.0",
"github-slugger": "^1.3.0",
"husky": "^4.2.5",
"prettier": "^2.2.1",
"pretty-quick": "^3.1.0"
"pretty-quick": "^3.1.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^26.6.3",
"react-test-renderer": "^17.0.1"
},
"scripts": {
"build": "yarn copy-contributors && yarn build:app && yarn build:lambda",
Expand All @@ -71,7 +76,7 @@
"start": "gatsby develop",
"start:static": "gatsby build && gatsby serve",
"serve": "gatsby serve",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
"test": "jest"
},
"husky": {
"hooks": {
Expand Down
Loading

0 comments on commit cf65fbb

Please sign in to comment.