From 342a2662b097562cf38a7e6cd97bfa6077501cc0 Mon Sep 17 00:00:00 2001
From: kazk
Date: Mon, 29 Nov 2021 23:30:44 -0800
Subject: [PATCH] Add snapshot tests for method-doc
---
jest.config.js | 3 +-
package-lock.json | 94 +++++
package.json | 1 +
test/__snapshots__/method-doc.spec.js.snap | 461 +++++++++++++++++++++
test/method-doc.spec.js | 37 +-
5 files changed, 585 insertions(+), 11 deletions(-)
create mode 100644 test/__snapshots__/method-doc.spec.js.snap
diff --git a/jest.config.js b/jest.config.js
index 8ce572d..abda2ff 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -1,4 +1,5 @@
module.exports = {
// The test environment that will be used for testing
- testEnvironment: "jest-environment-jsdom",
+ testEnvironment: 'jest-environment-jsdom',
+ snapshotSerializers: ['jest-serializer-html'],
};
diff --git a/package-lock.json b/package-lock.json
index b5ef6d9..90c0702 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -15764,6 +15764,15 @@
"integrity": "sha512-ag6wfpBFyNXZ0p8pcuIDS//D8H062ZQJ3fzYxjpmeKjnz8W4pekL3AI8VohmyZmsWW2PWaHgjsmqR6L13101VQ==",
"dev": true
},
+ "diffable-html": {
+ "version": "4.1.0",
+ "resolved": "https://registry.npmjs.org/diffable-html/-/diffable-html-4.1.0.tgz",
+ "integrity": "sha512-++kyNek+YBLH8cLXS+iTj/Hiy2s5qkRJEJ8kgu/WHbFrVY2vz9xPFUT+fii2zGF0m1CaojDlQJjkfrCt7YWM1g==",
+ "dev": true,
+ "requires": {
+ "htmlparser2": "^3.9.2"
+ }
+ },
"diffie-hellman": {
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz",
@@ -15800,6 +15809,30 @@
}
}
},
+ "dom-serializer": {
+ "version": "0.2.2",
+ "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-0.2.2.tgz",
+ "integrity": "sha512-2/xPb3ORsQ42nHYiSunXkDjPLBaEj/xTwUO4B7XCZQTRk7EBtTOPaygh10YAAh2OI1Qrp6NWfpAhzswj0ydt9g==",
+ "dev": true,
+ "requires": {
+ "domelementtype": "^2.0.1",
+ "entities": "^2.0.0"
+ },
+ "dependencies": {
+ "domelementtype": {
+ "version": "2.2.0",
+ "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-2.2.0.tgz",
+ "integrity": "sha512-DtBMo82pv1dFtUmHyr48beiuq792Sxohr+8Hm9zoxklYPfa6n0Z3Byjj2IV7bmr2IyqClnqEQhfgHJJ5QF0R5A==",
+ "dev": true
+ }
+ }
+ },
+ "domelementtype": {
+ "version": "1.3.1",
+ "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.3.1.tgz",
+ "integrity": "sha512-BSKB+TSpMpFI/HOxCNr1O8aMOTZ8hT3pM3GQ0w/mWRmkhEDSFJkkyzz4XQsBV44BChwGkrDfMyjVD0eA2aFV3w==",
+ "dev": true
+ },
"domexception": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/domexception/-/domexception-1.0.1.tgz",
@@ -15826,6 +15859,16 @@
}
}
},
+ "domutils": {
+ "version": "1.7.0",
+ "resolved": "https://registry.npmjs.org/domutils/-/domutils-1.7.0.tgz",
+ "integrity": "sha512-Lgd2XcJ/NjEw+7tFvfKxOzCYKZsdct5lczQ2ZaQY8Djz7pfAD3Gbp8ySJWtreII/vDlMVmxwa6pHmdxIYgttDg==",
+ "dev": true,
+ "requires": {
+ "dom-serializer": "0",
+ "domelementtype": "1"
+ }
+ },
"dotenv-expand": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-5.1.0.tgz",
@@ -16855,6 +16898,48 @@
"integrity": "sha1-x43mW1Zjqll5id0rerSSANfk25g=",
"dev": true
},
+ "htmlparser2": {
+ "version": "3.10.1",
+ "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.10.1.tgz",
+ "integrity": "sha512-IgieNijUMbkDovyoKObU1DUhm1iwNYE/fuifEoEHfd1oZKZDaONBSkal7Y01shxsM49R4XaMdGez3WnF9UfiCQ==",
+ "dev": true,
+ "requires": {
+ "domelementtype": "^1.3.1",
+ "domhandler": "^2.3.0",
+ "domutils": "^1.5.1",
+ "entities": "^1.1.1",
+ "inherits": "^2.0.1",
+ "readable-stream": "^3.1.1"
+ },
+ "dependencies": {
+ "domhandler": {
+ "version": "2.4.2",
+ "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-2.4.2.tgz",
+ "integrity": "sha512-JiK04h0Ht5u/80fdLMCEmV4zkNh2BcoMFBmZ/91WtYZ8qVXSKjiw7fXMgFPnHcSZgOo3XdinHvmnDUeMf5R4wA==",
+ "dev": true,
+ "requires": {
+ "domelementtype": "1"
+ }
+ },
+ "entities": {
+ "version": "1.1.2",
+ "resolved": "https://registry.npmjs.org/entities/-/entities-1.1.2.tgz",
+ "integrity": "sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==",
+ "dev": true
+ },
+ "readable-stream": {
+ "version": "3.6.0",
+ "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz",
+ "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==",
+ "dev": true,
+ "requires": {
+ "inherits": "^2.0.3",
+ "string_decoder": "^1.1.1",
+ "util-deprecate": "^1.0.1"
+ }
+ }
+ }
+ },
"http-proxy": {
"version": "1.18.1",
"resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.1.tgz",
@@ -19258,6 +19343,15 @@
}
}
},
+ "jest-serializer-html": {
+ "version": "7.1.0",
+ "resolved": "https://registry.npmjs.org/jest-serializer-html/-/jest-serializer-html-7.1.0.tgz",
+ "integrity": "sha512-xYL2qC7kmoYHJo8MYqJkzrl/Fdlx+fat4U1AqYg+kafqwcKPiMkOcjWHPKhueuNEgr+uemhGc+jqXYiwCyRyLA==",
+ "dev": true,
+ "requires": {
+ "diffable-html": "^4.1.0"
+ }
+ },
"jest-snapshot": {
"version": "27.3.1",
"resolved": "https://registry.npmjs.org/jest-snapshot/-/jest-snapshot-27.3.1.tgz",
diff --git a/package.json b/package.json
index bfcd1d1..ae97cbc 100644
--- a/package.json
+++ b/package.json
@@ -37,6 +37,7 @@
"husky": "^7.0.4",
"jest": "^27.3.1",
"jest-environment-jsdom": "^27.3.1",
+ "jest-serializer-html": "^7.1.0",
"marked": "^4.0.10",
"parcel": "^2.0.0",
"prettier": "^2.4.1",
diff --git a/test/__snapshots__/method-doc.spec.js.snap b/test/__snapshots__/method-doc.spec.js.snap
new file mode 100644
index 0000000..228b16c
--- /dev/null
+++ b/test/__snapshots__/method-doc.spec.js.snap
@@ -0,0 +1,461 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`%method-doc should match snapshot 1`] = `
+
+ Documentation
+
+
+ This is some text
+
+
+
+
+
+ open_files
+
+
+ (
+
+
+ files
+
+
+ ,
+
+
+ ext
+
+
+ )
+
+
+ Loops through any files that are not opened and opens them
+
+
+
+ -
+ Parameters
+
+ -
+
+
+ files
+
+ :
+
+ Array<File>
+
+ - An array of file objects that may need to be opened
+
+
+ -
+
+
+ ext
+
+ :
+
+ String
+
+ - An optional extension that may be used as a filter to determine which files to open
+
+
+ -
+ Return Value
+
+ -
+
+
+ Array<File>
+
+ - A filtered array of files that were opened
+
+
+ -
+ Constraints
+
+ -
+
+ 0 <= files.length <= 10
+
+
+
+
+
+
+ Examples
+
+
+
+
+
+ |
+
+ files
+ |
+
+ ext
+ |
+
+ Return Value
+ |
+
+
+
+
+
+
+ Example A
+
+ |
+
+
+ [1,2,3]
+
+ |
+
+
+ "js"
+
+ |
+
+
+ [1,2]
+
+ |
+
+
+
+
+ Example
+
+ |
+
+
+ [1,2,3]
+
+ |
+
+
+ "rb"
+
+ |
+
+
+ [3]
+
+ |
+
+
+
+
+
+`;
+
+exports[`%method-doc should match snapshot with language: csharp 1`] = `
+
+ Documentation
+
+
+ This is some text
+
+
+
+
+
+ Challenge
+
+ .
+
+ OpenFiles
+
+
+ (
+
+
+ files
+
+
+ ,
+
+
+ ext
+
+
+ )
+
+
+ Loops through any files that are not opened and opens them
+
+
+
+ -
+ Parameters
+
+ -
+
+
+ files
+
+ :
+
+ File[]
+
+ - An array of file objects that may need to be opened
+
+
+ -
+
+
+ ext
+
+ :
+
+ string
+
+ - An optional extension that may be used as a filter to determine which files to open
+
+
+ -
+ Return Value
+
+ -
+
+
+ File[]
+
+ - A filtered array of files that were opened
+
+
+ -
+ Constraints
+
+ -
+
+ 0 <= files.length <= 10
+
+
+
+
+
+
+ Examples
+
+
+
+
+
+ |
+
+ files
+ |
+
+ ext
+ |
+
+ Return Value
+ |
+
+
+
+
+
+
+ Example A
+
+ |
+
+
+ [1,2,3]
+
+ |
+
+
+ "js"
+
+ |
+
+
+ [1,2]
+
+ |
+
+
+
+
+ Example
+
+ |
+
+
+ [1,2,3]
+
+ |
+
+
+ "rb"
+
+ |
+
+
+ [3]
+
+ |
+
+
+
+
+
+`;
+
+exports[`%method-doc should match snapshot with language: javascript 1`] = `
+
+ Documentation
+
+
+ This is some text
+
+
+
+
+
+ openFiles
+
+
+ (
+
+
+ files
+
+
+ ,
+
+
+ ext
+
+
+ )
+
+
+ Loops through any files that are not opened and opens them
+
+
+
+ -
+ Parameters
+
+ -
+
+
+ files
+
+ :
+
+ Array<File>
+
+ - An array of file objects that may need to be opened
+
+
+ -
+
+
+ ext
+
+ :
+
+ String
+
+ - An optional extension that may be used as a filter to determine which files to open
+
+
+ -
+ Return Value
+
+ -
+
+
+ Array<File>
+
+ - A filtered array of files that were opened
+
+
+ -
+ Constraints
+
+ -
+
+ 0 <= files.length <= 10
+
+
+
+
+
+
+ Examples
+
+
+
+
+
+ |
+
+ files
+ |
+
+ ext
+ |
+
+ Return Value
+ |
+
+
+
+
+
+
+ Example A
+
+ |
+
+
+ [1,2,3]
+
+ |
+
+
+ "js"
+
+ |
+
+
+ [1,2]
+
+ |
+
+
+
+
+ Example
+
+ |
+
+
+ [1,2,3]
+
+ |
+
+
+ "rb"
+
+ |
+
+
+ [3]
+
+ |
+
+
+
+
+
+`;
diff --git a/test/method-doc.spec.js b/test/method-doc.spec.js
index 4272953..1526d35 100644
--- a/test/method-doc.spec.js
+++ b/test/method-doc.spec.js
@@ -1,24 +1,41 @@
import { process } from '../src/index';
import { marked } from 'marked';
-import { expect } from 'chai';
import { fixture } from './test-utils';
describe('%method-doc', () => {
it('should render json as formatted markdown', () => {
const example = process(marked, fixture('method-doc')).html();
- expect(example).to.include('>Examples');
- expect(example).to.include('files | ');
+ expect(example).toEqual(expect.stringContaining('>Examples'));
+ expect(example).toEqual(expect.stringContaining('files | '));
// Used to have empty `` after global prefix was removed
- // expect(example).to.include('');
- expect(example).to.include('- A filtered array of files that were opened
');
- expect(example).to.include('[1,2,3] | ');
- expect(example).to.include('"js" | ');
- expect(example).to.include('');
+ // expect(example).toEqual(expect.stringContaining(''));
+ expect(example).toEqual(
+ expect.stringContaining('- A filtered array of files that were opened')
+ );
+ expect(example).toEqual(expect.stringContaining('[1,2,3] | '));
+ expect(example).toEqual(expect.stringContaining('"js" | '));
+ expect(example).toEqual(expect.stringContaining(''));
});
it('should render json as formatted markdown', () => {
const example = process(marked, fixture('method-doc-invalid')).html();
- expect(example).to.not.include('>Examples');
- expect(example).to.include('openFiles');
+ expect(example).not.toEqual(expect.stringContaining('>Examples'));
+ expect(example).toEqual(expect.stringContaining('openFiles'));
+ });
+
+ it('should match snapshot', () => {
+ // Uses https://github.com/algolia/jest-serializer-html to make the snapshot diff readable
+ const example = process(marked, fixture('method-doc')).html();
+ expect(example).toMatchSnapshot();
+ });
+
+ it('should match snapshot with language: javascript', () => {
+ const example = process(marked, fixture('method-doc'), { language: 'javascript' }).html();
+ expect(example).toMatchSnapshot();
+ });
+
+ it('should match snapshot with language: csharp', () => {
+ const example = process(marked, fixture('method-doc'), { language: 'csharp' }).html();
+ expect(example).toMatchSnapshot();
});
});