mirror of
https://github.com/withastro/astro.git
synced 2025-01-27 22:19:04 -05:00
chore: Migrate content-collection-**.test.js
to node:test
(#10098)
* test: move to node:test * test: move content-collections-render to node:test * chore: bring back isWindows --------- Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
This commit is contained in:
parent
3007d24c98
commit
3387f75c99
2 changed files with 94 additions and 79 deletions
|
@ -1,4 +1,5 @@
|
|||
import { expect } from 'chai';
|
||||
import * as assert from 'node:assert/strict';
|
||||
import { after, describe, before, it } from 'node:test';
|
||||
import * as cheerio from 'cheerio';
|
||||
import { fixLineEndings, loadFixture } from './test-utils.js';
|
||||
|
||||
|
@ -39,31 +40,32 @@ describe('Content Collections - references', () => {
|
|||
});
|
||||
|
||||
it('Returns expected keys', () => {
|
||||
expect(json).to.haveOwnProperty('welcomePost');
|
||||
expect(json).to.haveOwnProperty('banner');
|
||||
expect(json).to.haveOwnProperty('author');
|
||||
expect(json).to.haveOwnProperty('relatedPosts');
|
||||
assert.ok(json.hasOwnProperty('welcomePost'));
|
||||
assert.ok(json.hasOwnProperty('banner'));
|
||||
assert.ok(json.hasOwnProperty('author'));
|
||||
assert.ok(json.hasOwnProperty('relatedPosts'));
|
||||
});
|
||||
|
||||
it('Returns `banner` data', () => {
|
||||
const { banner } = json;
|
||||
expect(banner).to.haveOwnProperty('data');
|
||||
expect(banner.id).to.equal('welcome');
|
||||
expect(banner.collection).to.equal('banners');
|
||||
expect(banner.data.alt).to.equal(
|
||||
assert.ok(banner.hasOwnProperty('data'));
|
||||
assert.equal(banner.id, 'welcome');
|
||||
assert.equal(banner.collection, 'banners');
|
||||
assert.equal(
|
||||
banner.data.alt,
|
||||
'Futuristic landscape with chrome buildings and blue skies'
|
||||
);
|
||||
|
||||
expect(banner.data.src.width).to.equal(400);
|
||||
expect(banner.data.src.height).to.equal(225);
|
||||
expect(banner.data.src.format).to.equal('jpg');
|
||||
expect(banner.data.src.src.includes('the-future')).to.be.true;
|
||||
assert.equal(banner.data.src.width, 400);
|
||||
assert.equal(banner.data.src.height, 225);
|
||||
assert.equal(banner.data.src.format, 'jpg');
|
||||
assert.equal(banner.data.src.src.includes('the-future'), true);
|
||||
});
|
||||
|
||||
it('Returns `author` data', () => {
|
||||
const { author } = json;
|
||||
expect(author).to.haveOwnProperty('data');
|
||||
expect(author).to.deep.equal({
|
||||
assert.ok(author.hasOwnProperty('data'));
|
||||
assert.deepEqual(author, {
|
||||
id: 'nate-moore',
|
||||
collection: 'authors',
|
||||
data: {
|
||||
|
@ -75,12 +77,12 @@ describe('Content Collections - references', () => {
|
|||
|
||||
it('Returns `relatedPosts` data', () => {
|
||||
const { relatedPosts } = json;
|
||||
expect(Array.isArray(relatedPosts)).to.be.true;
|
||||
assert.equal(Array.isArray(relatedPosts), true, 'Expected relatedPosts to be an array');
|
||||
const topLevelInfo = relatedPosts.map(({ data, body, ...meta }) => ({
|
||||
...meta,
|
||||
body: fixLineEndings(body).trim(),
|
||||
}));
|
||||
expect(topLevelInfo).to.deep.equal([
|
||||
assert.deepEqual(topLevelInfo, [
|
||||
{
|
||||
id: 'related-1.md',
|
||||
slug: 'related-1',
|
||||
|
@ -95,7 +97,7 @@ describe('Content Collections - references', () => {
|
|||
},
|
||||
]);
|
||||
const postData = relatedPosts.map(({ data }) => data);
|
||||
expect(postData).to.deep.equal([
|
||||
assert.deepEqual(postData, [
|
||||
{
|
||||
title: 'Related post 1',
|
||||
banner: { id: 'welcome', collection: 'banners' },
|
||||
|
@ -125,32 +127,33 @@ describe('Content Collections - references', () => {
|
|||
|
||||
it('Renders `banner` data', () => {
|
||||
const banner = $('img[data-banner]');
|
||||
expect(banner.length).to.equal(1);
|
||||
expect(banner.attr('src')).to.include('the-future');
|
||||
expect(banner.attr('alt')).to.equal(
|
||||
assert.equal(banner.length, 1);
|
||||
assert.ok(banner.attr('src').includes('the-future'));
|
||||
assert.equal(
|
||||
banner.attr('alt'),
|
||||
'Futuristic landscape with chrome buildings and blue skies'
|
||||
);
|
||||
expect(banner.attr('width')).to.equal('400');
|
||||
expect(banner.attr('height')).to.equal('225');
|
||||
assert.equal(banner.attr('width'), '400');
|
||||
assert.equal(banner.attr('height'), '225');
|
||||
});
|
||||
|
||||
it('Renders `author` data', () => {
|
||||
const author = $('a[data-author-name]');
|
||||
expect(author.length).to.equal(1);
|
||||
expect(author.attr('href')).to.equal('https://twitter.com/n_moore');
|
||||
expect(author.text()).to.equal('Nate Something Moore');
|
||||
assert.equal(author.length, 1);
|
||||
assert.equal(author.attr('href'), 'https://twitter.com/n_moore');
|
||||
assert.equal(author.text(), 'Nate Something Moore');
|
||||
});
|
||||
|
||||
it('Renders `relatedPosts` data', () => {
|
||||
const relatedPosts = $('ul[data-related-posts]');
|
||||
expect(relatedPosts.length).to.equal(1);
|
||||
assert.equal(relatedPosts.length, 1);
|
||||
const relatedPost1 = relatedPosts.find('li').eq(0);
|
||||
|
||||
expect(relatedPost1.find('a').attr('href')).to.equal('/blog/related-1');
|
||||
expect(relatedPost1.find('a').text()).to.equal('Related post 1');
|
||||
assert.equal(relatedPost1.find('a').attr('href'), '/blog/related-1');
|
||||
assert.equal(relatedPost1.find('a').text(), 'Related post 1');
|
||||
const relatedPost2 = relatedPosts.find('li').eq(1);
|
||||
expect(relatedPost2.find('a').attr('href')).to.equal('/blog/related-2');
|
||||
expect(relatedPost2.find('a').text()).to.equal('Related post 2');
|
||||
assert.equal(relatedPost2.find('a').attr('href'), '/blog/related-2');
|
||||
assert.equal(relatedPost2.find('a').text(), 'Related post 2');
|
||||
});
|
||||
});
|
||||
});
|
|
@ -1,9 +1,12 @@
|
|||
import { expect } from 'chai';
|
||||
import * as assert from 'node:assert/strict';
|
||||
import { after, describe, before, it } from 'node:test';
|
||||
import * as cheerio from 'cheerio';
|
||||
import { loadFixture, isWindows } from './test-utils.js';
|
||||
import testAdapter from './test-adapter.js';
|
||||
|
||||
const describe = isWindows ? global.describe.skip : global.describe;
|
||||
if(!isWindows) {
|
||||
describe()
|
||||
}
|
||||
|
||||
describe('Content Collections - render()', () => {
|
||||
describe('Build - SSG', () => {
|
||||
|
@ -24,10 +27,10 @@ describe('Content Collections - render()', () => {
|
|||
const $ = cheerio.load(html);
|
||||
|
||||
// Renders content
|
||||
expect($('ul li')).to.have.a.lengthOf(3);
|
||||
assert.equal($('ul li').length, 3);
|
||||
|
||||
// Includes styles
|
||||
expect($('link[rel=stylesheet]')).to.have.a.lengthOf(1);
|
||||
assert.equal($('link[rel=stylesheet]').length, 1);
|
||||
});
|
||||
|
||||
it('Excludes CSS for non-rendered entries', async () => {
|
||||
|
@ -35,7 +38,7 @@ describe('Content Collections - render()', () => {
|
|||
const $ = cheerio.load(html);
|
||||
|
||||
// Excludes styles
|
||||
expect($('link[rel=stylesheet]')).to.have.a.lengthOf(0);
|
||||
assert.equal($('link[rel=stylesheet]').length, 0);
|
||||
});
|
||||
|
||||
it('De-duplicates CSS used both in layout and directly in target page', async () => {
|
||||
|
@ -46,13 +49,13 @@ describe('Content Collections - render()', () => {
|
|||
|
||||
$('link[rel=stylesheet]').each((_, linkEl) => {
|
||||
const href = linkEl.attribs.href;
|
||||
expect(set).to.not.contain(href);
|
||||
assert.equal(set.has(href), false);
|
||||
set.add(href);
|
||||
});
|
||||
|
||||
$('style').each((_, styleEl) => {
|
||||
const textContent = styleEl.children[0].data;
|
||||
expect(set).to.not.contain(textContent);
|
||||
assert.equal(set.has(textContent), false);
|
||||
set.add(textContent);
|
||||
});
|
||||
});
|
||||
|
@ -62,16 +65,20 @@ describe('Content Collections - render()', () => {
|
|||
const $ = cheerio.load(html);
|
||||
|
||||
const allScripts = $('head > script[type="module"]');
|
||||
expect(allScripts).to.have.length;
|
||||
assert.ok(allScripts.length);
|
||||
|
||||
// Includes hoisted script
|
||||
expect(
|
||||
[...allScripts].find((script) => $(script).attr('src')?.includes('WithScripts')),
|
||||
const scriptWithSrc = [...allScripts].find((script) =>
|
||||
$(script).attr('src')?.includes('WithScripts')
|
||||
);
|
||||
assert.notEqual(
|
||||
scriptWithSrc,
|
||||
undefined,
|
||||
'`WithScripts.astro` hoisted script missing from head.'
|
||||
).to.not.be.undefined;
|
||||
);
|
||||
|
||||
// Includes inline script
|
||||
expect($('script[data-is-inline]')).to.have.a.lengthOf(1);
|
||||
assert.equal($('script[data-is-inline]').length, 1);
|
||||
});
|
||||
|
||||
it('Excludes component scripts for non-rendered entries', async () => {
|
||||
|
@ -81,12 +88,14 @@ describe('Content Collections - render()', () => {
|
|||
const allScripts = $('head > script[type="module"]');
|
||||
|
||||
// Excludes hoisted script
|
||||
expect(
|
||||
[...allScripts].find((script) =>
|
||||
$(script).text().includes('document.querySelector("#update-me")')
|
||||
),
|
||||
const scriptWithText = [...allScripts].find((script) =>
|
||||
$(script).text().includes('document.querySelector("#update-me")')
|
||||
);
|
||||
assert.equal(
|
||||
scriptWithText,
|
||||
undefined,
|
||||
'`WithScripts.astro` hoisted script included unexpectedly.'
|
||||
).to.be.undefined;
|
||||
);
|
||||
});
|
||||
|
||||
it('Applies MDX components export', async () => {
|
||||
|
@ -94,8 +103,8 @@ describe('Content Collections - render()', () => {
|
|||
const $ = cheerio.load(html);
|
||||
|
||||
const h2 = $('h2');
|
||||
expect(h2).to.have.a.lengthOf(1);
|
||||
expect(h2.attr('data-components-export-applied')).to.equal('true');
|
||||
assert.equal(h2.length, 1);
|
||||
assert.equal(h2.attr('data-components-export-applied'), 'true');
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -122,10 +131,10 @@ describe('Content Collections - render()', () => {
|
|||
const $ = cheerio.load(html);
|
||||
|
||||
// Renders content
|
||||
expect($('ul li')).to.have.a.lengthOf(3);
|
||||
assert.equal($('ul li').length, 3);
|
||||
|
||||
// Includes styles
|
||||
expect($('link[rel=stylesheet]')).to.have.a.lengthOf(1);
|
||||
assert.equal($('link[rel=stylesheet]').length, 1);
|
||||
});
|
||||
|
||||
it('Exclude CSS for non-rendered entries', async () => {
|
||||
|
@ -136,7 +145,7 @@ describe('Content Collections - render()', () => {
|
|||
const $ = cheerio.load(html);
|
||||
|
||||
// Includes styles
|
||||
expect($('link[rel=stylesheet]')).to.have.a.lengthOf(0);
|
||||
assert.equal($('link[rel=stylesheet]').length, 0);
|
||||
});
|
||||
|
||||
it('De-duplicates CSS used both in layout and directly in target page', async () => {
|
||||
|
@ -150,13 +159,13 @@ describe('Content Collections - render()', () => {
|
|||
|
||||
$('link[rel=stylesheet]').each((_, linkEl) => {
|
||||
const href = linkEl.attribs.href;
|
||||
expect(set).to.not.contain(href);
|
||||
assert.equal(set.has(href), false);
|
||||
set.add(href);
|
||||
});
|
||||
|
||||
$('style').each((_, styleEl) => {
|
||||
const textContent = styleEl.children[0].data;
|
||||
expect(set).to.not.contain(textContent);
|
||||
assert.equal(set.has(textContent), false);
|
||||
set.add(textContent);
|
||||
});
|
||||
});
|
||||
|
@ -169,8 +178,8 @@ describe('Content Collections - render()', () => {
|
|||
const $ = cheerio.load(html);
|
||||
|
||||
const h2 = $('h2');
|
||||
expect(h2).to.have.a.lengthOf(1);
|
||||
expect(h2.attr('data-components-export-applied')).to.equal('true');
|
||||
assert.equal(h2.length, 1);
|
||||
assert.equal(h2.attr('data-components-export-applied'), 'true');
|
||||
});
|
||||
|
||||
it('getCollection should return new instances of the array to be mutated safely', async () => {
|
||||
|
@ -180,13 +189,13 @@ describe('Content Collections - render()', () => {
|
|||
let response = await app.render(request);
|
||||
let html = await response.text();
|
||||
let $ = cheerio.load(html);
|
||||
expect($('li').first().text()).to.equal('With Layout Prop');
|
||||
assert.equal($('li').first().text(), 'With Layout Prop');
|
||||
|
||||
request = new Request('http://example.com/');
|
||||
response = await app.render(request);
|
||||
html = await response.text();
|
||||
$ = cheerio.load(html);
|
||||
expect($('li').first().text()).to.equal('Hello world');
|
||||
assert.equal($('li').first().text(), 'Hello world');
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -208,68 +217,71 @@ describe('Content Collections - render()', () => {
|
|||
|
||||
it('Includes CSS for rendered entry', async () => {
|
||||
const response = await fixture.fetch('/launch-week', { method: 'GET' });
|
||||
expect(response.status).to.equal(200);
|
||||
assert.equal(response.status, 200);
|
||||
|
||||
const html = await response.text();
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
// Renders content
|
||||
expect($('ul li')).to.have.a.lengthOf(3);
|
||||
assert.equal($('ul li').length, 3);
|
||||
|
||||
// Includes styles
|
||||
expect($('head > style')).to.have.a.lengthOf(1);
|
||||
expect($('head > style').text()).to.include("font-family: 'Comic Sans MS'");
|
||||
assert.equal($('head > style').length, 1);
|
||||
assert.ok($('head > style').text().includes("font-family: 'Comic Sans MS'"));
|
||||
});
|
||||
|
||||
it('Includes component scripts for rendered entry', async () => {
|
||||
const response = await fixture.fetch('/launch-week-component-scripts', { method: 'GET' });
|
||||
expect(response.status).to.equal(200);
|
||||
assert.equal(response.status, 200);
|
||||
|
||||
const html = await response.text();
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
const allScripts = $('head > script[src]');
|
||||
expect(allScripts).to.have.length;
|
||||
|
||||
assert.ok(allScripts.length);
|
||||
// Includes hoisted script
|
||||
expect(
|
||||
[...allScripts].find((script) => script.attribs.src.includes('WithScripts.astro')),
|
||||
const scriptWithSrc = [...allScripts].find((script) =>
|
||||
script.attribs.src.includes('WithScripts.astro')
|
||||
);
|
||||
assert.notEqual(
|
||||
scriptWithSrc,
|
||||
undefined,
|
||||
'`WithScripts.astro` hoisted script missing from head.'
|
||||
).to.not.be.undefined;
|
||||
);
|
||||
|
||||
// Includes inline script
|
||||
expect($('script[data-is-inline]')).to.have.a.lengthOf(1);
|
||||
assert.equal($('script[data-is-inline]').length, 1);
|
||||
});
|
||||
|
||||
it('Applies MDX components export', async () => {
|
||||
const response = await fixture.fetch('/launch-week-components-export', { method: 'GET' });
|
||||
expect(response.status).to.equal(200);
|
||||
assert.equal(response.status, 200);
|
||||
|
||||
const html = await response.text();
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
const h2 = $('h2');
|
||||
expect(h2).to.have.a.lengthOf(1);
|
||||
expect(h2.attr('data-components-export-applied')).to.equal('true');
|
||||
assert.equal(h2.length, 1);
|
||||
assert.equal(h2.attr('data-components-export-applied'), 'true');
|
||||
});
|
||||
|
||||
it('Supports layout prop with recursive getCollection() call', async () => {
|
||||
const response = await fixture.fetch('/with-layout-prop', { method: 'GET' });
|
||||
expect(response.status).to.equal(200);
|
||||
assert.equal(response.status, 200);
|
||||
|
||||
const html = await response.text();
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
const body = $('body');
|
||||
expect(body.attr('data-layout-prop')).to.equal('true');
|
||||
assert.equal(body.attr('data-layout-prop'), 'true');
|
||||
|
||||
const h1 = $('h1');
|
||||
expect(h1).to.have.a.lengthOf(1);
|
||||
expect(h1.text()).to.equal('With Layout Prop');
|
||||
assert.equal(h1.length, 1);
|
||||
assert.equal(h1.text(), 'With Layout Prop');
|
||||
|
||||
const h2 = $('h2');
|
||||
expect(h2).to.have.a.lengthOf(1);
|
||||
expect(h2.text()).to.equal('Content with a layout prop');
|
||||
assert.equal(h2.length, 1);
|
||||
assert.equal(h2.text(), 'Content with a layout prop');
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue