diff --git a/packages/astro/test/0-css.test.js b/packages/astro/test/0-css.test.ts similarity index 87% rename from packages/astro/test/0-css.test.js rename to packages/astro/test/0-css.test.ts index 35ca255f06cc..3aa14d9f13ee 100644 --- a/packages/astro/test/0-css.test.js +++ b/packages/astro/test/0-css.test.ts @@ -7,10 +7,9 @@ import assert from 'node:assert/strict'; import { after, before, describe, it } from 'node:test'; import * as cheerio from 'cheerio'; -import { loadFixture } from './test-utils.js'; +import { loadFixture, type DevServer, type Fixture } from './test-utils.js'; -/** @type {import('./test-utils').Fixture} */ -let fixture; +let fixture: Fixture; describe('CSS', function () { before(async () => { @@ -19,9 +18,9 @@ describe('CSS', function () { // test HTML and CSS contents for accuracy describe('build', () => { - let $; - let html; - let bundledCSS; + let $: cheerio.CheerioAPI; + let html: string; + let bundledCSS: string; before( async () => { @@ -30,7 +29,7 @@ describe('CSS', function () { // get bundled CSS (will be hashed, hence DOM query) html = await fixture.readFile('/index.html'); $ = cheerio.load(html); - const bundledCSSHREF = $('link[rel=stylesheet][href^=/_astro/]').attr('href'); + const bundledCSSHREF = $('link[rel=stylesheet][href^=/_astro/]').attr('href')!; const bundledCSSFilePath = bundledCSSHREF.replace(/^\/?/, '/'); bundledCSS = (await fixture.readFile(bundledCSSFilePath)) .replace(/\s/g, '') @@ -85,7 +84,7 @@ describe('CSS', function () { }); it('Using hydrated components adds astro-island styles', async () => { - const inline = $('style').html(); + const inline = $('style').html()!; assert.equal(inline.includes('display:contents'), true); }); @@ -100,7 +99,7 @@ describe('CSS', function () { it('Styles through barrel files should only include used Astro scoped styles', async () => { const barrelHtml = await fixture.readFile('/barrel-styles/index.html'); const barrel$ = cheerio.load(barrelHtml); - const barrelBundledCssHref = barrel$('link[rel=stylesheet][href^=/_astro/]').attr('href'); + const barrelBundledCssHref = barrel$('link[rel=stylesheet][href^=/_astro/]').attr('href')!; const style = await fixture.readFile(barrelBundledCssHref.replace(/^\/?/, '/')); assert.match(style, /\.comp-a\[data-astro-cid/); assert.match(style, /\.comp-c\{/); @@ -110,7 +109,7 @@ describe('CSS', function () { it('CSS modules imported in both frontmatter and script should not duplicate', async () => { const duplicateHtml = await fixture.readFile('/css-module-duplicate/index.html'); const $duplicate = cheerio.load(duplicateHtml); - const cssHref = $duplicate('link[rel=stylesheet][href^=/_astro/]').attr('href'); + const cssHref = $duplicate('link[rel=stylesheet][href^=/_astro/]').attr('href')!; const css = await fixture.readFile(cssHref.replace(/^\/?/, '/')); const normalizedCSS = css.replace(/\s+/g, ''); @@ -145,11 +144,12 @@ describe('CSS', function () { it('.module.css', async () => { const el = $('#react-module-css'); - const classes = el.attr('class').split(' '); - const moduleClass = classes.find((name) => /^_title_[\w-]+/.test(name)); + const classAttr = el.attr('class')!; + const classes = classAttr.split(' '); + const moduleClass = classes.find((name) => /^_title_[\w-]+/.test(name))!; // 1. check HTML - assert.equal(el.attr('class').includes(moduleClass), true); + assert.equal(classAttr.includes(moduleClass), true); // 2. check CSS assert.match(bundledCSS, new RegExp(`.${moduleClass}[^{]*{font-family:fantasy`)); @@ -159,7 +159,7 @@ describe('CSS', function () { const el = $('#react-sass'); // 1. check HTML - assert.equal(el.attr('class').includes('react-sass-title'), true); + assert.equal(el.attr('class')!.includes('react-sass-title'), true); // 2. check CSS assert.match(bundledCSS, /.react-sass-title[^{]*\{font-family:fantasy/); @@ -169,7 +169,7 @@ describe('CSS', function () { const el = $('#react-scss'); // 1. check HTML - assert.equal(el.attr('class').includes('react-scss-title'), true); + assert.equal(el.attr('class')!.includes('react-scss-title'), true); // 2. check CSS assert.match(bundledCSS, /.react-scss-title[^{]*\{font-family:fantasy/); @@ -177,11 +177,12 @@ describe('CSS', function () { it('.module.sass', async () => { const el = $('#react-module-sass'); - const classes = el.attr('class').split(' '); - const moduleClass = classes.find((name) => /^_title_[\w-]+/.test(name)); + const classAttr = el.attr('class')!; + const classes = classAttr.split(' '); + const moduleClass = classes.find((name) => /^_title_[\w-]+/.test(name))!; // 1. check HTML - assert.equal(el.attr('class').includes(moduleClass), true); + assert.equal(classAttr.includes(moduleClass), true); // 2. check CSS assert.match(bundledCSS, new RegExp(`.${moduleClass}[^{]*{font-family:fantasy`)); @@ -189,11 +190,12 @@ describe('CSS', function () { it('.module.scss', async () => { const el = $('#react-module-scss'); - const classes = el.attr('class').split(' '); - const moduleClass = classes.find((name) => /^_title_[\w-]+/.test(name)); + const classAttr = el.attr('class')!; + const classes = classAttr.split(' '); + const moduleClass = classes.find((name) => /^_title_[\w-]+/.test(name))!; // 1. check HTML - assert.equal(el.attr('class').includes(moduleClass), true); + assert.equal(classAttr.includes(moduleClass), true); // 2. check CSS assert.match(bundledCSS, new RegExp(`.${moduleClass}[^{]*{font-family:fantasy`)); @@ -214,7 +216,7 @@ describe('CSS', function () { const el = $('#vue-css'); // 1. check HTML - assert.equal(el.attr('class').includes('vue-css'), true); + assert.equal(el.attr('class')!.includes('vue-css'), true); // 2. check CSS assert.match(bundledCSS, /.vue-css[^{]*\{font-family:cursive/); @@ -224,12 +226,12 @@ describe('CSS', function () { const el = $('#vue-scoped'); // find data-v-* attribute (how Vue CSS scoping works) - const { attribs } = el.get(0); + const { attribs } = el.get(0)!; const scopeId = Object.keys(attribs).find((k) => k.startsWith('data-v-')); assert.ok(scopeId); // 1. check HTML - assert.equal(el.attr('class').includes('vue-scoped'), true); + assert.equal(el.attr('class')!.includes('vue-scoped'), true); // 2. check CSS assert.equal(bundledCSS.includes(`.vue-scoped[${scopeId}]`), true); @@ -237,11 +239,12 @@ describe('CSS', function () { it('