Skip to content

Commit 3954944

Browse files
author
Josh Johnson
authored
fix(gatsby-remark-images): regenerate markdown when used image changes (#34433)
1 parent f5bb0b6 commit 3954944

File tree

19 files changed

+399
-105
lines changed

19 files changed

+399
-105
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Hello World
3+
date: 2018-12-14
4+
---
5+
6+
This is a truly meaningful blog post
7+
8+
![Image](../src/images/image.png)

e2e-tests/development-runtime/cypress/integration/hot-reloading/non-js-file.js

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,68 @@ const TEST_ID = `sub-title`
33
const message = `This is a sub-title`
44

55
describe(`hot reloading non-js file`, () => {
6-
beforeEach(() => {
7-
cy.exec(
8-
`npm run update -- --file content/2018-12-14-hello-world.md --replacements "${message}:%${TEMPLATE}%" --exact`
9-
)
10-
cy.wait(1000)
6+
describe(`markdown`, () => {
7+
beforeEach(() => {
8+
cy.exec(
9+
`npm run update -- --file content/2018-12-14-hello-world.md --replacements "${message}:%${TEMPLATE}%" --exact`
10+
)
11+
cy.wait(1000)
12+
13+
cy.visit(`/2018-12-14-hello-world/`).waitForRouteChange()
14+
15+
cy.wait(1000)
16+
})
17+
18+
it(`displays placeholder content on launch`, () => {
19+
cy.getTestElement(TEST_ID).invoke(`text`).should(`contain`, TEMPLATE)
20+
})
21+
22+
it(`hot reloads with new content`, () => {
23+
cy.getTestElement(TEST_ID).invoke(`text`).should(`contain`, TEMPLATE)
24+
25+
cy.exec(
26+
`npm run update -- --file content/2018-12-14-hello-world.md --replacements "${TEMPLATE}:${message}"`
27+
)
28+
29+
// wait for socket.io to update
30+
cy.wait(5000)
31+
32+
cy.getTestElement(TEST_ID).invoke(`text`).should(`eq`, message)
33+
})
34+
})
1135

12-
cy.visit(`/2018-12-14-hello-world/`).waitForRouteChange()
36+
describe(`image`, () => {
37+
beforeEach(() => {
38+
cy.visit(`/md-image/`).waitForRouteChange()
39+
cy.wait(1000)
1340

14-
cy.wait(1000)
15-
})
41+
cy.exec(
42+
`npm run update -- --file src/images/image.png --copy "src/images/original.png"`
43+
)
44+
cy.wait(2000)
45+
})
1646

17-
it(`displays placeholder content on launch`, () => {
18-
cy.getTestElement(TEST_ID).invoke(`text`).should(`contain`, TEMPLATE)
19-
})
47+
const runImageSnapshot = (snapshotName) => {
48+
cy.get(`.gatsby-resp-image-wrapper`)
49+
.find("img")
50+
.each(($el, i) => {
51+
cy.wrap($el).matchImageSnapshot(`${snapshotName}-${i}`)
52+
})
53+
}
2054

21-
it(`hot reloads with new content`, () => {
22-
cy.getTestElement(TEST_ID).invoke(`text`).should(`contain`, TEMPLATE)
55+
it(`displays original content on launch`, () => {
56+
runImageSnapshot(`non-js-file--image-original`)
57+
})
2358

24-
cy.exec(
25-
`npm run update -- --file content/2018-12-14-hello-world.md --replacements "${TEMPLATE}:${message}"`
26-
)
59+
it(`hot reloads with new content`, () => {
60+
cy.exec(
61+
`npm run update -- --file src/images/image.png --copy "src/images/new.png"`
62+
)
2763

28-
// wati for socket.io to update
29-
cy.wait(5000)
64+
// wait for socket.io to update
65+
cy.wait(5000)
3066

31-
cy.getTestElement(TEST_ID).invoke(`text`).should(`eq`, message)
67+
runImageSnapshot(`non-js-file--image-new`)
68+
})
3269
})
3370
})

e2e-tests/development-runtime/cypress/integration/unified-routing/collection-routing.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ describe(`collection-routing`, () => {
1313
})
1414

1515
it(`can navigate to a collection route and see its content rendered`, () => {
16+
// this test depends on the alphabetical sorting of markdown files
1617
cy.findByTestId(`collection-routing-blog-0`)
17-
cy.should(`have.attr`, `data-testslug`, `/2018-12-14-hello-world/`)
18+
.should(`have.attr`, `data-testslug`, `/2018-12-14-hello-world/`)
1819
.click()
1920
cy.waitForRouteChange()
2021
.assertRoute(`/collection-routing/2018-12-14-hello-world/`)
@@ -25,8 +26,9 @@ describe(`collection-routing`, () => {
2526
})
2627

2728
it(`can navigate to a collection route that uses unions and see its content rendered`, () => {
29+
// this test depends on the alphabetical sorting of image files
2830
cy.findByTestId(`collection-routing-image-0`)
29-
cy.should(`have.attr`, `data-testimagename`, `gatsby-astronaut`)
31+
.should(`have.attr`, `data-testimagename`, `gatsby-astronaut`)
3032
.click()
3133
cy.waitForRouteChange()
3234
.assertRoute(`/collection-routing/gatsby-astronaut/`)

e2e-tests/development-runtime/cypress/plugins/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// the project's config changing)
1313
const blockResources = require(`./block-resources`)
1414
const gatsbyConfig = require(`./gatsby-config`)
15+
const { addMatchImageSnapshotPlugin } = require("cypress-image-snapshot/plugin")
1516

1617
module.exports = (on, config) => {
1718
// `on` is used to hook into various events Cypress emits
@@ -20,4 +21,13 @@ module.exports = (on, config) => {
2021
...blockResources,
2122
...gatsbyConfig,
2223
})
24+
25+
addMatchImageSnapshotPlugin(on, config)
26+
on("before:browser:launch", (browser = {}, launchOptions) => {
27+
if (browser.family === "chromium" || browser.family === "chrome") {
28+
// Make retina screens run at 1x density so they match the versions in CI
29+
launchOptions.args.push("--force-device-scale-factor=1")
30+
}
31+
return launchOptions
32+
})
2333
}
29.8 KB
Loading
29 KB
Loading

e2e-tests/development-runtime/cypress/support/commands.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import "@testing-library/cypress/add-commands"
2+
import { addMatchImageSnapshotCommand } from "cypress-image-snapshot/command"
3+
import "gatsby-cypress"
24

35
Cypress.Commands.add(`lifecycleCallCount`, action =>
46
cy
@@ -113,3 +115,12 @@ Cypress.Commands.add(`getFastRefreshOverlay`, () => (
113115
Cypress.Commands.add(`assertNoFastRefreshOverlay`, () => (
114116
cy.get('gatsby-fast-refresh').should('not.exist')
115117
))
118+
119+
addMatchImageSnapshotCommand({
120+
customDiffDir: `/__diff_output__`,
121+
customDiffConfig: {
122+
threshold: 0.1,
123+
},
124+
failureThreshold: 0.08,
125+
failureThresholdType: `percent`,
126+
})

e2e-tests/development-runtime/gatsby-config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ module.exports = {
3333
{
3434
resolve: `gatsby-transformer-remark`,
3535
options: {
36-
plugins: [`gatsby-remark-subcache`],
36+
plugins: [
37+
`gatsby-remark-subcache`,
38+
`gatsby-remark-images`
39+
],
3740
},
3841
},
3942
`gatsby-plugin-sharp`,

e2e-tests/development-runtime/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"author": "Dustin Schau <[email protected]>",
66
"dependencies": {
77
"babel-plugin-search-and-replace": "^1.1.0",
8-
"gatsby": "^3.0.0-next.6",
8+
"gatsby": "^4.6.0-next.4",
99
"gatsby-image": "^3.0.0-next.0",
1010
"gatsby-plugin-image": "^1.0.0-next.5",
1111
"gatsby-plugin-less": "^5.1.0-next.2",
@@ -15,10 +15,11 @@
1515
"gatsby-plugin-sass": "^4.1.0-next.2",
1616
"gatsby-plugin-sharp": "^3.0.0-next.5",
1717
"gatsby-plugin-stylus": "^3.1.0-next.2",
18+
"gatsby-remark-images": "^6.6.0-next.2",
1819
"gatsby-seo": "^0.1.0",
1920
"gatsby-source-filesystem": "^3.0.0-next.2",
2021
"gatsby-transformer-json": "^3.0.0-next.0",
21-
"gatsby-transformer-remark": "^3.0.0-next.0",
22+
"gatsby-transformer-remark": "^5.6.0-next.2",
2223
"gatsby-transformer-sharp": "^3.0.0-next.1",
2324
"node-fetch": "^2.6.1",
2425
"prop-types": "^15.6.2",
@@ -56,6 +57,7 @@
5657
"@testing-library/cypress": "^7.0.0",
5758
"cross-env": "^5.2.0",
5859
"cypress": "6.1.0",
60+
"cypress-image-snapshot": "^4.0.1",
5961
"fs-extra": "^7.0.1",
6062
"gatsby-core-utils": "^2.12.0",
6163
"gatsby-cypress": "^0.1.7",

e2e-tests/development-runtime/scripts/update.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const args = yargs
1313
default: [],
1414
type: `array`,
1515
})
16+
.option(`copy`, {
17+
default: undefined,
18+
type: `string`,
19+
})
1620
.option(`exact`, {
1721
default: false,
1822
type: `boolean`,
@@ -50,7 +54,7 @@ const args = yargs
5054
async function update() {
5155
const history = await getHistory()
5256

53-
const { file: fileArg, replacements, restore } = args
57+
const { file: fileArg, replacements, restore, copy } = args
5458
const filePath = path.resolve(fileArg)
5559
if (restore) {
5660
const original = history.get(filePath)
@@ -85,6 +89,9 @@ async function update() {
8589
if (exists) {
8690
await fs.remove(filePath)
8791
}
92+
} else if(args.copy) {
93+
const copyFileContent = await fs.readFile(args.copy)
94+
await fs.writeFile(filePath, copyFileContent)
8895
} else {
8996
const contents = replacements.reduce((replaced, pair) => {
9097
const [key, value] = pair.split(`:`)

0 commit comments

Comments
 (0)