app-entrypoint-css.test.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { loadFixture } from './test-utils.js';
  2. import * as assert from 'node:assert/strict';
  3. import { describe, it, before, after } from 'node:test';
  4. import { load as cheerioLoad } from 'cheerio';
  5. describe('App Entrypoint CSS', () => {
  6. /** @type {import('./test-utils').Fixture} */
  7. let fixture;
  8. before(async () => {
  9. fixture = await loadFixture({
  10. root: './fixtures/app-entrypoint-css/',
  11. });
  12. });
  13. describe('build', () => {
  14. before(async () => {
  15. await fixture.build();
  16. });
  17. it('injects styles referenced in appEntrypoint', async () => {
  18. const html = await fixture.readFile('/index.html');
  19. const $ = cheerioLoad(html);
  20. // test 1: basic component renders
  21. assert.equal($('#foo > #bar').text(), 'works');
  22. // test 2: injects the global style on the page
  23. assert.equal($('style').first().text().trim(), ':root{background-color:red}');
  24. });
  25. it('does not inject styles to pages without a Vue component', async () => {
  26. const html = await fixture.readFile('/unrelated/index.html');
  27. const $ = cheerioLoad(html);
  28. assert.equal($('style').length, 0);
  29. assert.equal($('link[rel="stylesheet"]').length, 0);
  30. });
  31. });
  32. describe('dev', () => {
  33. let devServer;
  34. before(async () => {
  35. devServer = await fixture.startDevServer();
  36. });
  37. after(async () => {
  38. await devServer.stop();
  39. });
  40. it('loads during SSR', async () => {
  41. const html = await fixture.fetch('/').then((res) => res.text());
  42. const $ = cheerioLoad(html);
  43. // test 1: basic component renders
  44. assert.equal($('#foo > #bar').text(), 'works');
  45. // test 2: injects the global style on the page
  46. assert.equal($('style').first().text().replace(/\s+/g, ''), ':root{background-color:red;}');
  47. });
  48. it('does not inject styles to pages without a Vue component', async () => {
  49. const html = await fixture.fetch('/unrelated').then((res) => res.text());
  50. const $ = cheerioLoad(html);
  51. assert.equal($('style').length, 0);
  52. assert.equal($('link[rel="stylesheet"]').length, 0);
  53. });
  54. });
  55. });