Skip to content

Commit

Permalink
Use console-testing-library to get consistent snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Dec 11, 2019
1 parent afc11d9 commit 9484114
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 45 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@types/jest": "^24.0.11",
"@types/node": "^10.14.4",
"@types/redux-immutable-state-invariant": "^2.1.1",
"console-testing-library": "^0.2.2",
"eslint-config-react-app": "^5.0.1",
"prettier": "^1.18.2",
"react": "^16.8.6",
Expand Down
61 changes: 16 additions & 45 deletions src/serializableStateInvariantMiddleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Reducer } from 'redux'
import { Console } from 'console'
import { Writable } from 'stream'
import { getLog } from 'console-testing-library'
import { configureStore } from './configureStore'

import {
Expand Down Expand Up @@ -81,35 +80,6 @@ describe('findNonSerializableValue', () => {
})

describe('serializableStateInvariantMiddleware', () => {
let log = ''
const originalConsole = window.console

beforeEach(() => {
log = ''

const writable = new Writable({
write(chunk, encoding, callback) {
log += chunk
callback()
}
})

const mockConsole = new Console({
stdout: writable,
stderr: writable
})

Object.defineProperty(window, 'console', {
value: mockConsole
})
})

afterEach(() => {
Object.defineProperty(window, 'console', {
value: originalConsole
})
})

it('Should log an error when a non-serializable action is dispatched', () => {
const reducer: Reducer = (state = 0, _action) => state + 1

Expand All @@ -125,11 +95,12 @@ describe('serializableStateInvariantMiddleware', () => {

store.dispatch(dispatchedAction)

expect(log).toMatchInlineSnapshot(`
expect(getLog().log).toMatchInlineSnapshot(`
"A non-serializable value was detected in an action, in the path: \`type\`. Value: Symbol(SOME_CONSTANT)
Take a look at the logic that dispatched this action: { type: Symbol(SOME_CONSTANT) }
(See https://redux.js.org/faq/actions#why-should-type-be-a-string-or-at-least-serializable-why-should-my-action-types-be-constants)
"
Take a look at the logic that dispatched this action: Object {
\\"type\\": Symbol(SOME_CONSTANT),
}
(See https://redux.js.org/faq/actions#why-should-type-be-a-string-or-at-least-serializable-why-should-my-action-types-be-constants)"
`)
})

Expand Down Expand Up @@ -165,11 +136,10 @@ describe('serializableStateInvariantMiddleware', () => {

store.dispatch({ type: ACTION_TYPE })

expect(log).toMatchInlineSnapshot(`
expect(getLog().log).toMatchInlineSnapshot(`
"A non-serializable value was detected in the state, in the path: \`testSlice.a\`. Value: Map {}
Take a look at the reducer(s) handling this action type: TEST_ACTION.
(See https://redux.js.org/faq/organizing-state#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state)
"
(See https://redux.js.org/faq/organizing-state#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state)"
`)
})

Expand Down Expand Up @@ -226,9 +196,11 @@ describe('serializableStateInvariantMiddleware', () => {
store.dispatch({ type: ACTION_TYPE })

// since default options are used, the `entries` function in `serializableObject` will cause the error
expect(log).toMatch(
/^A non-serializable value was detected in the state, in the path: \`testSlice.a.entries\`. Value:/
)
expect(getLog().log).toMatchInlineSnapshot(`
"A non-serializable value was detected in the state, in the path: \`testSlice.a.entries\`. Value: [Function entries]
Take a look at the reducer(s) handling this action type: TEST_ACTION.
(See https://redux.js.org/faq/organizing-state#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state)"
`)
})

it('Should use consumer supplied isSerializable and getEntries options to tolerate certain structures', () => {
Expand Down Expand Up @@ -269,11 +241,10 @@ describe('serializableStateInvariantMiddleware', () => {
store.dispatch({ type: ACTION_TYPE })

// error reported is from a nested class instance, rather than the `entries` function `serializableObject`
expect(log).toMatchInlineSnapshot(`
expect(getLog().log).toMatchInlineSnapshot(`
"A non-serializable value was detected in the state, in the path: \`testSlice.a.third.bad-map-instance\`. Value: Map {}
Take a look at the reducer(s) handling this action type: TEST_ACTION.
(See https://redux.js.org/faq/organizing-state#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state)
"
(See https://redux.js.org/faq/organizing-state#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state)"
`)
})
})
Expand Down Expand Up @@ -316,7 +287,7 @@ describe('serializableStateInvariantMiddleware', () => {

// Supplied 'isSerializable' considers all values serializable, hence
// no error logging is expected:
expect(log).toBe('')
expect(getLog().log).toBe('')
})

it('should not check serializability for ignored action types', () => {
Expand Down

0 comments on commit 9484114

Please sign in to comment.