Skip to content

Commit 612120f

Browse files
authored
Fix recipe test problems (gatsbyjs#23347)
* Strip ansi colors on diff during tests * Use tmpdir to run fixtures * Fix snapshots having wrong fixture formatting
1 parent aae0713 commit 612120f

File tree

8 files changed

+68
-29
lines changed

8 files changed

+68
-29
lines changed

.circleci/config.yml

-4
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,12 @@ jobs:
205205

206206
unit_tests_node10:
207207
executor: node
208-
environment:
209-
FORCE_COLOR: 0
210208
<<: *test_template
211209

212210
unit_tests_node12:
213211
executor:
214212
name: node
215213
image: "12"
216-
environment:
217-
FORCE_COLOR: 0
218214
<<: *test_template
219215

220216
unit_tests_www:

.jestSetup.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
process.env.GATSBY_RECIPES_NO_COLOR = "true"

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ module.exports = {
5656
: [`default`].concat(useCoverage ? `jest-junit` : []),
5757
testEnvironment: `jest-environment-jsdom-fourteen`,
5858
moduleFileExtensions: [`js`, `jsx`, `ts`, `tsx`, `json`],
59+
setupFiles: [`<rootDir>/.jestSetup.js`],
5960
}

packages/gatsby-recipes/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@
6868
"babel-preset-gatsby-package": "^0.3.1",
6969
"react": "^16.12.0",
7070
"react-dom": "^16.12.0",
71-
"rimraf": "^3.0.2"
71+
"rimraf": "^3.0.2",
72+
"strip-ansi": "^6.0.0",
73+
"tmp-promise": "^2.0.2"
7274
},
7375
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-recipes#readme",
7476
"keywords": [

packages/gatsby-recipes/src/providers/gatsby/__snapshots__/plugin.test.js.snap

+14-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Object {
1818
module.exports = {
1919
/* Your site config here */
2020
plugins: [],
21-
}
21+
};
2222
",
2323
"describe": "Install gatsby-plugin-foo in gatsby-config.js",
2424
"diff": "- Original - 1
@@ -30,7 +30,7 @@ module.exports = {
3030
/* Your site config here */
3131
- plugins: [],
3232
+ plugins: [\\"gatsby-plugin-foo\\"],
33-
}
33+
};
3434
",
3535
"id": "gatsby-plugin-foo",
3636
"name": "gatsby-plugin-foo",
@@ -42,7 +42,7 @@ module.exports = {
4242
module.exports = {
4343
/* Your site config here */
4444
plugins: [\\"gatsby-plugin-foo\\"],
45-
}
45+
};
4646
",
4747
}
4848
`;
@@ -67,7 +67,7 @@ Object {
6767
module.exports = {
6868
/* Your site config here */
6969
plugins: [\\"gatsby-plugin-foo\\"],
70-
}
70+
};
7171
",
7272
"describe": "Install gatsby-plugin-foo in gatsby-config.js",
7373
"diff": "Compared values have no visual difference.",
@@ -81,7 +81,7 @@ module.exports = {
8181
module.exports = {
8282
/* Your site config here */
8383
plugins: [\\"gatsby-plugin-foo\\"],
84-
}
84+
};
8585
",
8686
}
8787
`;
@@ -96,7 +96,7 @@ Object {
9696

9797
exports[`gatsby-plugin resource e2e plugin resource test: GatsbyPlugin create plan 1`] = `
9898
Object {
99-
"currentState": "const redish = \`#c5484d\`
99+
"currentState": "const redish = \`#c5484d\`;
100100
module.exports = {
101101
siteMetadata: {
102102
title: \`Bricolage\`,
@@ -163,7 +163,7 @@ module.exports = {
163163
\`gatsby-plugin-offline\`, // \`gatsby-plugin-preact\`,
164164
\`gatsby-plugin-react-helmet\`,
165165
],
166-
}
166+
};
167167
",
168168
"describe": "Install gatsby-plugin-foo in gatsby-config.js",
169169
"diff": "- Original - 0
@@ -175,11 +175,11 @@ module.exports = {
175175
\`gatsby-plugin-react-helmet\`,
176176
+ \\"gatsby-plugin-foo\\",
177177
],
178-
}
178+
};
179179
",
180180
"id": "gatsby-plugin-foo",
181181
"name": "gatsby-plugin-foo",
182-
"newState": "const redish = \`#c5484d\`
182+
"newState": "const redish = \`#c5484d\`;
183183
module.exports = {
184184
siteMetadata: {
185185
title: \`Bricolage\`,
@@ -247,7 +247,7 @@ module.exports = {
247247
\`gatsby-plugin-react-helmet\`,
248248
\\"gatsby-plugin-foo\\",
249249
],
250-
}
250+
};
251251
",
252252
}
253253
`;
@@ -264,7 +264,7 @@ Object {
264264
265265
exports[`gatsby-plugin resource e2e plugin resource test: GatsbyPlugin update plan 1`] = `
266266
Object {
267-
"currentState": "const redish = \`#c5484d\`
267+
"currentState": "const redish = \`#c5484d\`;
268268
module.exports = {
269269
siteMetadata: {
270270
title: \`Bricolage\`,
@@ -332,13 +332,13 @@ module.exports = {
332332
\`gatsby-plugin-react-helmet\`,
333333
\\"gatsby-plugin-foo\\",
334334
],
335-
}
335+
};
336336
",
337337
"describe": "Install gatsby-plugin-foo in gatsby-config.js",
338338
"diff": "Compared values have no visual difference.",
339339
"id": "gatsby-plugin-foo",
340340
"name": "gatsby-plugin-foo",
341-
"newState": "const redish = \`#c5484d\`
341+
"newState": "const redish = \`#c5484d\`;
342342
module.exports = {
343343
siteMetadata: {
344344
title: \`Bricolage\`,
@@ -406,7 +406,7 @@ module.exports = {
406406
\`gatsby-plugin-react-helmet\`,
407407
\\"gatsby-plugin-foo\\",
408408
],
409-
}
409+
};
410410
",
411411
}
412412
`;

packages/gatsby-recipes/src/providers/gatsby/plugin.test.js

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
11
const fs = require(`fs-extra`)
22
const path = require(`path`)
3+
const tmp = require(`tmp-promise`)
34

45
const plugin = require(`./plugin`)
56
const { addPluginToConfig, getPluginsFromConfig } = require(`./plugin`)
67
const resourceTestHelper = require(`../resource-test-helper`)
78

8-
const root = path.join(__dirname, `./fixtures/gatsby-starter-blog`)
9-
const helloWorldRoot = path.join(
9+
const STARTER_BLOG_FIXTURE = path.join(
10+
__dirname,
11+
`./fixtures/gatsby-starter-blog`
12+
)
13+
const HELLO_WORLD_FIXTURE = path.join(
1014
__dirname,
1115
`./fixtures/gatsby-starter-hello-world`
1216
)
1317
const name = `gatsby-plugin-foo`
14-
const configPath = path.join(root, `gatsby-config.js`)
1518

1619
describe(`gatsby-plugin resource`, () => {
20+
let tmpDir
21+
let starterBlogRoot
22+
let helloWorldRoot
23+
let configPath
24+
beforeAll(async () => {
25+
tmpDir = await tmp.dir({
26+
unsafeCleanup: true,
27+
})
28+
starterBlogRoot = path.join(tmpDir.path, `gatsby-starter-blog`)
29+
helloWorldRoot = path.join(tmpDir.path, `gatsby-starter-hello-world`)
30+
configPath = path.join(helloWorldRoot, `gatsby-config.js`)
31+
await fs.ensureDir(starterBlogRoot)
32+
await fs.copy(STARTER_BLOG_FIXTURE, starterBlogRoot)
33+
await fs.ensureDir(helloWorldRoot)
34+
await fs.copy(HELLO_WORLD_FIXTURE, helloWorldRoot)
35+
})
36+
afterAll(async () => {
37+
if (tmpDir) {
38+
await tmpDir.cleanup()
39+
}
40+
})
41+
1742
test(`e2e plugin resource test`, async () => {
1843
await resourceTestHelper({
1944
resourceModule: plugin,
2045
resourceName: `GatsbyPlugin`,
21-
context: { root },
46+
context: { root: starterBlogRoot },
2247
initialObject: { id: name, name },
2348
partialUpdate: { id: name },
2449
})

packages/gatsby-recipes/src/providers/utils/get-diff.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const diff = require(`jest-diff`).default
22
const chalk = require(`chalk`)
3+
const stripAnsi = require(`strip-ansi`)
34

45
module.exports = async (oldVal, newVal) => {
56
const options = {
@@ -12,7 +13,11 @@ module.exports = async (oldVal, newVal) => {
1213
expand: false,
1314
}
1415

15-
const diffText = diff(oldVal, newVal, options)
16+
let diffText = diff(oldVal, newVal, options)
17+
18+
if (process.env.GATSBY_RECIPES_NO_COLOR) {
19+
diffText = stripAnsi(diffText)
20+
}
1621

1722
return diffText
1823
}

yarn.lock

+14-5
Original file line numberDiff line numberDiff line change
@@ -22685,11 +22685,6 @@ sudo-prompt@^8.2.0:
2268522685
resolved "https://registry.yarnpkg.com/sudo-prompt/-/sudo-prompt-8.2.5.tgz#cc5ef3769a134bb94b24a631cc09628d4d53603e"
2268622686
integrity sha512-rlBo3HU/1zAJUrkY6jNxDOC9eVYliG6nS4JA8u8KAshITd07tafMc/Br7xQwCSseXwJ2iCcHCE8SNWX3q8Z+kw==
2268722687

22688-
sudo-prompt@^8.2.0:
22689-
version "8.2.5"
22690-
resolved "https://registry.yarnpkg.com/sudo-prompt/-/sudo-prompt-8.2.5.tgz#cc5ef3769a134bb94b24a631cc09628d4d53603e"
22691-
integrity sha512-rlBo3HU/1zAJUrkY6jNxDOC9eVYliG6nS4JA8u8KAshITd07tafMc/Br7xQwCSseXwJ2iCcHCE8SNWX3q8Z+kw==
22692-
2269322688
[email protected], supports-color@^6.0.0, supports-color@^6.1.0:
2269422689
version "6.1.0"
2269522690
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-6.1.0.tgz#0764abc69c63d5ac842dd4867e8d025e880df8f3"
@@ -23204,6 +23199,20 @@ title-case@^2.1.0:
2320423199
no-case "^2.2.0"
2320523200
upper-case "^1.0.3"
2320623201

23202+
tmp-promise@^2.0.2:
23203+
version "2.0.2"
23204+
resolved "https://registry.yarnpkg.com/tmp-promise/-/tmp-promise-2.0.2.tgz#ee605edb10f100954be5dd8b9dbe1bfd56194202"
23205+
integrity sha512-zl71nFWjPKW2KXs+73gEk8RmqvtAeXPxhWDkTUoa3MSMkjq3I+9OeknjF178MQoMYsdqL730hfzvNfEkePxq9Q==
23206+
dependencies:
23207+
tmp "0.1.0"
23208+
23209+
23210+
version "0.1.0"
23211+
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.1.0.tgz#ee434a4e22543082e294ba6201dcc6eafefa2877"
23212+
integrity sha512-J7Z2K08jbGcdA1kkQpJSqLF6T0tdQqpR2pnSUXsIchbPdTI9v3e85cLW0d6WDhwuAleOV71j2xWs8qMPfK7nKw==
23213+
dependencies:
23214+
rimraf "^2.6.3"
23215+
2320723216
tmp@^0.0.33:
2320823217
version "0.0.33"
2320923218
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"

0 commit comments

Comments
 (0)