diff --git a/src/__node_tests__/pretty-dom.js b/src/__node_tests__/pretty-dom.js new file mode 100644 index 00000000..df9d16d3 --- /dev/null +++ b/src/__node_tests__/pretty-dom.js @@ -0,0 +1,107 @@ +import {JSDOM} from 'jsdom' +import {prettyDOM} from '../pretty-dom' +import {getUserCodeFrame} from '../get-user-code-frame' + +function render(html) { + const {window} = new JSDOM() + const container = window.document.createElement('div') + container.innerHTML = html + return {container} +} + +jest.mock('../get-user-code-frame') + +test('prettyDOM supports a COLORS environment variable', () => { + const {container} = render('
Hello World!
') + + // process.env.COLORS is a string, so make sure we test it as such + process.env.COLORS = 'false' + expect(prettyDOM(container)).toMatchInlineSnapshot(` +
+
+ Hello World! +
+
+ `) + + process.env.COLORS = 'true' + expect(prettyDOM(container)).toMatchInlineSnapshot(` + 
 + 
 + Hello World! + 
 + 
 + `) +}) + +test('prettyDOM handles a COLORS env variable of unexpected object type by colorizing for node', () => { + const {container} = render('
Hello World!
') + + const originalNodeVersion = process.versions.node + process.env.COLORS = '{}' + delete process.versions.node + expect(prettyDOM(container)).toMatchInlineSnapshot(` +
+
+ Hello World! +
+
+ `) + process.versions.node = '1.2.3' + expect(prettyDOM(container)).toMatchInlineSnapshot(` + 
 + 
 + Hello World! + 
 + 
 + `) + process.versions.node = originalNodeVersion +}) + +test('prettyDOM handles a COLORS env variable of undefined by colorizing for node', () => { + const {container} = render('
Hello World!
') + + const originalNodeVersion = process.versions.node + process.env.COLORS = undefined + delete process.versions.node + expect(prettyDOM(container)).toMatchInlineSnapshot(` +
+
+ Hello World! +
+
+ `) + process.versions.node = '1.2.3' + expect(prettyDOM(container)).toMatchInlineSnapshot(` + 
 + 
 + Hello World! + 
 + 
 + `) + process.versions.node = originalNodeVersion +}) + +test('prettyDOM handles a COLORS env variable of empty string by colorizing for node', () => { + const {container} = render('
Hello World!
') + + const originalNodeVersion = process.versions.node + process.env.COLORS = '' + delete process.versions.node + expect(prettyDOM(container)).toMatchInlineSnapshot(` +
+
+ Hello World! +
+
+ `) + process.versions.node = '1.2.3' + expect(prettyDOM(container)).toMatchInlineSnapshot(` + 
 + 
 + Hello World! + 
 + 
 + `) + process.versions.node = originalNodeVersion +}) diff --git a/src/__tests__/element-queries.js b/src/__tests__/element-queries.js index f3bc0ab0..a8e00011 100644 --- a/src/__tests__/element-queries.js +++ b/src/__tests__/element-queries.js @@ -1,6 +1,9 @@ +import jestSnapshotSerializerAnsi from 'jest-snapshot-serializer-ansi' import {configure, getConfig} from '../config' import {render, renderIntoDocument} from './helpers/test-utils' +expect.addSnapshotSerializer(jestSnapshotSerializerAnsi) + // set original config let originalConfig beforeAll(() => { diff --git a/src/__tests__/get-user-code-frame.js b/src/__tests__/get-user-code-frame.js index 8d2bd058..59a1123b 100644 --- a/src/__tests__/get-user-code-frame.js +++ b/src/__tests__/get-user-code-frame.js @@ -1,6 +1,9 @@ import fs from 'fs' +import jestSnapshotSerializerAnsi from 'jest-snapshot-serializer-ansi' import {getUserCodeFrame} from '../get-user-code-frame' +expect.addSnapshotSerializer(jestSnapshotSerializerAnsi) + jest.mock('fs', () => ({ // We setup the contents of a sample file readFileSync: jest.fn( diff --git a/src/__tests__/log-dom.js b/src/__tests__/log-dom.js index ea7fd5bf..d6a52e62 100644 --- a/src/__tests__/log-dom.js +++ b/src/__tests__/log-dom.js @@ -12,20 +12,20 @@ afterEach(() => { console.log.mockRestore() }) -test('logDOM logs prettyDOM to the console', () => { +test('logDOM logs highlighted prettyDOM to the console', () => { const {container} = render('
Hello World!
') logDOM(container) expect(console.log).toHaveBeenCalledTimes(1) expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(` -
-
- Hello World! -
-
+ 
 + 
 + Hello World! + 
 + 
 `) }) -test('logDOM logs prettyDOM with code frame to the console', () => { +test('logDOM logs highlighted prettyDOM with code frame to the console', () => { getUserCodeFrame.mockImplementationOnce( () => `"/home/john/projects/sample-error/error-example.js:7:14 5 | document.createTextNode('Hello World!') @@ -39,11 +39,11 @@ test('logDOM logs prettyDOM with code frame to the console', () => { logDOM(container) expect(console.log).toHaveBeenCalledTimes(1) expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(` -
-
- Hello World! -
-
+ 
 + 
 + Hello World! + 
 + 
 "/home/john/projects/sample-error/error-example.js:7:14 5 | document.createTextNode('Hello World!') diff --git a/src/__tests__/pretty-dom.js b/src/__tests__/pretty-dom.js index fea04efa..3f4b8551 100644 --- a/src/__tests__/pretty-dom.js +++ b/src/__tests__/pretty-dom.js @@ -1,14 +1,32 @@ -import {prettyDOM} from '../pretty-dom' +import {prettyDOM as prettyDOMImpl} from '../pretty-dom' import {render, renderIntoDocument} from './helpers/test-utils' -test('prettyDOM prints out the given DOM element tree', () => { +function prettyDOM(...args) { + let originalProcess + // this shouldn't be defined in this environment in the first place + if (typeof process !== 'undefined') { + originalProcess = process + // TODO: Delete to test browser environments + // delete globalThis.process + } else { + throw new Error('process is no longer defined. Remove this setup code.') + } + + try { + return prettyDOMImpl(...args) + } finally { + globalThis.process = originalProcess + } +} + +test('prettyDOM prints out the given DOM element tree highlighted', () => { const {container} = render('
Hello World!
') expect(prettyDOM(container)).toMatchInlineSnapshot(` -
-
- Hello World! -
-
+ 
 + 
 + Hello World! + 
 + 
 `) }) @@ -17,21 +35,21 @@ test('prettyDOM supports truncating the output length', () => { expect(prettyDOM(container, 5)).toMatch(/\.\.\./) expect(prettyDOM(container, 0)).toMatch('') expect(prettyDOM(container, Number.POSITIVE_INFINITY)).toMatchInlineSnapshot(` -
-
- Hello World! -
-
+ 
 + 
 + Hello World! + 
 + 
 `) }) test('prettyDOM defaults to document.body', () => { const defaultInlineSnapshot = ` - -
- Hello World! -
- +  + 
 + Hello World! + 
 +  ` renderIntoDocument('
Hello World!
') expect(prettyDOM()).toMatchInlineSnapshot(defaultInlineSnapshot) @@ -40,10 +58,10 @@ test('prettyDOM defaults to document.body', () => { test('prettyDOM supports receiving the document element', () => { expect(prettyDOM(document)).toMatchInlineSnapshot(` - - - - +  +  +  +  `) }) @@ -71,11 +89,11 @@ test('prettyDOM ignores script elements and comments nodes by default', () => { ) expect(prettyDOM(container)).toMatchInlineSnapshot(` - -

- Hello, Dave -

- +  + 

 + Hello, Dave + 

 +  `) }) @@ -87,73 +105,18 @@ test('prettyDOM can include all elements with a custom filter', () => { expect( prettyDOM(container, Number.POSITIVE_INFINITY, {filterNode: () => true}), ).toMatchInlineSnapshot(` - -