tailwindcss/tests/normalize-config.test.js
Adam Wathan 42136e94ce
Run test suite against both engines (#10373)
* Run test suite against both engines

* make eslint happy

* only run `stable` tests on Node 12

* use normal expectation instead of snapshot file

When we run the tests only against `stable` (for node 12), then the
snapshots exists for the `Oxide` build. They are marked as `obsolete`
and will cause the `npm run test` script to fail. Sadly.

Inlined them for now, but ideally we make those tests more blackbox-y so
that we test that we get source maps and that we can map the sourcemap
back to the input files (without looking at the actual annotations).

* properly indent inline css

Co-authored-by: Adam Wathan <4323180+adamwathan@users.noreply.github.com>
Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
2023-01-20 18:45:04 +01:00

153 lines
4.2 KiB
JavaScript

import { normalizeConfig } from '../src/util/normalizeConfig'
import resolveConfig from '../src/public/resolve-config'
import log from '../src/util/log'
import { crosscheck, run, css } from './util/run'
crosscheck(({ stable, oxide }) => {
it.each`
config
${{ purge: [{ raw: 'text-center' }] }}
${{ purge: { content: [{ raw: 'text-center' }] } }}
${{ content: { content: [{ raw: 'text-center' }] } }}
`('should normalize content $config', ({ config }) => {
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.text-center {
text-align: center;
}
`)
})
})
it.each`
config
${{ purge: { safelist: ['text-center'] } }}
${{ purge: { options: { safelist: ['text-center'] } } }}
${{ content: { safelist: ['text-center'] } }}
`('should normalize safelist $config', ({ config }) => {
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.text-center {
text-align: center;
}
`)
})
})
oxide.test.todo('should normalize extractors')
stable.test.each`
config
${{ content: [{ raw: 'text-center' }], purge: { extract: () => ['font-bold'] } }}
${{ content: [{ raw: 'text-center' }], purge: { extract: { DEFAULT: () => ['font-bold'] } } }}
${{
content: [{ raw: 'text-center' }],
purge: { options: { defaultExtractor: () => ['font-bold'] } },
}}
${{
content: [{ raw: 'text-center' }],
purge: { options: { extractors: [{ extractor: () => ['font-bold'], extensions: ['html'] }] } },
}}
${{ content: [{ raw: 'text-center' }], purge: { extract: { html: () => ['font-bold'] } } }}
`('should normalize extractors $config', ({ config }) => {
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.font-bold {
font-weight: 700;
}
`)
})
})
oxide.test.todo('should still be possible to use the "old" v2 config')
stable.test('should still be possible to use the "old" v2 config', () => {
let config = {
purge: {
content: [
{ raw: 'text-svelte', extension: 'svelte' },
{ raw: '# My Big Heading', extension: 'md' },
],
options: {
defaultExtractor(content) {
return content.split(' ').concat(['font-bold'])
},
},
extract: {
svelte(content) {
return content.replace('svelte', 'center').split(' ')
},
},
transform: {
md() {
return 'text-4xl'
},
},
},
theme: {
extends: {},
},
variants: {
extends: {},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.text-center {
text-align: center;
}
.text-4xl {
font-size: 2.25rem;
line-height: 2.5rem;
}
.font-bold {
font-weight: 700;
}
`)
})
})
it('should keep content files with globs', () => {
let config = {
content: ['./example-folder/**/*.{html,js}'],
}
expect(normalizeConfig(resolveConfig(config)).content).toEqual({
files: ['./example-folder/**/*.{html,js}'],
relative: false,
extract: {},
transform: {},
})
})
it('should warn when we detect invalid globs with incorrect brace expansion', () => {
let spy = jest.spyOn(log, 'warn')
let config = {
content: [
'./{example-folder}/**/*.{html,js}',
'./{example-folder}/**/*.{html}',
'./example-folder/**/*.{html}',
],
}
// No rewrite happens
expect(normalizeConfig(resolveConfig(config)).content).toEqual({
files: [
'./{example-folder}/**/*.{html,js}',
'./{example-folder}/**/*.{html}',
'./example-folder/**/*.{html}',
],
relative: false,
extract: {},
transform: {},
})
// But a warning should happen
expect(spy).toHaveBeenCalledTimes(2)
expect(spy.mock.calls.map((x) => x[0])).toEqual(['invalid-glob-braces', 'invalid-glob-braces'])
spy.mockRestore()
})
})