Skip to content

Commit

Permalink
chore: Migrate some astro-*.test.js to node:test (#10078)
Browse files Browse the repository at this point in the history
* chore: Migrate some astro-*.test.js to node:test

* revert astro-dev-headers and astro-client-only

* replace strictEqual with equal in astro-class-list

---------

Co-authored-by: Emanuele Stoppa <[email protected]>
  • Loading branch information
xMohamd and ematipico authored Feb 13, 2024
1 parent f13d897 commit 2168635
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 177 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { describe, before, it } from 'node:test';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

Expand All @@ -22,7 +23,7 @@ describe('Asset URL resolution in build', () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const href = $('link[rel=stylesheet]').attr('href');
expect(href.startsWith('/sub/path/')).to.equal(false);
assert.equal(href.startsWith('/sub/path/'), false);
});
});

Expand All @@ -42,14 +43,14 @@ describe('Asset URL resolution in build', () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const href = $('link[rel=stylesheet]').attr('href');
expect(href.startsWith('/sub/path/')).to.equal(false);
assert.equal(href.startsWith('/sub/path/'), false);
});

it('does include the base subpath', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const href = $('link[rel=stylesheet]').attr('href');
expect(href.startsWith('/another/base/')).to.equal(true);
assert.equal(href.startsWith('/another/base/'), true);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { describe, before, it } from 'node:test';
import * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';
Expand All @@ -22,46 +23,46 @@ describe('Assets Prefix - Static', () => {
const $ = cheerio.load(html);
const stylesheets = $('link[rel="stylesheet"]');
stylesheets.each((i, el) => {
expect(el.attribs.href).to.match(assetsPrefixRegex);
assert.match(el.attribs.href, assetsPrefixRegex);
});
});

it('image src start with assetsPrefix', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const imgAsset = $('#image-asset');
expect(imgAsset.attr('src')).to.match(assetsPrefixRegex);
assert.match(imgAsset.attr('src'), assetsPrefixRegex);
});

it('react component astro-island should import from assetsPrefix', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const island = $('astro-island');
expect(island.attr('component-url')).to.match(assetsPrefixRegex);
expect(island.attr('renderer-url')).to.match(assetsPrefixRegex);
assert.match(island.attr('component-url'), assetsPrefixRegex);
assert.match(island.attr('renderer-url'), assetsPrefixRegex);
});

it('import.meta.env.ASSETS_PREFIX works', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const env = $('#assets-prefix-env');
expect(env.text()).to.equal(assetsPrefix);
assert.equal(env.text(), assetsPrefix);
});

it('markdown image src start with assetsPrefix', async () => {
const html = await fixture.readFile('/markdown/index.html');
const $ = cheerio.load(html);
const imgAssets = $('img');
imgAssets.each((i, el) => {
expect(el.attribs.src).to.match(assetsPrefixRegex);
assert.match(el.attribs.src, assetsPrefixRegex);
});
});

it('content collections image src start with assetsPrefix', async () => {
const html = await fixture.readFile('/blog/index.html');
const $ = cheerio.load(html);
const imgAsset = $('img');
expect(imgAsset.attr('src')).to.match(assetsPrefixRegex);
assert.match(imgAsset.attr('src'), assetsPrefixRegex);
});
});

Expand All @@ -83,7 +84,7 @@ describe('Assets Prefix - with path prefix', () => {
const $ = cheerio.load(html);
const stylesheets = $('link[rel="stylesheet"]');
stylesheets.each((i, el) => {
expect(el.attribs.href).to.match(/^\/starting-slash\/.*/);
assert.match(el.attribs.href, /^\/starting-slash\/.*/);
});
});
});
Expand All @@ -104,44 +105,44 @@ describe('Assets Prefix, server', () => {
it('all stylesheets should start with assetPrefix', async () => {
const request = new Request('http://example.com/custom-base/');
const response = await app.render(request);
expect(response.status).to.equal(200);
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerio.load(html);
const stylesheets = $('link[rel="stylesheet"]');
stylesheets.each((i, el) => {
expect(el.attribs.href).to.match(assetsPrefixRegex);
assert.match(el.attribs.href, assetsPrefixRegex);
});
});

it('image src start with assetsPrefix', async () => {
const request = new Request('http://example.com/custom-base/');
const response = await app.render(request);
expect(response.status).to.equal(200);
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerio.load(html);
const imgAsset = $('#image-asset');
expect(imgAsset.attr('src')).to.match(assetsPrefixRegex);
assert.match(imgAsset.attr('src'), assetsPrefixRegex);
});

it('react component astro-island should import from assetsPrefix', async () => {
const request = new Request('http://example.com/custom-base/');
const response = await app.render(request);
expect(response.status).to.equal(200);
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerio.load(html);
const island = $('astro-island');
expect(island.attr('component-url')).to.match(assetsPrefixRegex);
expect(island.attr('renderer-url')).to.match(assetsPrefixRegex);
assert.match(island.attr('component-url'), assetsPrefixRegex);
assert.match(island.attr('renderer-url'), assetsPrefixRegex);
});

it('markdown optimized image src does not start with assetsPrefix in SSR', async () => {
const request = new Request('http://example.com/custom-base/markdown/');
const response = await app.render(request);
expect(response.status).to.equal(200);
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerio.load(html);
const imgAsset = $('img');
expect(imgAsset.attr('src')).to.not.match(assetsPrefixRegex);
assert.doesNotMatch(imgAsset.attr('src'), assetsPrefixRegex);
});
});

