Skip to content

Commit

Permalink
Mock reporter.panicOnBuild to simplify unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hexrcs committed Nov 27, 2018
1 parent c0f887c commit 5a9901b
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions packages/gatsby-remark-katex/src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
jest.mock(`gatsby-cli/lib/reporter`, () => {
return {
panicOnBuild: jest.fn(),
}
})

const Remark = require(`remark`)
const plugin = require(`../index`)
const reporter = require(`gatsby-cli/lib/reporter`)
Expand Down Expand Up @@ -36,39 +42,14 @@ describe(`remark katex plugin`, () => {
expect(markdownAST).toMatchSnapshot()
})

it(`doesn't crash when there's an error in development mode`, () => {
it(`panics on build if there's an error when parsing maths`, () => {
const equation = `$a^2 + b^2 = c^$`
let remark = new Remark()
const realProcess = process
const exitMock = jest.fn()
global.process = { ...realProcess, exit: exitMock }

for (let parserPlugins of plugin.setParserPlugins()) {
remark = remark.use(parserPlugins)
}
process.env.gatsby_executing_command = `develop`
const markdownAST = remark.parse(equation)
plugin({ markdownAST, reporter })

expect(exitMock).not.toHaveBeenCalledWith(1)
global.process = realProcess
})

it(`crashes when there's an error in build mode`, () => {
const equation = `$a^2 + b^2 = c^$`
let remark = new Remark()
const realProcess = process
const exitMock = jest.fn()
global.process = { ...realProcess, exit: exitMock }

for (let parserPlugins of plugin.setParserPlugins()) {
remark = remark.use(parserPlugins)
}
process.env.gatsby_executing_command = `build`
const markdownAST = remark.parse(equation)
plugin({ markdownAST, reporter })

expect(exitMock).toHaveBeenCalledWith(1)
global.process = realProcess
expect(reporter.panicOnBuild.mock.calls.length).toBe(1)
})
})

0 comments on commit 5a9901b

Please sign in to comment.