-
Notifications
You must be signed in to change notification settings - Fork 27.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test(unit): add cases for cssnano simple #46862
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
test/unit/cssnano-simple/cssnano-preset-simple/issue-1.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import cssnanoPresetSimple from 'next/src/bundles/cssnano-simple/cssnano-preset-simple' | ||
|
||
// https://github.com/Timer/cssnano-preset-simple/issues/1 | ||
describe('https://github.com/Timer/cssnano-preset-simple/issues/1', () => { | ||
test('evaluates without error', () => { | ||
cssnanoPresetSimple() | ||
}) | ||
}) |
40 changes: 40 additions & 0 deletions
40
test/unit/cssnano-simple/cssnano-preset-simple/plugin-config.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import postcss from 'postcss' | ||
|
||
import mod from './plugin' | ||
import css from '../noop-template' | ||
|
||
describe('accepts plugin configuration', () => { | ||
test('should not remove all comments', async () => { | ||
const input = css` | ||
p { | ||
/*! heading */ | ||
color: yellow; | ||
} | ||
` | ||
|
||
const res = await postcss([mod({ discardComments: {} })]).process(input, { | ||
from: 'input.css', | ||
to: 'output.css', | ||
}) | ||
|
||
expect(res.css).not.toBe('p{color:#ff0}') | ||
}) | ||
|
||
test('should remove all comments', async () => { | ||
const input = css` | ||
p { | ||
/*! heading */ | ||
color: yellow; | ||
} | ||
` | ||
|
||
const res = await postcss([ | ||
mod({ discardComments: { removeAll: true } }), | ||
]).process(input, { | ||
from: 'input.css', | ||
to: 'output.css', | ||
}) | ||
|
||
expect(res.css).toBe('p{color:#ff0}') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// https://github.com/Timer/cssnano-preset-simple/blob/master/test/plugin.js | ||
import postcss from 'postcss' | ||
import type { PluginCreator } from 'postcss' | ||
|
||
// Since the cssnano-preset-simple.js will be bundled into cssnano-simple | ||
// during pre-compilation, we need to test against the source file directly | ||
import cssnanoPresetSimple from 'next/src/bundles/cssnano-simple/cssnano-preset-simple' | ||
|
||
const cssnanoPlugin = (options = {}) => { | ||
const plugins: any[] = [] | ||
const nanoPlugins = cssnanoPresetSimple(options).plugins | ||
for (const nanoPlugin of nanoPlugins) { | ||
if (Array.isArray(nanoPlugin)) { | ||
let [processor, opts] = nanoPlugin | ||
processor = processor.default || processor | ||
if ( | ||
typeof opts === 'undefined' || | ||
(typeof opts === 'object' && !opts.exclude) || | ||
(typeof opts === 'boolean' && opts === true) | ||
) { | ||
plugins.push(processor(opts)) | ||
} | ||
} else { | ||
plugins.push(nanoPlugin) | ||
} | ||
} | ||
return postcss(plugins) | ||
} | ||
|
||
cssnanoPlugin.postcss = true | ||
export default cssnanoPlugin as PluginCreator<any> |
24 changes: 24 additions & 0 deletions
24
test/unit/cssnano-simple/cssnano-preset-simple/property-sorting.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import postcss from 'postcss' | ||
import mod from './plugin' | ||
import css from '../noop-template' | ||
|
||
describe('property sorting', () => { | ||
test('should result in correct border width', async () => { | ||
const input = css` | ||
p { | ||
border: 1px solid var(--test); | ||
border-radius: var(--test); | ||
border-width: 0; | ||
} | ||
` | ||
|
||
const res = await postcss([mod]).process(input, { | ||
from: 'input.css', | ||
to: 'output.css', | ||
}) | ||
|
||
expect(res.css).toMatchInlineSnapshot( | ||
`"p{border-width:1px;border-radius:var(--test);border:0 solid var(--test)}"` | ||
) | ||
}) | ||
}) |
19 changes: 19 additions & 0 deletions
19
test/unit/cssnano-simple/cssnano-preset-simple/with-escaped-selector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import postcss from 'postcss' | ||
import mod from './plugin' | ||
|
||
describe('with escaped selector', () => { | ||
test('should not misplace the backslash', async () => { | ||
const input = ` | ||
.foo\\,2 { | ||
background: blue; | ||
} | ||
` | ||
|
||
const res = await postcss([mod]).process(input, { | ||
from: 'input.css', | ||
to: 'output.css', | ||
}) | ||
|
||
expect(res.css).toMatchInlineSnapshot(`".foo\\\\,2{background:blue}"`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import postcss from 'postcss' | ||
|
||
import mod from 'next/src/bundles/cssnano-simple/index' | ||
import css from '../noop-template' | ||
|
||
describe('basic test', () => { | ||
test('should minify css', async () => { | ||
const input = css` | ||
p { | ||
color: yellow; | ||
} | ||
` | ||
|
||
const res = await postcss([mod()]).process(input, { | ||
from: 'input.css', | ||
to: 'output.css', | ||
}) | ||
|
||
expect(res.css).toBe('p{color:#ff0}') | ||
}) | ||
}) |
71 changes: 71 additions & 0 deletions
71
test/unit/cssnano-simple/cssnano-simple/exclude-all.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import postcss from 'postcss' | ||
|
||
import mod from 'next/src/bundles/cssnano-simple/index' | ||
import css from '../noop-template' | ||
|
||
describe('exclude all test', () => { | ||
test('should not transform css', async () => { | ||
const input = css` | ||
p { | ||
/* test */ | ||
color: yellow; | ||
} | ||
` | ||
|
||
const res = await postcss([mod({ excludeAll: true })]).process(input, { | ||
from: 'input.css', | ||
to: 'output.css', | ||
}) | ||
|
||
expect(res.css).toBe(input) | ||
}) | ||
|
||
test('should strip comments and spaces from css', async () => { | ||
const input = css` | ||
p { | ||
/* test */ | ||
color: yellow; | ||
} | ||
.empty { | ||
} | ||
` | ||
|
||
const res = await postcss([ | ||
mod({ | ||
excludeAll: true, | ||
discardComments: true, | ||
normalizeWhitespace: { exclude: false }, | ||
}), | ||
]).process(input, { | ||
from: 'input.css', | ||
to: 'output.css', | ||
}) | ||
|
||
expect(res.css).toMatchInlineSnapshot(`"p{color:yellow}.empty{}"`) | ||
}) | ||
|
||
test('should enable rule with empty object', async () => { | ||
const input = css` | ||
p { | ||
/* test */ | ||
color: yellow; | ||
} | ||
.empty { | ||
} | ||
` | ||
|
||
const res = await postcss([ | ||
mod({ | ||
excludeAll: true, | ||
discardComments: true, | ||
normalizeWhitespace: { exclude: false }, | ||
discardEmpty: {}, | ||
}), | ||
]).process(input, { | ||
from: 'input.css', | ||
to: 'output.css', | ||
}) | ||
|
||
expect(res.css).toMatchInlineSnapshot(`"p{color:yellow}"`) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I add the
postcss
to thedevDependencies
of the workspace root, as the unit test cases requirepostcss
.