Skip to content

Commit

Permalink
Gatsby cli develop tests (#22697)
Browse files Browse the repository at this point in the history
* chore(gatsby-cli): Add integration tests for `gatsby develop`

* add skip tests to add later
  • Loading branch information
blainekasten authored Mar 31, 2020
1 parent 12c61a6 commit ff51768
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 1 deletion.
76 changes: 76 additions & 0 deletions integration-tests/gatsby-cli/__tests__/develop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import spawn from "cross-spawn"
import { GatsbyCLI, removeFolder } from "../test-helpers"
import strip from "strip-ansi"

const timeout = seconds =>
new Promise(resolve => {
setTimeout(resolve, seconds * 1000)
})

const MAX_TIMEOUT = 2147483647
jest.setTimeout(MAX_TIMEOUT)

describe(`gatsby develop`, () => {
const cwd = `gatsby-sites/gatsby-develop`

beforeAll(() => removeFolder(`${cwd}/.cache`))
beforeAll(() => removeFolder(`${cwd}/public`))
afterAll(() => removeFolder(`${cwd}/.cache`))
afterAll(() => removeFolder(`${cwd}/public`))

it(`starts a gatsby site on port 8000`, async () => {
// 1. Start the `gatsby develop` command
const [childProcess, getLogs] = GatsbyCLI.from(cwd).invokeAsync(`develop`)

// 2. Wait for the build process to finish
await timeout(10)

// 3. Get the process identifier for what is running on port 8000
const res = spawn.sync("lsof", ["-i", ":8000", "-t"])
const portPID = Number(/\d+/.exec(res.output?.toString())[0])

// 4. kill the `gatsby develop` command so we can get logs
spawn.sync("kill", [childProcess.pid])

// 5. Make sure the process we started was the one on gatsby port
expect(portPID).toEqual(childProcess.pid)

// 6. Make sure logs for the user contain expected results
const logs = getLogs()
logs.should.contain(`success open and validate gatsby-configs`)
logs.should.contain(`success load plugins`)
logs.should.contain(`success onPreInit`)
logs.should.contain(`success initialize cache`)
logs.should.contain(`success copy gatsby files`)
logs.should.contain(`success onPreBootstrap`)
logs.should.contain(`success createSchemaCustomization`)
logs.should.contain(`success source and transform nodes`)
logs.should.contain(`success building schema`)
logs.should.contain(`success createPages`)
logs.should.contain(`success createPagesStatefully`)
logs.should.contain(`success onPreExtractQueries`)
logs.should.contain(`success update schema`)
logs.should.contain(`success extract queries from components`)
logs.should.contain(`success write out requires`)
logs.should.contain(`success write out redirect data`)
logs.should.contain(`success onPostBootstrap`)
logs.should.contain(`info bootstrap finished`)
logs.should.contain(
`You can now view gatsby-starter-default in the browser.`
)
logs.should.contain(`http://localhost:8000/`)
logs.should.contain(
`View GraphiQL, an in-browser IDE, to explore your site's data and schema`
)
logs.should.contain(`http://localhost:8000/___graphql`)
logs.should.contain(`Note that the development build is not optimized.`)
logs.should.contain(`To create a production build, use gatsby build`)
})

it.skip(`starts a gatsby site on port 9000 with -p 9000`, () => {})
it.skip(`starts a gatsby site at a diffent host with -h`, () => {})
it.skip(`starts a gatsby site with ssl using -S`, () => {})
it.skip(`starts a gatsby site with cert file using -c`, () => {})
it.skip(`starts a gatsby site with key file using -k`, () => {})
it.skip(`starts a gatsby site with -open-tracing-config-file`, () => {})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# dotenv environment variable files
.env*

# gatsby files
.cache/
public

# Mac files
.DS_Store

# Yarn
yarn-error.log
.pnp/
.pnp.js
# Yarn Integrity file
.yarn-integrity
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "gatsby-starter-default",
"private": true,
"description": "A simple starter to get up and developing quickly with Gatsby",
"version": "0.1.0",
"author": "Kyle Mathews <mathews.kyle@gmail.com>",
"dependencies": {
"gatsby": "^2.19.45",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-helmet": "^5.2.1"
},
"devDependencies": {
"prettier": "^1.19.1"
},
"keywords": [
"gatsby"
],
"license": "MIT",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
"start": "npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-default"
},
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import React from "react"
export default () => <div>Hi</div>
19 changes: 18 additions & 1 deletion integration-tests/gatsby-cli/test-helpers/invoke-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,26 @@ export const GatsbyCLI = {
return [
results.status,
createLogsMatcher(results.output.toString().split("\n")),
results.output.toString().split("\n"),
]
},

invokeAsync: (...args) => {
const res = spawn(
join(__dirname, `../../../packages/gatsby-cli/lib/index.js`),
args,
{
cwd: join(__dirname, `../`, `./${relativeCwd}`),
}
)

const logs = []

res.stdout.on("data", data => {
logs.push(data.toString())
})

return [res, () => createLogsMatcher(logs)]
},
}
},
}

0 comments on commit ff51768

Please sign in to comment.