From 8980511a7acfd0324248988ade53d3859187b966 Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Mon, 20 Nov 2023 05:46:54 +0200 Subject: [PATCH] Rewrite tests to es6 modules --- .eslintrc.yml | 8 ++++-- package.json | 4 +-- test/.eslintrc.yml | 3 +-- ...k-responder.js => babelmark-responder.mjs} | 14 ++++++---- test/{commonmark.js => commonmark.mjs} | 17 ++++++------ test/markdown-it.js | 19 -------------- test/markdown-it.mjs | 15 +++++++++++ test/{misc.js => misc.mjs} | 10 +++---- test/{pathological.js => pathological.mjs} | 26 ++++++++++--------- test/{ruler.js => ruler.mjs} | 7 ++--- test/{token.js => token.mjs} | 6 ++--- test/{utils.js => utils.mjs} | 22 +++++++--------- 12 files changed, 73 insertions(+), 78 deletions(-) rename test/{babelmark-responder.js => babelmark-responder.mjs} (72%) rename test/{commonmark.js => commonmark.mjs} (61%) delete mode 100644 test/markdown-it.js create mode 100644 test/markdown-it.mjs rename test/{misc.js => misc.mjs} (98%) rename test/{pathological.js => pathological.mjs} (89%) rename test/{ruler.js => ruler.mjs} (97%) rename test/{token.js => token.mjs} (81%) rename test/{utils.js => utils.mjs} (78%) diff --git a/.eslintrc.yml b/.eslintrc.yml index 27778e3..70e0873 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -1,7 +1,11 @@ env: node: true - browser: false - es6: false + +overrides: + - + files: [ '*.mjs' ] + parserOptions: + sourceType: module ignorePatterns: - coverage/ diff --git a/package.json b/package.json index be31d3d..95d96f5 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "markdown-it": "bin/markdown-it.js" }, "scripts": { - "lint": "eslint .", + "lint": "eslint --ext js --ext mjs .", "test": "npm run lint && nyc mocha && node support/specsplit.js", "coverage": "npm run test && nyc report --reporter html", "report-coveralls": "nyc --reporter=lcov mocha", @@ -56,7 +56,7 @@ "express": "^4.14.0", "gh-pages": "^3.1.0", "highlight.js": "^10.7.2", - "jest-worker": "^26.6.2", + "jest-worker": "^29.7.0", "markdown-it-abbr": "^1.0.4", "markdown-it-container": "^3.0.0", "markdown-it-deflist": "^2.0.0", diff --git a/test/.eslintrc.yml b/test/.eslintrc.yml index e367433..b1644db 100644 --- a/test/.eslintrc.yml +++ b/test/.eslintrc.yml @@ -1,7 +1,6 @@ env: - node: true mocha: true es6: true parserOptions: - ecmaVersion: 2017 + ecmaVersion: 2020 diff --git a/test/babelmark-responder.js b/test/babelmark-responder.mjs similarity index 72% rename from test/babelmark-responder.js rename to test/babelmark-responder.mjs index d45c9ad..c1d3f21 100644 --- a/test/babelmark-responder.js +++ b/test/babelmark-responder.mjs @@ -1,22 +1,24 @@ -'use strict'; +import supertest from 'supertest'; +import { execFile } from 'child_process'; +import { readFileSync } from 'fs'; describe('babelmark responder app', function () { var app; var PORT = 5005; - var request = require('supertest')('http://127.0.0.1:' + PORT); + var request = supertest('http://127.0.0.1:' + PORT); function timeout(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } before(async () => { - app = require('child_process').execFile( + app = execFile( 'node', [ '../support/babelmark-responder.js' ], { - cwd: __dirname, + cwd: new URL('.', import.meta.url), env: Object.assign({}, process.env, { PORT: PORT }) } ); @@ -41,13 +43,15 @@ describe('babelmark responder app', function () { it('do request', () => { + const version = JSON.parse(readFileSync(new URL('../package.json', import.meta.url))).version; + return request .get('/?text=foo') .expect(200) .expect({ html: '

foo

\n', name: 'markdown-it', - version: require('../package.json').version + version }); }); diff --git a/test/commonmark.js b/test/commonmark.mjs similarity index 61% rename from test/commonmark.js rename to test/commonmark.mjs index d0f901e..bff24eb 100644 --- a/test/commonmark.js +++ b/test/commonmark.mjs @@ -1,9 +1,8 @@ -'use strict'; - - -var p = require('path'); -var load = require('markdown-it-testgen').load; -var assert = require('chai').assert; +import { fileURLToPath } from 'node:url'; +import { relative } from 'node:path'; +import { load } from 'markdown-it-testgen'; +import markdownit from '../index.js'; +import { assert } from 'chai'; function normalize(text) { @@ -15,7 +14,7 @@ function generate(path, md) { load(path, function (data) { data.meta = data.meta || {}; - var desc = data.meta.desc || p.relative(path, data.file); + var desc = data.meta.desc || relative(path, data.file); (data.meta.skip ? describe.skip : describe)(desc, function () { data.fixtures.forEach(function (fixture) { @@ -29,7 +28,7 @@ function generate(path, md) { describe('CommonMark', function () { - var md = require('../')('commonmark'); + var md = markdownit('commonmark'); - generate(p.join(__dirname, 'fixtures/commonmark/good.txt'), md); + generate(fileURLToPath(new URL('fixtures/commonmark/good.txt', import.meta.url)), md); }); diff --git a/test/markdown-it.js b/test/markdown-it.js deleted file mode 100644 index 98f5daa..0000000 --- a/test/markdown-it.js +++ /dev/null @@ -1,19 +0,0 @@ -'use strict'; - - -var path = require('path'); - - -var generate = require('markdown-it-testgen'); - - -describe('markdown-it', function () { - var md = require('../')({ - html: true, - langPrefix: '', - typographer: true, - linkify: true - }); - - generate(path.join(__dirname, 'fixtures/markdown-it'), md); -}); diff --git a/test/markdown-it.mjs b/test/markdown-it.mjs new file mode 100644 index 0000000..782fed3 --- /dev/null +++ b/test/markdown-it.mjs @@ -0,0 +1,15 @@ +import { fileURLToPath } from 'node:url'; +import generate from 'markdown-it-testgen'; +import markdownit from '../index.js'; + + +describe('markdown-it', function () { + var md = markdownit({ + html: true, + langPrefix: '', + typographer: true, + linkify: true + }); + + generate(fileURLToPath(new URL('fixtures/markdown-it', import.meta.url)), md); +}); diff --git a/test/misc.js b/test/misc.mjs similarity index 98% rename from test/misc.js rename to test/misc.mjs index 11cd0f4..b1c4c59 100644 --- a/test/misc.js +++ b/test/misc.mjs @@ -1,8 +1,6 @@ -'use strict'; - - -var assert = require('chai').assert; -var markdownit = require('../'); +import { assert } from 'chai'; +import markdownit from '../index.js'; +import forInline from 'markdown-it-for-inline'; describe('API', function () { @@ -269,7 +267,7 @@ describe('Misc', function () { it('Should render link target attr', function () { var md = markdownit() - .use(require('markdown-it-for-inline'), 'target', 'link_open', function (tokens, idx) { + .use(forInline, 'target', 'link_open', function (tokens, idx) { tokens[idx].attrs.push([ 'target', '_blank' ]); }); diff --git a/test/pathological.js b/test/pathological.mjs similarity index 89% rename from test/pathological.js rename to test/pathological.mjs index 4f0b82b..e53c8ab 100644 --- a/test/pathological.js +++ b/test/pathological.mjs @@ -1,17 +1,18 @@ -'use strict'; - - -const needle = require('needle'); -const assert = require('assert'); -const crypto = require('crypto'); -const Worker = require('jest-worker').default; +import needle from 'needle'; +import assert from 'node:assert'; +import crypto from 'node:crypto'; +import { Worker as JestWorker } from 'jest-worker'; +import { readFileSync } from 'fs'; async function test_pattern(str) { - const worker = new Worker(require.resolve('./pathological_worker.js'), { - numWorkers: 1, - enableWorkerThreads: true - }); + const worker = new JestWorker( + new URL('./pathological_worker.js', import.meta.url), + { + numWorkers: 1, + enableWorkerThreads: true + } + ); let result; @@ -49,10 +50,11 @@ describe('Pathological sequences speed', () => { /* eslint-disable max-len */ const src = await needle('get', 'https://raw.githubusercontent.com/commonmark/cmark/master/test/pathological_tests.py'); const src_md5 = crypto.createHash('md5').update(src.body).digest('hex'); + const tracked_md5 = JSON.parse(readFileSync(new URL('./pathological.json', import.meta.url))).md5; assert.strictEqual( src_md5, - require('./pathological.json').md5, + tracked_md5, 'CRC or cmark pathological tests hanged. Verify and update pathological.json' ); }); diff --git a/test/ruler.js b/test/ruler.mjs similarity index 97% rename from test/ruler.js rename to test/ruler.mjs index b61ba19..c49b323 100644 --- a/test/ruler.js +++ b/test/ruler.mjs @@ -1,8 +1,5 @@ -'use strict'; - - -var assert = require('chai').assert; -var Ruler = require('../lib/ruler'); +import { assert } from 'chai'; +import Ruler from '../lib/ruler.js'; describe('Ruler', function () { diff --git a/test/token.js b/test/token.mjs similarity index 81% rename from test/token.js rename to test/token.mjs index 285f052..21ada8d 100644 --- a/test/token.js +++ b/test/token.mjs @@ -1,7 +1,5 @@ -'use strict'; - -var assert = require('chai').assert; -var Token = require('../lib/token'); +import { assert } from 'chai'; +import Token from '../lib/token.js'; describe('Token', function () { diff --git a/test/utils.js b/test/utils.mjs similarity index 78% rename from test/utils.js rename to test/utils.mjs index 0748008..42f7846 100644 --- a/test/utils.js +++ b/test/utils.mjs @@ -1,20 +1,18 @@ -'use strict'; - - -var assert = require('chai').assert; +import { assert } from 'chai'; +import utils from '../lib/common/utils.js'; describe('Utils', function () { it('fromCodePoint', function () { - var fromCodePoint = require('../lib/common/utils').fromCodePoint; + var fromCodePoint = utils.fromCodePoint; assert.strictEqual(fromCodePoint(0x20), ' '); assert.strictEqual(fromCodePoint(0x1F601), '😁'); }); it('isValidEntityCode', function () { - var isValidEntityCode = require('../lib/common/utils').isValidEntityCode; + var isValidEntityCode = utils.isValidEntityCode; assert.strictEqual(isValidEntityCode(0x20), true); assert.strictEqual(isValidEntityCode(0xD800), false); @@ -28,7 +26,7 @@ describe('Utils', function () { }); /*it('replaceEntities', function () { - var replaceEntities = require('../lib/common/utils').replaceEntities; + var replaceEntities = utils.replaceEntities; assert.strictEqual(replaceEntities('&'), '&'); assert.strictEqual(replaceEntities(' '), ' '); @@ -40,7 +38,7 @@ describe('Utils', function () { });*/ it('assign', function () { - var assign = require('../lib/common/utils').assign; + var assign = utils.assign; assert.deepEqual(assign({ a: 1 }, null, { b: 2 }), { a: 1, b: 2 }); assert.throws(function () { @@ -49,13 +47,13 @@ describe('Utils', function () { }); it('escapeRE', function () { - var escapeRE = require('../lib/common/utils').escapeRE; + var escapeRE = utils.escapeRE; assert.strictEqual(escapeRE(' .?*+^$[]\\(){}|-'), ' \\.\\?\\*\\+\\^\\$\\[\\]\\\\\\(\\)\\{\\}\\|\\-'); }); it('isWhiteSpace', function () { - var isWhiteSpace = require('../lib/common/utils').isWhiteSpace; + var isWhiteSpace = utils.isWhiteSpace; assert.strictEqual(isWhiteSpace(0x2000), true); assert.strictEqual(isWhiteSpace(0x09), true); @@ -64,7 +62,7 @@ describe('Utils', function () { }); it('isMdAsciiPunct', function () { - var isMdAsciiPunct = require('../lib/common/utils').isMdAsciiPunct; + var isMdAsciiPunct = utils.isMdAsciiPunct; assert.strictEqual(isMdAsciiPunct(0x30), false); @@ -74,7 +72,7 @@ describe('Utils', function () { }); it('unescapeMd', function () { - var unescapeMd = require('../lib/common/utils').unescapeMd; + var unescapeMd = utils.unescapeMd; assert.strictEqual(unescapeMd('\\foo'), '\\foo'); assert.strictEqual(unescapeMd('foo'), 'foo');