Skip to content

Commit

Permalink
suppress console.warn logs in tests
Browse files Browse the repository at this point in the history
This makes the test output a bit nicer to look at
  • Loading branch information
RobinMalfait authored and adamwathan committed Aug 28, 2020
1 parent ad7fec4 commit 62eaac4
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions __tests__/purgeUnusedStyles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import postcss from 'postcss'
import tailwind from '../src/index'
import defaultConfig from '../stubs/defaultConfig.stub.js'

function suppressConsoleLogs(cb, type = 'warn') {
return () => {
const spy = jest.spyOn(global.console, type).mockImplementation(jest.fn())

return new Promise((resolve, reject) => {
Promise.resolve(cb()).then(resolve, reject)
}).finally(() => spy.mockRestore())
}
}

const config = {
...defaultConfig,
theme: {
Expand Down Expand Up @@ -146,29 +156,32 @@ test('does not purge except in production', () => {
})
})

test('does not purge if the array is empty', () => {
const OLD_NODE_ENV = process.env.NODE_ENV
process.env.NODE_ENV = 'production'
const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`)
const input = fs.readFileSync(inputPath, 'utf8')

return postcss([
tailwind({
...defaultConfig,
purge: [],
}),
])
.process(input, { from: inputPath })
.then(result => {
process.env.NODE_ENV = OLD_NODE_ENV
const expected = fs.readFileSync(
path.resolve(`${__dirname}/fixtures/tailwind-output.css`),
'utf8'
)

expect(result.css).toBe(expected)
})
})
test(
'does not purge if the array is empty',
suppressConsoleLogs(() => {
const OLD_NODE_ENV = process.env.NODE_ENV
process.env.NODE_ENV = 'production'
const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`)
const input = fs.readFileSync(inputPath, 'utf8')

return postcss([
tailwind({
...defaultConfig,
purge: [],
}),
])
.process(input, { from: inputPath })
.then(result => {
process.env.NODE_ENV = OLD_NODE_ENV
const expected = fs.readFileSync(
path.resolve(`${__dirname}/fixtures/tailwind-output.css`),
'utf8'
)

expect(result.css).toBe(expected)
})
})
)

test('does not purge if explicitly disabled', () => {
const OLD_NODE_ENV = process.env.NODE_ENV
Expand Down

0 comments on commit 62eaac4

Please sign in to comment.