test.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const { execSync } = require('child_process')
  2. const os = require('os')
  3. const fs = require('fs-extra')
  4. const platformMap = {
  5. darwin: `./dist/tailwindcss-macos-${process.arch}`,
  6. linux: `./dist/tailwindcss-linux-${process.arch}`,
  7. win32: `.\\dist\\tailwindcss-windows-${process.arch}`,
  8. }
  9. function exec(args) {
  10. return execSync(`${platformMap[process.platform]} ${args}`).toString()
  11. }
  12. it('works', () => {
  13. let result = exec('--content tests/fixtures/basic.html')
  14. expect(result).toContain('.uppercase')
  15. expect(result).toContain('.\\[will-change\\:opacity\\]')
  16. expect(result).toContain('will-change: opacity')
  17. // Verify that no plugins are installed that modify the `[will-change:opacity]` class
  18. expect(result).not.toContain('backface-visibility: hidden')
  19. })
  20. it('supports first-party plugins', () => {
  21. let result = exec('--content tests/fixtures/plugins.html --config tests/fixtures/test.config.js')
  22. expect(result).toContain('.aspect-w-1')
  23. expect(result).toContain('.form-input')
  24. expect(result).toContain('.line-clamp-2')
  25. expect(result).toContain('.prose')
  26. expect(result).toContain('@container')
  27. expect(result).toContain('@md\\:bg-teal-600')
  28. })
  29. it('supports postcss config files', async () => {
  30. // We have to run this test outside of any place with node_modules for it to properly test this situation
  31. let result = await inIsolatedContext(() => {
  32. // Emulate the user adding their own postcss plugins
  33. execSync(`npm install postcss-will-change`)
  34. return exec('--content tests/fixtures/basic.html --postcss tests/fixtures/postcss.config.js')
  35. })
  36. expect(result).toContain('.uppercase')
  37. // Ensure the custom added postcss plugin is working
  38. expect(result).toContain('will-change: opacity')
  39. expect(result).toContain('backface-visibility: hidden')
  40. })
  41. /**
  42. * @template T
  43. * @param {() => T} fn
  44. * @returns {Promise<T>}
  45. */
  46. async function inIsolatedContext(fn) {
  47. // Create a new directory entirely outside of the package for the test
  48. let dest = `${os.tmpdir()}/tailwindcss-cli`
  49. // Recursively copy the dist and tests folders
  50. let dirs = ['dist', 'tests']
  51. await Promise.all(
  52. dirs.map((dir) =>
  53. fs.copy(`${__dirname}/../${dir}`, `${dest}/${dir}`, {
  54. overwrite: true,
  55. recursive: true,
  56. })
  57. )
  58. )
  59. // Change the working directory to the new directory
  60. process.chdir(dest)
  61. try {
  62. return await fn()
  63. } finally {
  64. // Change back to the original working directory
  65. process.chdir(__dirname)
  66. // Delete the new directory
  67. await fs.rm(dest, { recursive: true })
  68. }
  69. }