-
Notifications
You must be signed in to change notification settings - Fork 424
[postcss-plugin] Add unit tests #1067
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
95ef1af
[postcss-plugin] Add unit tests
javascripter a73a2c9
[postcss-plugin] rename it() to test() for consistency in tests
javascripter 20592c7
[postcss-plugin] move jest config from package.json to jest.config.js…
javascripter 4d7ec46
[postcss-plugin] rename .babelrc.json to .babelrc.js in tests
javascripter ce048a6
[postcss-plugin] remove devDependencies already installed at root in…
javascripter 448f3a6
[postcss-plugin] remove @babel/preset-env config from tests file
javascripter 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
5 changes: 5 additions & 0 deletions
5
packages/@stylexjs/postcss-plugin/__tests__/__fixtures__/.babelrc.js
This file contains hidden or 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,5 @@ | ||
| module.exports = { | ||
| plugins: [ | ||
| ['@stylexjs/babel-plugin', { dev: false, runtimeInjection: false }], | ||
| ], | ||
| }; |
14 changes: 14 additions & 0 deletions
14
packages/@stylexjs/postcss-plugin/__tests__/__fixtures__/import-sources-object.js
This file contains hidden or 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,14 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import { css } from 'react-strict-dom'; | ||
|
|
||
| export const styles = css.create({ | ||
| object: { | ||
| backgroundColor: 'yellow', | ||
| }, | ||
| }); |
14 changes: 14 additions & 0 deletions
14
packages/@stylexjs/postcss-plugin/__tests__/__fixtures__/import-sources-string.js
This file contains hidden or 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,14 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import * as custom from 'custom'; | ||
|
|
||
| export const styles = custom.create({ | ||
| string: { | ||
| backgroundColor: 'blue', | ||
| }, | ||
| }); |
14 changes: 14 additions & 0 deletions
14
packages/@stylexjs/postcss-plugin/__tests__/__fixtures__/styles-second.js
This file contains hidden or 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,14 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import * as stylex from '@stylexjs/stylex'; | ||
|
|
||
| export const styles = stylex.create({ | ||
| second: { | ||
| backgroundColor: 'green', | ||
| }, | ||
| }); |
14 changes: 14 additions & 0 deletions
14
packages/@stylexjs/postcss-plugin/__tests__/__fixtures__/styles.js
This file contains hidden or 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,14 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import * as stylex from '@stylexjs/stylex'; | ||
|
|
||
| export const styles = stylex.create({ | ||
| container: { | ||
| backgroundColor: 'red', | ||
| }, | ||
| }); |
153 changes: 153 additions & 0 deletions
153
packages/@stylexjs/postcss-plugin/__tests__/index-test.js
This file contains hidden or 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,153 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const path = require('path'); | ||
| const postcss = require('postcss'); | ||
| const createPlugin = require('../src/plugin'); | ||
|
|
||
| describe('@stylexjs/postcss-plugin', () => { | ||
| const fixturesDir = path.resolve(__dirname, '__fixtures__'); | ||
|
|
||
| async function runStylexPostcss(options = {}, inputCSS = '@stylex;') { | ||
| // Create a new instance for each test as the plugin is stateful | ||
| const stylexPostcssPlugin = createPlugin(); | ||
|
|
||
| const plugin = stylexPostcssPlugin({ | ||
| cwd: fixturesDir, | ||
| include: ['**/*.js'], | ||
| babelConfig: { | ||
| configFile: path.join(fixturesDir, '.babelrc.js'), | ||
| }, | ||
| ...options, | ||
| }); | ||
|
|
||
| const processor = postcss([plugin]); | ||
| const result = await processor.process(inputCSS, { | ||
| from: path.join(fixturesDir, 'input.css'), | ||
| }); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| test('extracts CSS from StyleX files', async () => { | ||
| const result = await runStylexPostcss(); | ||
|
|
||
| expect(result.css).toMatchInlineSnapshot(` | ||
| ".x1u857p9{background-color:green} | ||
| .xrkmrrc{background-color:red}" | ||
| `); | ||
|
|
||
| // Check that messages contain dependency information | ||
| expect(result.messages.length).toBeGreaterThan(0); | ||
| expect(result.messages.some((m) => m.type === 'dir-dependency')).toBe(true); | ||
| }); | ||
|
|
||
| test('handles empty CSS input without @stylex rule', async () => { | ||
| const result = await runStylexPostcss({}, '/* No stylex rule here */'); | ||
|
|
||
| expect(result.css).toMatchInlineSnapshot('"/* No stylex rule here */"'); | ||
| expect(result.messages.length).toBe(0); | ||
| }); | ||
|
|
||
| test('supports CSS layers', async () => { | ||
| const result = await runStylexPostcss({ useCSSLayers: true }); | ||
|
|
||
| expect(result.css).toContain('@layer'); | ||
| expect(result.css).toMatchInlineSnapshot(` | ||
| " | ||
| @layer priority1; | ||
| @layer priority1{ | ||
| .x1u857p9{background-color:green} | ||
| .xrkmrrc{background-color:red} | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| test('handles exclude patterns', async () => { | ||
| const result = await runStylexPostcss({ | ||
| exclude: ['**/styles-second.js'], | ||
| }); | ||
|
|
||
| // Should not contain styles-second.js styles | ||
| expect(result.css).not.toContain('green'); | ||
|
|
||
| expect(result.css).toMatchInlineSnapshot( | ||
| '".xrkmrrc{background-color:red}"', | ||
| ); | ||
| }); | ||
|
|
||
| test('respects string syntax for importSources', async () => { | ||
| // Default importSources should not process any files | ||
| const defaultResult = await runStylexPostcss({ | ||
| include: ['**/import-sources-*.js'], | ||
| }); | ||
|
|
||
| expect(defaultResult.css).toBe(''); | ||
|
|
||
| // Custom importSources should process only import-sources-string.js | ||
| const customResult = await runStylexPostcss({ | ||
| include: ['**/import-sources-*.js'], | ||
| importSources: ['custom'], | ||
| babelConfig: { | ||
| babelrc: false, | ||
| plugins: [ | ||
| [ | ||
| '@stylexjs/babel-plugin', | ||
| { | ||
| dev: false, | ||
| runtimeInjection: false, | ||
| importSources: ['custom'], | ||
| }, | ||
| ], | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| expect(customResult.css).toMatchInlineSnapshot( | ||
| '".x1t391ir{background-color:blue}"', | ||
| ); | ||
| }); | ||
|
|
||
| test('supports object syntax for importSources', async () => { | ||
| const result = await runStylexPostcss({ | ||
| include: ['**/import-sources-object.js'], | ||
| importSources: [{ as: 'css', from: 'react-strict-dom' }], | ||
| babelConfig: { | ||
| babelrc: false, | ||
| plugins: [ | ||
| [ | ||
| '@stylexjs/babel-plugin', | ||
| { | ||
| dev: false, | ||
| runtimeInjection: false, | ||
| importSources: [{ as: 'css', from: 'react-strict-dom' }], | ||
| }, | ||
| ], | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| expect(result.css).toMatchInlineSnapshot( | ||
| '".x1cu41gw{background-color:yellow}"', | ||
| ); | ||
| }); | ||
|
|
||
| test('skips files that do not match include/exclude patterns', async () => { | ||
| const result = await runStylexPostcss({ | ||
| include: ['**/styles-second.js'], | ||
| }); | ||
|
|
||
| // Should contain styles-second.js styles but not styles.js | ||
| expect(result.css).not.toContain('red'); | ||
|
|
||
| expect(result.css).toMatchInlineSnapshot( | ||
| '".x1u857p9{background-color:green}"', | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,13 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| module.exports = { | ||
| testPathIgnorePatterns: ['/__fixtures__/'], | ||
| testEnvironment: 'node', | ||
| }; |
This file contains hidden or 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
This file contains hidden or 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
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.
This is where we re-create the plugin every time and reset internal state