detect-nesting.test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { run, html, css } from './util/run'
  2. it('should warn when we detect nested css', () => {
  3. let config = {
  4. content: [{ raw: html`<div class="nested"></div>` }],
  5. }
  6. let input = css`
  7. @tailwind utilities;
  8. .nested {
  9. .example {
  10. }
  11. }
  12. `
  13. return run(input, config).then((result) => {
  14. expect(result.messages).toHaveLength(1)
  15. expect(result.messages).toMatchObject([
  16. {
  17. type: 'warning',
  18. text: [
  19. 'Nested CSS was detected, but CSS nesting has not been configured correctly.',
  20. 'Please enable a CSS nesting plugin *before* Tailwind in your configuration.',
  21. 'See how here: https://tailwindcss.com/docs/using-with-preprocessors#nesting',
  22. ].join('\n'),
  23. },
  24. ])
  25. })
  26. })
  27. it('should not warn when we detect nested css inside css @layer rules', () => {
  28. let config = {
  29. content: [{ raw: html`<div class="underline"></div>` }],
  30. }
  31. let input = css`
  32. @layer tw-base, tw-components, tw-utilities;
  33. @layer tw-utilities {
  34. @tailwind utilities;
  35. }
  36. `
  37. return run(input, config).then((result) => {
  38. expect(result.css).toMatchFormattedCss(css`
  39. @layer tw-base, tw-components, tw-utilities;
  40. @layer tw-utilities {
  41. .underline {
  42. text-decoration-line: underline;
  43. }
  44. }
  45. `)
  46. expect(result.messages).toHaveLength(0)
  47. })
  48. })
  49. it('should warn when we detect namespaced @tailwind at rules', () => {
  50. let config = {
  51. content: [{ raw: html`<div class="text-center"></div>` }],
  52. }
  53. let input = css`
  54. .namespace {
  55. @tailwind utilities;
  56. }
  57. `
  58. return run(input, config).then((result) => {
  59. expect(result.messages).toHaveLength(1)
  60. expect(result.messages).toMatchObject([
  61. {
  62. type: 'warning',
  63. text: [
  64. 'Nested @tailwind rules were detected, but are not supported.',
  65. "Consider using a prefix to scope Tailwind's classes: https://tailwindcss.com/docs/configuration#prefix",
  66. 'Alternatively, use the important selector strategy: https://tailwindcss.com/docs/configuration#selector-strategy',
  67. ].join('\n'),
  68. },
  69. ])
  70. })
  71. })
  72. it('should not warn when nesting a single rule inside a media query', () => {
  73. let config = {
  74. content: [{ raw: html`<div class="nested"></div>` }],
  75. }
  76. let input = css`
  77. @tailwind utilities;
  78. @media (min-width: 768px) {
  79. .nested {
  80. }
  81. }
  82. `
  83. return run(input, config).then((result) => {
  84. expect(result.messages).toHaveLength(0)
  85. expect(result.messages).toEqual([])
  86. })
  87. })
  88. it('should only warn for the first detected nesting ', () => {
  89. let config = {
  90. content: [{ raw: html`<div class="nested other"></div>` }],
  91. }
  92. let input = css`
  93. @tailwind utilities;
  94. .nested {
  95. .example {
  96. }
  97. .other {
  98. }
  99. }
  100. .other {
  101. .example {
  102. }
  103. }
  104. `
  105. return run(input, config).then((result) => {
  106. expect(result.messages).toHaveLength(1)
  107. })
  108. })