Skip to content

Commit

Permalink
Set up jest-puppeteer and jest-image-snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
jhuynh85 committed Apr 8, 2019
1 parent eabb0a7 commit ba6c9f0
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ module.exports = {
},
globals: {
alert: "readonly",
browser: true,
context: true,
document: "readonly",
fetch: "readonly",
FormData: "readonly",
Expand All @@ -54,6 +56,7 @@ module.exports = {
location: "readonly",
navigator: "readonly",
Notification: "readonly",
page: true,
window: "readonly",
__PATH_PREFIX__: true,
},
Expand Down
6 changes: 3 additions & 3 deletions gatsby-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if (process.env.LOCAL_BUILD || process.env.NODE_ENV === "development") {
if (process.env.LOCAL_BUILD || process.env.NODE_ENV !== "production") {
require("dotenv").config({ path: ".env.dev" })
}

Expand Down Expand Up @@ -72,7 +72,7 @@ const plugins = [
entities: ["events", "venues"],
},
},
"gatsby-plugin-netlify-cms"
"gatsby-plugin-netlify-cms",
]

if (process.env.NODE_ENV === "production") {
Expand Down Expand Up @@ -106,7 +106,7 @@ const config = {
"San Diego Tech Hub represents a movement aimed at changing the perception of the San Diego tech ecosystem. Our focus is to be a conduit for change connecting businesses, organizations, and individuals, leveraging their resources and talents to build a stronger San Diego tech community through collaboration.",
author: "Claude Jones",
twitterHandle: "@SanDiegoTechHub",
image: "https://www.SanDiegoTechHub.com/circle-logo.png"
image: "https://www.SanDiegoTechHub.com/circle-logo.png",
},
plugins,
}
Expand Down
8 changes: 8 additions & 0 deletions jest-puppeteer.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
server: {
command: "gatsby develop",
launchTimeout: 20000,
port: 8000,
debug: true,
},
}
39 changes: 39 additions & 0 deletions jest.visual.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module.exports = {
preset: "jest-puppeteer",
globals: {
__PATH_PREFIX__: "",
},
moduleFileExtensions: [
"js", "jsx", "json",
],
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",
"^Components/(.*)$": "<rootDir>/src/components/$1",
"^Common/(.*)$": "<rootDir>/src/components/common/$1",
"^Test/(.*)$": "<rootDir>/test-env/$1",
"^Utils/(.*)$": "<rootDir>/src/utils/$1",
},
setupFiles: [
"<rootDir>/test-env/loadershim.js",
],
setupFilesAfterEnv: [
"<rootDir>/test-env/setup-test-env.js",
],
testPathIgnorePatterns: [
"/node_modules/",
"<rootDir>/.cache/",
"<rootDir>/public/",
],
testRegex: "(visualtest|visualspec)\\.[jt]sx?$",
testURL: "http://localhost",
transform: {
"^.+\\.jsx?$": "<rootDir>/test-env/jest-preprocess.js",
},
transformIgnorePatterns: ["node_modules/(?!(gatsby)/)"],
watchPathIgnorePatterns: [
"<rootDir>/node_modules/",
"<rootDir>/.cache/",
"<rootDir>/public/",
],
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"clearCache": "rm -rf ./.cache",
"serve": "gatsby serve",
"start": "gatsby develop",
"test": "jest --watchAll"
"test": "jest --watchAll",
"test:visual": "jest -c jest.visual.config.js --runInBand"
},
"husky": {
"hooks": {
Expand Down Expand Up @@ -110,7 +111,10 @@
"jest": "^24.3.1",
"jest-dom": "^3.1.2",
"jest-fetch-mock": "^2.1.1",
"jest-image-snapshot": "^2.8.1",
"jest-puppeteer": "^4.1.0",
"lint-staged": "^8.1.4",
"puppeteer": "^1.14.0",
"react-testing-library": "^6.0.0"
},
"license": "MIT"
Expand Down
14 changes: 14 additions & 0 deletions src/__visualtests__/404.visualtest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { port } = require("../../jest-puppeteer.config").server

const url = `http://localhost:${port}/404`

describe("404 page", () => {
beforeAll(async () => {
await page.goto(`${url}`)
})

it("works", async () => {
const image = await page.screenshot()
await expect(image).toMatchImageSnapshot()
})
})
14 changes: 14 additions & 0 deletions src/__visualtests__/index.visualtest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { port } = require("../../jest-puppeteer.config").server

const url = `http://localhost:${port}`

describe("Home page", () => {
beforeAll(async () => {
await page.goto(`${url}`)
})

it("works", async () => {
const image = await page.screenshot()
await expect(image).toMatchImageSnapshot()
})
})
7 changes: 7 additions & 0 deletions test-env/setup-test-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ import "react-testing-library/cleanup-after-each"
import MockFetch from "jest-fetch-mock"

global.fetch = MockFetch

// Setup for visual testing
require("expect-puppeteer")
const { toMatchImageSnapshot } = require("jest-image-snapshot")

expect.extend({ toMatchImageSnapshot })
jest.setTimeout(25000)

0 comments on commit ba6c9f0

Please sign in to comment.