diff options
author | 2024-02-04 11:58:44 -0800 | |
---|---|---|
committer | 2024-02-04 19:58:44 +0000 | |
commit | 4260ecf1ff4ba38f5f068c642158f4cc1a4815e5 (patch) | |
tree | 86f3addcd8f3987f0ababe2e84b94fdbf9cc6300 /packages/astro-rss/test/rss.test.js | |
parent | 8b8f26fdf2af2a769f4846bdaaf4cf6b30f9e37c (diff) | |
download | astro-4260ecf1ff4ba38f5f068c642158f4cc1a4815e5.tar.gz astro-4260ecf1ff4ba38f5f068c642158f4cc1a4815e5.tar.zst astro-4260ecf1ff4ba38f5f068c642158f4cc1a4815e5.zip |
chore(`@astrojs/rss`): Migrate tests to `node:test` (#9939)
* Progressively add new tests (remove old later)
* Finalize tests
* Remove unused comments
* Assert that errors are not present and add jsdocs to util function
* Fix
* Remove mocha deps
Diffstat (limited to 'packages/astro-rss/test/rss.test.js')
-rw-r--r-- | packages/astro-rss/test/rss.test.js | 55 |
1 files changed, 33 insertions, 22 deletions
diff --git a/packages/astro-rss/test/rss.test.js b/packages/astro-rss/test/rss.test.js index d32c420f1..dea3e7787 100644 --- a/packages/astro-rss/test/rss.test.js +++ b/packages/astro-rss/test/rss.test.js @@ -1,6 +1,6 @@ -import chai from 'chai'; -import chaiPromises from 'chai-as-promised'; -import chaiXml from 'chai-xml'; +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; + import { z } from 'astro/zod'; import rss, { getRssString } from '../dist/index.js'; import { rssSchema } from '../dist/schema.js'; @@ -14,11 +14,9 @@ import { web1FeedItem, web1FeedItemWithAllData, web1FeedItemWithContent, + parseXmlString, } from './test-utils.js'; -chai.use(chaiPromises); -chai.use(chaiXml); - // note: I spent 30 minutes looking for a nice node-based snapshot tool // ...and I gave up. Enjoy big strings! // prettier-ignore @@ -34,6 +32,15 @@ const validXmlWithStylesheet = `<?xml version="1.0" encoding="UTF-8"?><?xml-styl // prettier-ignore const validXmlWithXSLStylesheet = `<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/feedstylesheet.xsl" type="text/xsl"?><rss version="2.0"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link></channel></rss>`; +function assertXmlDeepEqual(a, b) { + const parsedA = parseXmlString(a); + const parsedB = parseXmlString(b); + + assert.strictEqual(parsedA.err, null); + assert.strictEqual(parsedB.err, null); + assert.deepStrictEqual(parsedA.result, parsedB.result); +} + describe('rss', () => { it('should return a response', async () => { const response = await rss({ @@ -44,10 +51,14 @@ describe('rss', () => { }); const str = await response.text(); - chai.expect(str).xml.to.equal(validXmlResult); + + // NOTE: Chai used the below parser to perform the tests, but I have omitted it for now. + // parser = new xml2js.Parser({ trim: flag(this, 'deep') }); + + assertXmlDeepEqual(str, validXmlResult); const contentType = response.headers.get('Content-Type'); - chai.expect(contentType).to.equal('application/xml'); + assert.strictEqual(contentType, 'application/xml'); }); it('should be the same string as getRssString', async () => { @@ -62,7 +73,7 @@ describe('rss', () => { const str1 = await response.text(); const str2 = await getRssString(options); - chai.expect(str1).to.equal(str2); + assert.strictEqual(str1, str2); }); }); @@ -75,7 +86,7 @@ describe('getRssString', () => { site, }); - chai.expect(str).xml.to.equal(validXmlResult); + assertXmlDeepEqual(str, validXmlResult); }); it('should generate on valid RSSFeedItem array with HTML content included', async () => { @@ -86,7 +97,7 @@ describe('getRssString', () => { site, }); - chai.expect(str).xml.to.equal(validXmlWithContentResult); + assertXmlDeepEqual(str, validXmlWithContentResult); }); it('should generate on valid RSSFeedItem array with all RSS content included', async () => { @@ -97,7 +108,7 @@ describe('getRssString', () => { site, }); - chai.expect(str).xml.to.equal(validXmlResultWithAllData); + assertXmlDeepEqual(str, validXmlResultWithAllData); }); it('should generate on valid RSSFeedItem array with custom data included', async () => { @@ -111,7 +122,7 @@ describe('getRssString', () => { site, }); - chai.expect(str).xml.to.equal(validXmlWithCustomDataResult); + assertXmlDeepEqual(str, validXmlWithCustomDataResult); }); it('should include xml-stylesheet instruction when stylesheet is defined', async () => { @@ -123,7 +134,7 @@ describe('getRssString', () => { stylesheet: '/feedstylesheet.css', }); - chai.expect(str).xml.to.equal(validXmlWithStylesheet); + assertXmlDeepEqual(str, validXmlWithStylesheet); }); it('should include xml-stylesheet instruction with xsl type when stylesheet is set to xsl file', async () => { @@ -135,7 +146,7 @@ describe('getRssString', () => { stylesheet: '/feedstylesheet.xsl', }); - chai.expect(str).xml.to.equal(validXmlWithXSLStylesheet); + assertXmlDeepEqual(str, validXmlWithXSLStylesheet); }); it('should preserve self-closing tags on `customData`', async () => { @@ -152,7 +163,7 @@ describe('getRssString', () => { customData, }); - chai.expect(str).to.contain(customData); + assert.ok(str.includes(customData)); }); it('should not append trailing slash to URLs with the given option', async () => { @@ -164,8 +175,8 @@ describe('getRssString', () => { trailingSlash: false, }); - chai.expect(str).xml.to.contain('https://example.com/<'); - chai.expect(str).xml.to.contain('https://example.com/php<'); + assert.ok(str.includes('https://example.com/<')); + assert.ok(str.includes('https://example.com/php<')); }); it('Deprecated import.meta.glob mapping still works', async () => { @@ -201,7 +212,7 @@ describe('getRssString', () => { site, }); - chai.expect(str).xml.to.equal(validXmlResult); + assertXmlDeepEqual(str, validXmlResult); }); it('should fail when an invalid date string is provided', async () => { @@ -212,8 +223,8 @@ describe('getRssString', () => { link: phpFeedItem.link, }); - chai.expect(res.success).to.be.false; - chai.expect(res.error.issues[0].path[0]).to.equal('pubDate'); + assert.strictEqual(res.success, false); + assert.strictEqual(res.error.issues[0].path[0], 'pubDate'); }); it('should be extendable', () => { @@ -225,7 +236,7 @@ describe('getRssString', () => { } catch (e) { error = e.message; } - chai.expect(error).to.be.null; + assert.strictEqual(error, null); }); it('should not fail when an enclosure has a length of 0', async () => { |