From 48099c15df61a1bfabb215cae666d5d1c7486710 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 13 Jun 2020 18:54:47 -0400 Subject: [PATCH 01/11] test: migrate from `mocha` to `jest` Update package.json devDependencies: ```diff +jest -mocha -nyc ``` Update package.json scripts and remove `test:coverage:report` Run `jest-codemods` (https://jestjs.io/docs/en/migration-guide): ```sh jscodeshift -t jest-codemods/dist/transformers/mocha.js . --ignore-pattern node_modules --parser babel ``` Rename files under `test` directory Update eslint, husky, travis, and tsconfig --- .eslintrc.json | 2 +- .huskyrc.json | 2 +- .travis.yml | 4 ++-- package.json | 8 +++----- ...attributes-to-props.js => attributes-to-props.test.js} | 4 ++-- test/{dom-to-react.js => dom-to-react.test.js} | 4 ++-- test/{html-to-react.js => html-to-react.test.js} | 0 test/types/{index.test.tsx => index.tsx} | 0 .../types/lib/{dom-to-react.test.tsx => dom-to-react.tsx} | 0 test/{utilities.js => utilities.test.js} | 0 tsconfig.json | 4 ++-- 11 files changed, 13 insertions(+), 15 deletions(-) rename test/{attributes-to-props.js => attributes-to-props.test.js} (99%) rename test/{dom-to-react.js => dom-to-react.test.js} (99%) rename test/{html-to-react.js => html-to-react.test.js} (100%) rename test/types/{index.test.tsx => index.tsx} (100%) rename test/types/lib/{dom-to-react.test.tsx => dom-to-react.tsx} (100%) rename test/{utilities.js => utilities.test.js} (100%) diff --git a/.eslintrc.json b/.eslintrc.json index f0b7e80f..29bad671 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -2,7 +2,7 @@ "env": { "browser": true, "commonjs": true, - "mocha": true, + "jest": true, "node": true }, "extends": "eslint:recommended", diff --git a/.huskyrc.json b/.huskyrc.json index 6731aadb..7337f72f 100644 --- a/.huskyrc.json +++ b/.huskyrc.json @@ -1,6 +1,6 @@ { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS", - "pre-commit": "npm run lint:dts && npm run test:coverage && lint-staged" + "pre-commit": "npm run lint:dts && npm test && lint-staged" } } diff --git a/.travis.yml b/.travis.yml index 990b41cb..6292d095 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,11 +8,11 @@ script: - npx commitlint --from=HEAD~1 - npm run lint - npm run lint:dts - - npm run test:coverage + - npm test - npm run build - npm run benchmark after_success: - - npx nyc report --reporter=text-lcov | npx coveralls + - cat coverage/lcov.info | npx coveralls cache: directories: - node_modules diff --git a/package.json b/package.json index d754bfd4..7c156995 100644 --- a/package.json +++ b/package.json @@ -13,9 +13,8 @@ "lint:fix": "npm run lint -- --fix", "prepublishOnly": "npm run lint && npm run lint:dts && npm test && npm run clean && npm run build", "release": "standard-version --no-verify", - "test": "mocha", - "test:coverage": "nyc npm test", - "test:coverage:report": "nyc report --reporter=html" + "test": "jest --coverage", + "test:watch": "npm test -- --watch" }, "repository": { "type": "git", @@ -49,9 +48,8 @@ "eslint": "^7.1.0", "eslint-plugin-prettier": "^3.1.3", "husky": "^4.2.5", + "jest": "^26.0.1", "lint-staged": "^10.2.7", - "mocha": "^7.2.0", - "nyc": "^15.1.0", "preact": "^10.4.4", "prettier": "^2.0.5", "react": "^16", diff --git a/test/attributes-to-props.js b/test/attributes-to-props.test.js similarity index 99% rename from test/attributes-to-props.js rename to test/attributes-to-props.test.js index f4898e68..df65e9bd 100644 --- a/test/attributes-to-props.js +++ b/test/attributes-to-props.test.js @@ -259,11 +259,11 @@ describe('attributes to props', () => { describe('when utilties.PRESERVE_CUSTOM_ATTRIBUTES=false', () => { const { PRESERVE_CUSTOM_ATTRIBUTES } = utilities; - before(() => { + beforeAll(() => { utilities.PRESERVE_CUSTOM_ATTRIBUTES = false; }); - after(() => { + afterAll(() => { utilities.PRESERVE_CUSTOM_ATTRIBUTES = PRESERVE_CUSTOM_ATTRIBUTES; }); diff --git a/test/dom-to-react.js b/test/dom-to-react.test.js similarity index 99% rename from test/dom-to-react.js rename to test/dom-to-react.test.js index fdcefdf0..36595fae 100644 --- a/test/dom-to-react.js +++ b/test/dom-to-react.test.js @@ -210,11 +210,11 @@ describe('DOM to React', () => { describe('when React <16', () => { const { PRESERVE_CUSTOM_ATTRIBUTES } = utilities; - before(() => { + beforeAll(() => { utilities.PRESERVE_CUSTOM_ATTRIBUTES = false; }); - after(() => { + afterAll(() => { utilities.PRESERVE_CUSTOM_ATTRIBUTES = PRESERVE_CUSTOM_ATTRIBUTES; }); diff --git a/test/html-to-react.js b/test/html-to-react.test.js similarity index 100% rename from test/html-to-react.js rename to test/html-to-react.test.js diff --git a/test/types/index.test.tsx b/test/types/index.tsx similarity index 100% rename from test/types/index.test.tsx rename to test/types/index.tsx diff --git a/test/types/lib/dom-to-react.test.tsx b/test/types/lib/dom-to-react.tsx similarity index 100% rename from test/types/lib/dom-to-react.test.tsx rename to test/types/lib/dom-to-react.tsx diff --git a/test/utilities.js b/test/utilities.test.js similarity index 100% rename from test/utilities.js rename to test/utilities.test.js diff --git a/tsconfig.json b/tsconfig.json index 0ae4f4e0..37e9f500 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,7 +19,7 @@ "files": [ "index.d.ts", "lib/dom-to-react.d.ts", - "test/types/index.test.tsx", - "test/types/lib/dom-to-react.test.tsx" + "test/types/index.tsx", + "test/types/lib/dom-to-react.tsx" ] } From 5077d6991f20aee021687f2054042a133b631f84 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 13 Jun 2020 19:28:43 -0400 Subject: [PATCH 02/11] test(dom-to-react): refactor react tests to snapshot tests --- test/__snapshots__/dom-to-react.test.js.snap | 84 +++++++++++++ test/dom-to-react.test.js | 117 ++++++------------- 2 files changed, 118 insertions(+), 83 deletions(-) create mode 100644 test/__snapshots__/dom-to-react.test.js.snap diff --git a/test/__snapshots__/dom-to-react.test.js.snap b/test/__snapshots__/dom-to-react.test.js.snap new file mode 100644 index 00000000..c205788e --- /dev/null +++ b/test/__snapshots__/dom-to-react.test.js.snap @@ -0,0 +1,84 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`DOM to React converts ', + script: '', + style: '', + img: 'Image', + void: '

', + comment: '', + doctype: '', + customElement: + '' +}; + +/** + * SVG. + */ +module.exports.svg = { + simple: 'Inner', + complex: + 'AYour browser does not support inline SVG.' +}; diff --git a/test/helpers/data.json b/test/helpers/data.json deleted file mode 100644 index f49ecdd6..00000000 --- a/test/helpers/data.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "html": { - "single": "

foo

", - "multiple": "

foo

bar

", - "nested": "

foo bar

", - "attributes": "
", - "complex": "Title
Header

Heading


Paragraph

Some text.
", - "textarea": "", - "script": "", - "style": "", - "img": "\"Image\"/", - "void": "

", - "comment": "", - "doctype": "", - "customElement": "" - }, - "svg": { - "simple": "Inner", - "complex": "AYour browser does not support inline SVG." - } -} From 9cb48edad048548375bae5abdf01b3b6f6e35d0a Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 14 Jun 2020 15:33:33 -0400 Subject: [PATCH 08/11] test(helpers): refactor `render` method This brings test coverage back to 100% --- test/helpers/index.js | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/test/helpers/index.js b/test/helpers/index.js index bed6568d..dd324233 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -1,6 +1,6 @@ -const { isValidElement } = require('react'); const { renderToStaticMarkup } = require('react-dom/server'); -const data = require('./data'); + +module.exports.data = require('./data'); /** * Renders a React element to static HTML markup. @@ -8,14 +8,4 @@ const data = require('./data'); * @param {ReactElement} reactElement - The React element. * @return {String} - The static HTML markup. */ -const render = reactElement => { - if (!isValidElement(reactElement)) { - throw new Error(reactElement, 'is not a valid React element.'); - } - return renderToStaticMarkup(reactElement); -}; - -module.exports = { - render, - data -}; +module.exports.render = reactElement => renderToStaticMarkup(reactElement); From 6bf35266ebaba3f54ef058947df368b7d21be8e2 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 14 Jun 2020 17:10:48 -0400 Subject: [PATCH 09/11] test(html-to-react): refactor `forEach` to `it.each` And add more test cases --- test/html-to-react.test.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/html-to-react.test.js b/test/html-to-react.test.js index 7bda8e69..7defece0 100644 --- a/test/html-to-react.test.js +++ b/test/html-to-react.test.js @@ -18,13 +18,14 @@ describe('HTML to React', () => { }); describe('parser', () => { - [undefined, null, {}, [], 42].forEach(value => { - it(`throws an error if first argument is ${value}`, () => { + it.each([undefined, null, {}, [], 0, 1, () => {}, new Date()])( + 'throws an error if first argument is %p', + input => { expect(() => { - parse(value); + parse(input); }).toThrow(TypeError); - }); - }); + } + ); it('converts empty string to empty array', () => { expect(parse('')).toEqual([]); From 4c64bb3c13acfcda9d161448beb9a06e14f016bc Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 14 Jun 2020 17:12:34 -0400 Subject: [PATCH 10/11] test(utilities): refactor `forEach` to `it.each` and add more cases --- test/utilities.test.js | 61 +++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/test/utilities.test.js b/test/utilities.test.js index f3883c9d..b8b60923 100644 --- a/test/utilities.test.js +++ b/test/utilities.test.js @@ -8,46 +8,47 @@ const { describe('utilities', () => { describe('camelCase', () => { - [undefined, null, 1337, {}, []].forEach(value => { - it(`throws an error if first argument is ${value}`, () => { + it.each([undefined, null, {}, [], 0, 1, () => {}, new Date()])( + 'throws an error if first argument is %p', + input => { expect(() => { - camelCase(value); + camelCase(input); }).toThrow(TypeError); - }); - }); + } + ); - it('does not modify string if it does not need to be camelCased', () => { - [ - ['', ''], - ['foo', 'foo'], - ['fooBar', 'fooBar'], - ['--fooBar', '--fooBar'], - ['--foo-bar', '--foo-bar'], - ['--foo-100', '--foo-100'] - ].forEach(testCase => { - expect(camelCase(testCase[0])).toBe(testCase[1]); - }); - }); + it.each([ + ['', ''], + ['foo', 'foo'], + ['fooBar', 'fooBar'], + ['--fooBar', '--fooBar'], + ['--foo-bar', '--foo-bar'], + ['--foo-100', '--foo-100'] + ])( + 'does not modify string if it does not need to be camelCased', + (input, expected) => { + expect(camelCase(input)).toBe(expected); + } + ); - it('camelCases a string', () => { - [ - ['foo-bar', 'fooBar'], - ['foo-bar-baz', 'fooBarBaz'], - ['CAMEL-CASE', 'camelCase'] - ].forEach(testCase => { - expect(camelCase(testCase[0])).toBe(testCase[1]); - }); + it.each([ + ['foo-bar', 'fooBar'], + ['foo-bar-baz', 'fooBarBaz'], + ['CAMEL-CASE', 'camelCase'] + ])('camelCases a string', (input, expected) => { + expect(camelCase(input)).toBe(expected); }); }); describe('invertObject', () => { - [undefined, null, 'foo', 1337].forEach(value => { - it(`throws an error if the first argument is ${value}`, () => { + it.each([undefined, null, 'string', 0, 1, () => {}])( + `throws an error if the first argument is %p`, + input => { expect(() => { - invertObject(value); + invertObject(input); }).toThrow(TypeError); - }); - }); + } + ); it('swaps key with value', () => { expect( From e7c507919504b84553dd42da38a2cee2f22a8951 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 14 Jun 2020 17:14:08 -0400 Subject: [PATCH 11/11] chore(package): add script `test:ci`; update husky, travis, publish `npm run test:ci` passes option `--ci` to `npm test`, which fails the test if a new snapshot is encountered. --- .huskyrc.json | 2 +- .travis.yml | 2 +- package.json | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.huskyrc.json b/.huskyrc.json index 7337f72f..5ead76e6 100644 --- a/.huskyrc.json +++ b/.huskyrc.json @@ -1,6 +1,6 @@ { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS", - "pre-commit": "npm run lint:dts && npm test && lint-staged" + "pre-commit": "npm run lint:dts && npm run test:ci && lint-staged" } } diff --git a/.travis.yml b/.travis.yml index 6292d095..81dfe10a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ script: - npx commitlint --from=HEAD~1 - npm run lint - npm run lint:dts - - npm test + - npm run test:ci - npm run build - npm run benchmark after_success: diff --git a/package.json b/package.json index 7c156995..f7e679a7 100644 --- a/package.json +++ b/package.json @@ -11,9 +11,10 @@ "lint": "eslint --ignore-path .gitignore --ignore-pattern /examples/ .", "lint:dts": "dtslint .", "lint:fix": "npm run lint -- --fix", - "prepublishOnly": "npm run lint && npm run lint:dts && npm test && npm run clean && npm run build", + "prepublishOnly": "npm run lint && npm run lint:dts && npm run test:ci && npm run clean && npm run build", "release": "standard-version --no-verify", "test": "jest --coverage", + "test:ci": "npm test -- --ci", "test:watch": "npm test -- --watch" }, "repository": {