-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.test.js
More file actions
74 lines (63 loc) · 2.03 KB
/
plugin.test.js
File metadata and controls
74 lines (63 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import postcss from 'postcss';
import { describe, expect, it, vi } from 'vitest';
import {
loadFixture,
normalizeCSS,
postcssFixtureTests,
runtimeOnlyFixtureTests
} from '../../../test/scripts/fixture-utils.js';
import { postcssIfFunction } from '../src/index.js';
describe('postcss-if-function plugin', () => {
async function run(input, output, options = {}) {
const result = await postcss([postcssIfFunction(options)]).process(
input,
{
from: undefined
}
);
expect(normalizeCSS(result.css)).toBe(normalizeCSS(output));
expect(result.warnings()).toHaveLength(0);
}
// Generate tests for each shared fixture
for (const { fixture, description } of postcssFixtureTests) {
it(`should ${description}`, async () => {
const { input, expected } = loadFixture(fixture);
await run(input, expected);
});
}
// Runtime-only fixtures that cannot be transformed at build time
// PostCSS should preserve these unchanged
for (const { fixture, description } of runtimeOnlyFixtureTests) {
it(`should preserve ${description} unchanged`, async () => {
const { input } = loadFixture(fixture);
// For runtime-only features, PostCSS should preserve the original CSS
await run(input, input);
});
}
it('should work with logTransformations option', async () => {
const { input, expected } = loadFixture('basic-media');
// Spy on console.log
const logSpy = vi.spyOn(console, 'log');
await run(input, expected, { logTransformations: true });
expect(logSpy).toHaveBeenCalledWith(
'[postcss-if-function] Transformation statistics:'
);
expect(
logSpy.mock.calls.some((call) =>
call[0].includes('Total transformations: 1')
)
).toBe(true);
logSpy.mockRestore();
});
it('should handle malformed CSS gracefully', async () => {
const input = `
.broken {
color: if(media(invalid-query): blue; else: red);
}`;
// Should not throw an error, but may not transform properly
const result = await postcss([postcssIfFunction()]).process(input, {
from: undefined
});
expect(result.css).toBeDefined();
});
});