From a3b963824273206f8663b5eafbaf13699b6ad08f Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Sat, 30 Mar 2024 19:31:52 +0800 Subject: [PATCH] refactor: migrate typescript (#5430) * chore: remove unused test entry file * refactor: migrate ts --- test/fixtures/.eslintrc | 6 ++++ .../{post_render.js => post_render.ts} | 16 ++++----- test/index.js | 20 ----------- test/scripts/generators/asset.ts | 4 +-- test/scripts/hexo/hexo.ts | 4 +-- test/scripts/hexo/post.ts | 34 +++++++++---------- test/scripts/hexo/router.ts | 6 ++-- test/util/.eslintrc | 6 ++++ test/util/index.js | 3 -- test/util/index.ts | 1 + test/util/{stream.js => stream.ts} | 8 ++--- 11 files changed, 46 insertions(+), 62 deletions(-) create mode 100644 test/fixtures/.eslintrc rename test/fixtures/{post_render.js => post_render.ts} (88%) delete mode 100644 test/index.js create mode 100644 test/util/.eslintrc delete mode 100644 test/util/index.js create mode 100644 test/util/index.ts rename test/util/{stream.js => stream.ts} (65%) diff --git a/test/fixtures/.eslintrc b/test/fixtures/.eslintrc new file mode 100644 index 0000000000..f25990b7d4 --- /dev/null +++ b/test/fixtures/.eslintrc @@ -0,0 +1,6 @@ +{ + "extends": "hexo/ts", + "rules": { + "node/no-unsupported-features/es-syntax": 0 + } +} \ No newline at end of file diff --git a/test/fixtures/post_render.js b/test/fixtures/post_render.ts similarity index 88% rename from test/fixtures/post_render.js rename to test/fixtures/post_render.ts index f9e7351b49..c631822a29 100644 --- a/test/fixtures/post_render.js +++ b/test/fixtures/post_render.ts @@ -1,13 +1,11 @@ -'use strict'; - -const { highlight } = require('hexo-util'); +import { highlight } from 'hexo-util'; const code = [ 'if tired && night:', ' sleep()' ].join('\n'); -exports.content = [ +export const content = [ '# Title', '``` python', code, @@ -24,7 +22,7 @@ exports.content = [ '{% endquote %}' ].join('\n'); -exports.expected = [ +export const expected = [ '

Title

', highlight(code, {lang: 'python'}), '\n

some content

\n', @@ -36,7 +34,7 @@ exports.expected = [ '' ].join(''); -exports.expected_disable_nunjucks = [ +export const expected_disable_nunjucks = [ '

Title

', highlight(code, {lang: 'python'}), '\n

some content

\n', @@ -49,7 +47,7 @@ exports.expected_disable_nunjucks = [ '{% endquote %}

' ].join(''); -exports.content_for_issue_3346 = [ +export const content_for_issue_3346 = [ '# Title', '```', '{% test1 %}', @@ -63,7 +61,7 @@ exports.content_for_issue_3346 = [ '{% endblockquote %}' ].join('\n'); -exports.expected_for_issue_3346 = [ +export const expected_for_issue_3346 = [ '

Title

', highlight('{% test1 %}\n{{ test2 }}').replace(/{/g, '{').replace(/}/g, '}'), // Escaped by backtick_code_block '\n

some content

\n', @@ -73,7 +71,7 @@ exports.expected_for_issue_3346 = [ '' ].join(''); -exports.content_for_issue_4460 = [ +export const content_for_issue_4460 = [ '```html', '', '', diff --git a/test/index.js b/test/index.js deleted file mode 100644 index 5baf989398..0000000000 --- a/test/index.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -const chai = require('chai'); -global.should = chai.should(); - -describe('Hexo', () => { - require('./scripts/box'); - require('./scripts/console'); - require('./scripts/extend'); - require('./scripts/filters'); - require('./scripts/generators'); - require('./scripts/helpers'); - require('./scripts/hexo'); - require('./scripts/models'); - require('./scripts/processors'); - require('./scripts/renderers'); - require('./scripts/tags'); - require('./scripts/theme'); - require('./scripts/theme_processors'); -}); diff --git a/test/scripts/generators/asset.ts b/test/scripts/generators/asset.ts index 72ea4dc29c..185de808f9 100644 --- a/test/scripts/generators/asset.ts +++ b/test/scripts/generators/asset.ts @@ -1,6 +1,6 @@ import { join } from 'path'; import { mkdirs, rmdir, unlink, writeFile } from 'hexo-fs'; -import testUtil from '../../util'; +import { readStream } from '../../util'; import Hexo from '../../../lib/hexo'; import assetGenerator from '../../../lib/plugins/generator/asset'; import { spy } from 'sinon'; @@ -15,7 +15,7 @@ describe('asset', () => { const Asset = hexo.model('Asset'); const checkStream = async (stream, expected) => { - const data = await testUtil.stream.read(stream); + const data = await readStream(stream); data.should.eql(expected); }; diff --git a/test/scripts/hexo/hexo.ts b/test/scripts/hexo/hexo.ts index 6ab320ecd5..9dfae90aaf 100644 --- a/test/scripts/hexo/hexo.ts +++ b/test/scripts/hexo/hexo.ts @@ -3,7 +3,7 @@ import { mkdirs, rmdir, unlink, writeFile } from 'hexo-fs'; // @ts-ignore import Promise from 'bluebird'; import { spy } from 'sinon'; -import testUtil from '../../util'; +import { readStream } from '../../util'; import { full_url_for } from 'hexo-util'; import Hexo from '../../../lib/hexo'; import chai from 'chai'; @@ -20,7 +20,7 @@ describe('Hexo', () => { const { route } = hexo; async function checkStream(stream, expected) { - const data = await testUtil.stream.read(stream); + const data = await readStream(stream); data.should.eql(expected); } diff --git a/test/scripts/hexo/post.ts b/test/scripts/hexo/post.ts index c101b768d6..b01e41d61f 100644 --- a/test/scripts/hexo/post.ts +++ b/test/scripts/hexo/post.ts @@ -3,7 +3,7 @@ import moment from 'moment'; import { readFile, mkdirs, unlink, rmdir, writeFile, exists, stat, listDir } from 'hexo-fs'; import { spy, useFakeTimers } from 'sinon'; import { parse as yfm } from 'hexo-front-matter'; -import fixture from '../../fixtures/post_render'; +import { expected, content, expected_disable_nunjucks, content_for_issue_3346, expected_for_issue_3346, content_for_issue_4460 } from '../../fixtures/post_render'; import { highlight } from 'hexo-util'; import Hexo from '../../../lib/hexo'; import chai from 'chai'; @@ -659,17 +659,17 @@ describe('Post', () => { hexo.extend.filter.register('after_post_render', afterHook); const data = await post.render('', { - content: fixture.content, + content, engine: 'markdown' }); - data.content.trim().should.eql(fixture.expected); + data.content.trim().should.eql(expected); beforeHook.calledOnce.should.be.true; afterHook.calledOnce.should.be.true; }); it('render() - callback', done => { post.render('', { - content: fixture.content, + content, engine: 'markdown' }, err => { done(err); @@ -795,10 +795,10 @@ describe('Post', () => { try { const data = await post.render('', { - content: fixture.content, + content, engine: 'markdown' }); - data.content.trim().should.eql(fixture.expected_disable_nunjucks); + data.content.trim().should.eql(expected_disable_nunjucks); } finally { renderer.disableNunjucks = false; } @@ -811,10 +811,10 @@ describe('Post', () => { try { const data = await post.render('', { - content: fixture.content, + content, engine: 'markdown' }); - data.content.trim().should.eql(fixture.expected); + data.content.trim().should.eql(expected); } finally { renderer.disableNunjucks = false; } @@ -848,11 +848,11 @@ describe('Post', () => { try { const data = await post.render('', { - content: fixture.content, + content, engine: 'markdown', disableNunjucks: false }); - data.content.trim().should.eql(fixture.expected); + data.content.trim().should.eql(expected); } finally { renderer.disableNunjucks = false; } @@ -864,11 +864,11 @@ describe('Post', () => { try { const data = await post.render('', { - content: fixture.content, + content, engine: 'markdown', disableNunjucks: true }); - data.content.trim().should.eql(fixture.expected_disable_nunjucks); + data.content.trim().should.eql(expected_disable_nunjucks); } finally { renderer.disableNunjucks = false; } @@ -881,12 +881,12 @@ describe('Post', () => { try { const data = await post.render('', { - content: fixture.content, + content, engine: 'markdown', // @ts-ignore disableNunjucks: null }); - data.content.trim().should.eql(fixture.expected_disable_nunjucks); + data.content.trim().should.eql(expected_disable_nunjucks); } finally { renderer.disableNunjucks = false; } @@ -1155,14 +1155,14 @@ describe('Post', () => { // #3346 it('render() - swig tag inside backtick code block', async () => { - const content = fixture.content_for_issue_3346; + const content = content_for_issue_3346; const data = await post.render('', { content, engine: 'markdown' }); - data.content.trim().should.eql(fixture.expected_for_issue_3346); + data.content.trim().should.eql(expected_for_issue_3346); }); // test for https://github.com/hexojs/hexo/pull/4171#issuecomment-594412367 @@ -1343,7 +1343,7 @@ describe('Post', () => { it('render() - issue #4460', async () => { hexo.config.syntax_highlighter = 'prismjs'; - const content = fixture.content_for_issue_4460; + const content = content_for_issue_4460; const data = await post.render('', { content, diff --git a/test/scripts/hexo/router.ts b/test/scripts/hexo/router.ts index 2fed61bef8..5683a13636 100644 --- a/test/scripts/hexo/router.ts +++ b/test/scripts/hexo/router.ts @@ -5,7 +5,7 @@ import { join } from 'path'; import crypto from 'crypto'; import { createReadStream } from 'hexo-fs'; import { spy, assert as sinonAssert } from 'sinon'; -import testUtil from '../../util'; +import { readStream } from '../../util'; import Router from '../../../lib/hexo/router'; import chai from 'chai'; const should = chai.should(); @@ -14,7 +14,7 @@ describe('Router', () => { const router = new Router(); function checkStream(stream, expected) { - return testUtil.stream.read(stream).then(data => { + return readStream(stream).then(data => { data.should.eql(expected); }); } @@ -125,7 +125,7 @@ describe('Router', () => { throw new Error('error test'); }); - return testUtil.stream.read(router.get('test')).then(() => { + return readStream(router.get('test')).then(() => { should.fail('Return value must be rejected'); }, err => { err.should.have.property('message', 'error test'); diff --git a/test/util/.eslintrc b/test/util/.eslintrc new file mode 100644 index 0000000000..f25990b7d4 --- /dev/null +++ b/test/util/.eslintrc @@ -0,0 +1,6 @@ +{ + "extends": "hexo/ts", + "rules": { + "node/no-unsupported-features/es-syntax": 0 + } +} \ No newline at end of file diff --git a/test/util/index.js b/test/util/index.js deleted file mode 100644 index 1d03088e3e..0000000000 --- a/test/util/index.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict'; - -exports.stream = require('./stream'); diff --git a/test/util/index.ts b/test/util/index.ts new file mode 100644 index 0000000000..2e9c6cef28 --- /dev/null +++ b/test/util/index.ts @@ -0,0 +1 @@ +export { readStream } from './stream'; diff --git a/test/util/stream.js b/test/util/stream.ts similarity index 65% rename from test/util/stream.js rename to test/util/stream.ts index 930e4ccdc4..e53e9d8a30 100644 --- a/test/util/stream.js +++ b/test/util/stream.ts @@ -1,8 +1,6 @@ -'use strict'; +import Promise from 'bluebird'; -const Promise = require('bluebird'); - -function readStream(stream) { +export function readStream(stream): Promise { return new Promise((resolve, reject) => { let data = ''; @@ -13,5 +11,3 @@ function readStream(stream) { }).on('error', reject); }); } - -exports.read = readStream;