Expand All @@ -164,12 +165,12 @@ describe('Assets Prefix, with path prefix', () => {
it('all stylesheets should start with assetPrefix', async () => {
const request = new Request('http://example.com/custom-base/');
const response = await app.render(request);
expect(response.status).to.equal(200);
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerio.load(html);
const stylesheets = $('link[rel="stylesheet"]');
stylesheets.each((i, el) => {
expect(el.attribs.href).to.match(/^\/starting-slash\/.*/);
assert.match(el.attribs.href, /^\/starting-slash\/.*/);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { describe, before, it } from 'node:test';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import srcsetParse from 'srcset-parse';
Expand All @@ -22,7 +23,7 @@ describe('Assets', () => {
const $ = cheerio.load(html);
const imgPath = $('img').attr('src');
const data = await fixture.readFile(imgPath);
expect(!!data).to.equal(true);
assert.equal(!!data, true);
});

it('built the 2x image', async () => {
Expand All @@ -32,7 +33,7 @@ describe('Assets', () => {
const candidates = matchSrcset(srcset);
const match = candidates.find((a) => a.density === 2);
const data = await fixture.readFile(match.url);
expect(!!data).to.equal(true);
assert.equal(!!data, true);
});

it('built the 3x image', async () => {
Expand All @@ -42,22 +43,22 @@ describe('Assets', () => {
const candidates = matchSrcset(srcset);
const match = candidates.find((a) => a.density === 3);
const data = await fixture.readFile(match.url);
expect(!!data).to.equal(true);
assert.equal(!!data, true);
});

it('built image from an import specifier', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const src = $('#import-no-url').attr('src');
const data = await fixture.readFile(src);
expect(!!data).to.equal(true);
assert.equal(!!data, true);
});

it('built image from an import specifier using ?url', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const src = $('#import-url').attr('src');
const data = await fixture.readFile(src);
expect(!!data).to.equal(true);
assert.equal(!!data, true);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { describe, before, it } from 'node:test';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

Expand Down Expand Up @@ -33,32 +34,32 @@ describe('Attributes', async () => {
for (const id of Object.keys(attrs)) {
const { attribute, value } = attrs[id];
const attr = $(`#${id}`).attr(attribute);
expect(attr).to.equal(value);
assert.equal(attr, value);
}
});

it('Passes boolean attributes to components as expected', async () => {
const html = await fixture.readFile('/component/index.html');
const $ = cheerio.load(html);

expect($('#true').attr('attr')).to.equal('attr-true');
expect($('#true').attr('type')).to.equal('boolean');
expect($('#false').attr('attr')).to.equal('attr-false');
expect($('#false').attr('type')).to.equal('boolean');
assert.equal($('#true').attr('attr'), 'attr-true');
assert.equal($('#true').attr('type'), 'boolean');
assert.equal($('#false').attr('attr'), 'attr-false');
assert.equal($('#false').attr('type'), 'boolean');
});

it('Passes namespaced attributes as expected', async () => {
const html = await fixture.readFile('/namespaced/index.html');
const $ = cheerio.load(html);

expect($('div').attr('xmlns:happy')).to.equal('https://example.com/schemas/happy');
expect($('img').attr('happy:smile')).to.equal('sweet');
assert.equal($('div').attr('xmlns:happy'), 'https://example.com/schemas/happy');
assert.equal($('img').attr('happy:smile'), 'sweet');
});

it('Passes namespaced attributes to components as expected', async () => {
const html = await fixture.readFile('/namespaced-component/index.html');
const $ = cheerio.load(html);

expect($('span').attr('on:click')).to.deep.equal('(event) => console.log(event)');
assert.deepEqual($('span').attr('on:click'), '(event) => console.log(event)');
});
});
Loading

0 comments on commit 2168635

Please sign in to comment.