Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import '../extend-expect'
import {plugins} from 'pretty-format'
import {render} from './helpers/test-utils'
expect.addSnapshotSerializer(plugins.ConvertAnsi)
test('.toBeInTheDOM', () => {
const {queryByTestId} = render(`
<span data-testid="count-container">
<span data-testid="count-value"></span>
<svg data-testid="svg-element"></svg>
</span>`)
const containerElement = queryByTestId('count-container')
const valueElement = queryByTestId('count-value')
const nonExistantElement = queryByTestId('not-exists')
const svgElement = queryByTestId('svg-element')
const fakeElement = {thisIsNot: 'an html element'}
// Testing toBeInTheDOM without container
expect(valueElement).toBeInTheDOM()
// - can minify output
// - handles circular references
// - can serialize DOM
const serialize = function(param) {
return prettyFormat(param, PRETTY_FORMAT_OPTS)
}
const PRETTY_FORMAT_OPTS = {
min: true,
maxDepth: 2,
plugins: [
plugins.DOMElement,
plugins.DOMCollection,
plugins.ReactElement,
plugins.Immutable,
plugins.ConvertAnsi,
],
}
// Make names short by truncating them
const truncateName = function(name) {
if (name.length <= MAX_NAME_LENGTH) {
return name
}
const start = name.slice(0, TRUNCATE_START_LENGTH)
const end = name.slice(name.length - TRUNCATE_END_LENGTH)
return `${start}...${end}`
}
const MAX_NAME_LENGTH = 120
const TRUNCATE_START_LENGTH = Math.ceil((MAX_NAME_LENGTH - 3) / 2)
// - can serialize DOM
const serialize = function(value) {
const title = prettyFormat(value, PRETTY_FORMAT_OPTS)
const titleA = ESCAPE_SEQUENCES.reduce(escapeSequence, title)
return titleA
}
const PRETTY_FORMAT_OPTS = {
min: true,
maxDepth: 2,
plugins: [
plugins.DOMElement,
plugins.DOMCollection,
plugins.ReactElement,
plugins.Immutable,
plugins.ConvertAnsi,
],
}
// Escape newline characters to ensure title is on a single line
const escapeSequence = function(title, [regExp, replacement]) {
return title.replace(regExp, replacement)
}
const ESCAPE_SEQUENCES = [
[/\n/gu, '\\n'],
[/\r/gu, '\\r'],
[/\f/gu, '\\f'],
[/\v/gu, '\\v'],
]
// Make titles short by truncating them in the middle
// - can minify output (including maxDepth)
// - handles circular references
// - can serialize DOM
const serialize = function(param) {
return prettyFormat(param, PRETTY_FORMAT_OPTS)
}
const PRETTY_FORMAT_OPTS = {
min: true,
maxDepth: 2,
plugins: [
plugins.DOMElement,
plugins.DOMCollection,
plugins.ReactElement,
plugins.Immutable,
plugins.ConvertAnsi,
],
}
// Make titles short by truncating them in the middle
const truncateTitle = function(title) {
if (title.length <= MAX_TITLE_SIZE) {
return title
}
const start = title.slice(0, TRUNCATE_START_LENGTH)
const end = title.slice(title.length - TRUNCATE_END_LENGTH)
return `${start}${ELLIPSIS}${end}`
}
const MAX_TITLE_SIZE = 40
const ELLIPSIS = '...'
import {plugins} from 'pretty-format'
import '../src/extend-expect'
expect.addSnapshotSerializer(plugins.ConvertAnsi)
import { plugins } from 'pretty-format';
import './src/extend-expect';
expect.addSnapshotSerializer(plugins.ConvertAnsi);