Skip to content

Commit

Permalink
Fix recipe test problems (#23347)
Browse files Browse the repository at this point in the history
* Strip ansi colors on diff during tests
* Use tmpdir to run fixtures
* Fix snapshots having wrong fixture formatting
  • Loading branch information
freiksenet authored Apr 23, 2020
1 parent aae0713 commit 612120f
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 29 deletions.
4 changes: 0 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,12 @@ jobs:

unit_tests_node10:
executor: node
environment:
FORCE_COLOR: 0
<<: *test_template

unit_tests_node12:
executor:
name: node
image: "12"
environment:
FORCE_COLOR: 0
<<: *test_template

unit_tests_www:
Expand Down
1 change: 1 addition & 0 deletions .jestSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.env.GATSBY_RECIPES_NO_COLOR = "true"
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ module.exports = {
: [`default`].concat(useCoverage ? `jest-junit` : []),
testEnvironment: `jest-environment-jsdom-fourteen`,
moduleFileExtensions: [`js`, `jsx`, `ts`, `tsx`, `json`],
setupFiles: [`<rootDir>/.jestSetup.js`],
}
4 changes: 3 additions & 1 deletion packages/gatsby-recipes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@
"babel-preset-gatsby-package": "^0.3.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"rimraf": "^3.0.2"
"rimraf": "^3.0.2",
"strip-ansi": "^6.0.0",
"tmp-promise": "^2.0.2"
},
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-recipes#readme",
"keywords": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Object {
module.exports = {
/* Your site config here */
plugins: [],
}
};
",
"describe": "Install gatsby-plugin-foo in gatsby-config.js",
"diff": "- Original - 1
Expand All @@ -30,7 +30,7 @@ module.exports = {
/* Your site config here */
- plugins: [],
+ plugins: [\\"gatsby-plugin-foo\\"],
}
};
",
"id": "gatsby-plugin-foo",
"name": "gatsby-plugin-foo",
Expand All @@ -42,7 +42,7 @@ module.exports = {
module.exports = {
/* Your site config here */
plugins: [\\"gatsby-plugin-foo\\"],
}
};
",
}
`;
Expand All @@ -67,7 +67,7 @@ Object {
module.exports = {
/* Your site config here */
plugins: [\\"gatsby-plugin-foo\\"],
}
};
",
"describe": "Install gatsby-plugin-foo in gatsby-config.js",
"diff": "Compared values have no visual difference.",
Expand All @@ -81,7 +81,7 @@ module.exports = {
module.exports = {
/* Your site config here */
plugins: [\\"gatsby-plugin-foo\\"],
}
};
",
}
`;
Expand All @@ -96,7 +96,7 @@ Object {

exports[`gatsby-plugin resource e2e plugin resource test: GatsbyPlugin create plan 1`] = `
Object {
"currentState": "const redish = \`#c5484d\`
"currentState": "const redish = \`#c5484d\`;
module.exports = {
siteMetadata: {
title: \`Bricolage\`,
Expand Down Expand Up @@ -163,7 +163,7 @@ module.exports = {
\`gatsby-plugin-offline\`, // \`gatsby-plugin-preact\`,
\`gatsby-plugin-react-helmet\`,
],
}
};
",
"describe": "Install gatsby-plugin-foo in gatsby-config.js",
"diff": "- Original - 0
Expand All @@ -175,11 +175,11 @@ module.exports = {
\`gatsby-plugin-react-helmet\`,
+ \\"gatsby-plugin-foo\\",
],
}
};
",
"id": "gatsby-plugin-foo",
"name": "gatsby-plugin-foo",
"newState": "const redish = \`#c5484d\`
"newState": "const redish = \`#c5484d\`;
module.exports = {
siteMetadata: {
title: \`Bricolage\`,
Expand Down Expand Up @@ -247,7 +247,7 @@ module.exports = {
\`gatsby-plugin-react-helmet\`,
\\"gatsby-plugin-foo\\",
],
}
};
",
}
`;
Expand All @@ -264,7 +264,7 @@ Object {
exports[`gatsby-plugin resource e2e plugin resource test: GatsbyPlugin update plan 1`] = `
Object {
"currentState": "const redish = \`#c5484d\`
"currentState": "const redish = \`#c5484d\`;
module.exports = {
siteMetadata: {
title: \`Bricolage\`,
Expand Down Expand Up @@ -332,13 +332,13 @@ module.exports = {
\`gatsby-plugin-react-helmet\`,
\\"gatsby-plugin-foo\\",
],
}
};
",
"describe": "Install gatsby-plugin-foo in gatsby-config.js",
"diff": "Compared values have no visual difference.",
"id": "gatsby-plugin-foo",
"name": "gatsby-plugin-foo",
"newState": "const redish = \`#c5484d\`
"newState": "const redish = \`#c5484d\`;
module.exports = {
siteMetadata: {
title: \`Bricolage\`,
Expand Down Expand Up @@ -406,7 +406,7 @@ module.exports = {
\`gatsby-plugin-react-helmet\`,
\\"gatsby-plugin-foo\\",
],
}
};
",
}
`;
33 changes: 29 additions & 4 deletions packages/gatsby-recipes/src/providers/gatsby/plugin.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
const fs = require(`fs-extra`)
const path = require(`path`)
const tmp = require(`tmp-promise`)

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

const root = path.join(__dirname, `./fixtures/gatsby-starter-blog`)
const helloWorldRoot = path.join(
const STARTER_BLOG_FIXTURE = path.join(
__dirname,
`./fixtures/gatsby-starter-blog`
)
const HELLO_WORLD_FIXTURE = path.join(
__dirname,
`./fixtures/gatsby-starter-hello-world`
)
const name = `gatsby-plugin-foo`
const configPath = path.join(root, `gatsby-config.js`)

describe(`gatsby-plugin resource`, () => {
let tmpDir
let starterBlogRoot
let helloWorldRoot
let configPath
beforeAll(async () => {
tmpDir = await tmp.dir({
unsafeCleanup: true,
})
starterBlogRoot = path.join(tmpDir.path, `gatsby-starter-blog`)
helloWorldRoot = path.join(tmpDir.path, `gatsby-starter-hello-world`)
configPath = path.join(helloWorldRoot, `gatsby-config.js`)
await fs.ensureDir(starterBlogRoot)
await fs.copy(STARTER_BLOG_FIXTURE, starterBlogRoot)
await fs.ensureDir(helloWorldRoot)
await fs.copy(HELLO_WORLD_FIXTURE, helloWorldRoot)
})
afterAll(async () => {
if (tmpDir) {
await tmpDir.cleanup()
}
})

test(`e2e plugin resource test`, async () => {
await resourceTestHelper({
resourceModule: plugin,
resourceName: `GatsbyPlugin`,
context: { root },
context: { root: starterBlogRoot },
initialObject: { id: name, name },
partialUpdate: { id: name },
})
Expand Down
7 changes: 6 additions & 1 deletion packages/gatsby-recipes/src/providers/utils/get-diff.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const diff = require(`jest-diff`).default
const chalk = require(`chalk`)
const stripAnsi = require(`strip-ansi`)

module.exports = async (oldVal, newVal) => {
const options = {
Expand All @@ -12,7 +13,11 @@ module.exports = async (oldVal, newVal) => {
expand: false,
}

const diffText = diff(oldVal, newVal, options)
let diffText = diff(oldVal, newVal, options)

if (process.env.GATSBY_RECIPES_NO_COLOR) {
diffText = stripAnsi(diffText)
}

return diffText
}
19 changes: 14 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22685,11 +22685,6 @@ sudo-prompt@^8.2.0:
resolved "https://registry.yarnpkg.com/sudo-prompt/-/sudo-prompt-8.2.5.tgz#cc5ef3769a134bb94b24a631cc09628d4d53603e"
integrity sha512-rlBo3HU/1zAJUrkY6jNxDOC9eVYliG6nS4JA8u8KAshITd07tafMc/Br7xQwCSseXwJ2iCcHCE8SNWX3q8Z+kw==

sudo-prompt@^8.2.0:
version "8.2.5"
resolved "https://registry.yarnpkg.com/sudo-prompt/-/sudo-prompt-8.2.5.tgz#cc5ef3769a134bb94b24a631cc09628d4d53603e"
integrity sha512-rlBo3HU/1zAJUrkY6jNxDOC9eVYliG6nS4JA8u8KAshITd07tafMc/Br7xQwCSseXwJ2iCcHCE8SNWX3q8Z+kw==

supports-color@6.1.0, supports-color@^6.0.0, supports-color@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-6.1.0.tgz#0764abc69c63d5ac842dd4867e8d025e880df8f3"
Expand Down Expand Up @@ -23204,6 +23199,20 @@ title-case@^2.1.0:
no-case "^2.2.0"
upper-case "^1.0.3"

tmp-promise@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/tmp-promise/-/tmp-promise-2.0.2.tgz#ee605edb10f100954be5dd8b9dbe1bfd56194202"
integrity sha512-zl71nFWjPKW2KXs+73gEk8RmqvtAeXPxhWDkTUoa3MSMkjq3I+9OeknjF178MQoMYsdqL730hfzvNfEkePxq9Q==
dependencies:
tmp "0.1.0"

tmp@0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.1.0.tgz#ee434a4e22543082e294ba6201dcc6eafefa2877"
integrity sha512-J7Z2K08jbGcdA1kkQpJSqLF6T0tdQqpR2pnSUXsIchbPdTI9v3e85cLW0d6WDhwuAleOV71j2xWs8qMPfK7nKw==
dependencies:
rimraf "^2.6.3"

tmp@^0.0.33:
version "0.0.33"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"
Expand Down

0 comments on commit 612120f

Please sign in to comment.