run.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import pkg from '../../package.json'
  2. import postcss from 'postcss'
  3. import tailwind from '../../src'
  4. import corePluginList from '../../src/corePluginList'
  5. import resolveConfig from '../../src/public/resolve-config'
  6. import { createContext } from '../../src/lib/setupContextUtils'
  7. import { variantPlugins } from '../../src/corePlugins'
  8. import { css } from './strings'
  9. export * from './strings'
  10. export * from './defaults'
  11. export let map = JSON.stringify({
  12. version: 3,
  13. file: null,
  14. sources: [],
  15. names: [],
  16. mappings: '',
  17. })
  18. export function run(input, config, plugin = tailwind) {
  19. let { currentTestName, testPath } = expect.getState()
  20. let path = `${testPath}?test=${Buffer.from(currentTestName).toString('base64')}`
  21. return postcss(plugin(config)).process(input, {
  22. from: path,
  23. to: path,
  24. })
  25. }
  26. export function runWithSourceMaps(
  27. input,
  28. config,
  29. options = { map: { prev: map } },
  30. plugin = tailwind
  31. ) {
  32. let { currentTestName, testPath } = expect.getState()
  33. let path = `${testPath}?test=${Buffer.from(currentTestName).toString('base64')}`
  34. return postcss(plugin(config)).process(input, {
  35. from: path,
  36. to: path,
  37. ...options,
  38. })
  39. }
  40. export function quickPluginTest(pluginName, extendedConfig = {}) {
  41. return new Proxy(
  42. {},
  43. {
  44. get: (_, prop) => {
  45. return (...args) => {
  46. it(`should test the '${pluginName}' plugin`, () => {
  47. expect.assertions(3)
  48. // Ensure the plugin exists, this will help if you made a typo in the plugin name and
  49. // didn't realize it.
  50. expect(corePluginList).toContain(pluginName)
  51. let { safelist = [], corePlugins = [], ...rest } = extendedConfig || {}
  52. let config = {
  53. safelist: [{ pattern: /./g }, ...safelist], // Generate all known classes
  54. corePlugins: [pluginName, ...corePlugins], // Only load the plugin we're testing
  55. content: [], // Be explicit about the empty content, otherwise auto-detection will kick in
  56. ...rest,
  57. }
  58. let input = css`
  59. @tailwind base;
  60. @tailwind components;
  61. @tailwind utilities;
  62. `
  63. return run(input, config).then((result) => {
  64. let firstNewLine = result.css.indexOf('\n')
  65. let license = result.css.slice(0, firstNewLine)
  66. let css = result.css.slice(firstNewLine)
  67. // Ensure we have a license
  68. expect(license).toEqual(
  69. `/* ! tailwindcss v${pkg.version} | MIT License | https://tailwindcss.com */`
  70. )
  71. expect(css)[prop](...args)
  72. })
  73. })
  74. }
  75. },
  76. }
  77. )
  78. }
  79. export function quickVariantPluginTest(pluginName, extendedConfig = {}) {
  80. // Drop the _other_ variant related plugins
  81. for (let variant in variantPlugins) {
  82. if (variant !== pluginName) {
  83. variantPlugins[variant] = () => {}
  84. }
  85. }
  86. return new Proxy(
  87. {},
  88. {
  89. get: (_, prop) => {
  90. return (...args) => {
  91. it(`should test the '${pluginName}' plugin`, () => {
  92. expect.assertions(3)
  93. // Ensure the plugin exists, this will help if you made a typo in the plugin name and
  94. // didn't realize it.
  95. expect(Object.keys(variantPlugins)).toContain(pluginName)
  96. let context
  97. let config = {
  98. corePlugins: [],
  99. get content() {
  100. if (!context) return [] // Context is not ready yet
  101. return context
  102. .getVariants()
  103. .flatMap((variant) => {
  104. return variant.values.length > 0
  105. ? variant.values.map((value) => `${variant.name}-${value}`)
  106. : variant.name
  107. })
  108. .map((variant) => ({ raw: `${variant}:flex` }))
  109. },
  110. plugins: [
  111. function testPlugin({ addUtilities }) {
  112. addUtilities({
  113. '.flex': { display: 'flex' },
  114. })
  115. },
  116. ],
  117. ...extendedConfig,
  118. }
  119. context = createContext(resolveConfig(config))
  120. let input = css`
  121. @tailwind base;
  122. @tailwind components;
  123. @tailwind utilities;
  124. `
  125. return run(input, config).then((result) => {
  126. let firstNewLine = result.css.indexOf('\n')
  127. let license = result.css.slice(0, firstNewLine)
  128. let css = result.css.slice(firstNewLine)
  129. // Ensure we have a license
  130. expect(license).toEqual(
  131. `/* ! tailwindcss v${pkg.version} | MIT License | https://tailwindcss.com */`
  132. )
  133. expect(css)[prop](...args)
  134. })
  135. })
  136. }
  137. },
  138. }
  139. )
  140. }