Skip to content
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

feat: Support SASS #52

Merged
merged 23 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
186147d
feat: (In Progress) Support SASS
ntt261298 Apr 16, 2022
e52d4a1
refactor: Remove jest preview configure in setupTests
ntt261298 Apr 16, 2022
6108ec8
refactor: Add partial scss style
ntt261298 Apr 16, 2022
f515658
fix: Move config jest-preview to setupFilesAfterEnv
ntt261298 Apr 17, 2022
76e7d3f
fix: Use fs.existsSync to check if sassLoadPathsConfigPath exists
ntt261298 Apr 17, 2022
c8b853d
fix: Move sass compile out of of run time phase
ntt261298 Apr 17, 2022
290b8e4
fix: Update code style for sass configure
ntt261298 Apr 17, 2022
0571ef6
feat: Add example for pre-configured sass file issue
ntt261298 Apr 17, 2022
0ab3b65
chore: Add global scss style to main app
nvh95 Apr 17, 2022
eca8a24
Merge branch 'main' into sass-support
ntt261298 Apr 18, 2022
54e3428
chore: Update partial name for global scss
nvh95 Apr 18, 2022
99de46e
chore: use sass.compile instead of sass.compileString
nvh95 Apr 18, 2022
0a3057b
Merge branch 'sass-support' of github.com:nvh95/jest-preview-dom into…
ntt261298 Apr 18, 2022
32862e0
feat: Use CLI to transform sass in configure.ts, compile to transform…
ntt261298 Apr 18, 2022
0b2d56e
refactor: Update vite-react example to support global scss
ntt261298 Apr 18, 2022
0a4aae1
docs: Update docs for sass
nvh95 Apr 19, 2022
e7668b5
fix: Correct css cache filenames
ntt261298 Apr 19, 2022
4761899
refactor: Use filename as the fallback for scss transformer
ntt261298 Apr 19, 2022
baab8a5
fix: Add createCacheFolderIfNeeded util, add comment to clarify exec …
ntt261298 Apr 19, 2022
1d4892c
refactor: Update demo, vite example
ntt261298 Apr 19, 2022
8225289
Merge branch 'main' into sass-support
nvh95 Apr 19, 2022
9d44c67
chore: release 0.1.5-alpha.0
nvh95 Apr 19, 2022
74d43d2
Merge branch 'main' into sass-support
nvh95 Apr 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.1.5

- Support non-english characters and responsive design.
- Support Sass.

# 0.1.4

## Fixes
Expand All @@ -20,6 +25,7 @@
## Chores

- Export `debug` as named also.
- Handle CSS Modules asynchronously.

# 0.1.2

Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ When writing tests using Jest, we usually debug by reading the HTML code. Someti
- ✅ [Styled-components](https://styled-components.com/)
- ✅ [External CSS](#4-optional-configure-external-css)
- ✅ [CSS Modules](https://github.com/css-modules/css-modules)
- 🚧 [Sass](https://sass-lang.com/)
- [Sass](https://sass-lang.com/)
- 🌄 Support viewing images.

## How to use `jest-preview` in 2 lines of code
Expand Down Expand Up @@ -77,13 +77,15 @@ pnpm install --dev jest-preview

### 2. Configure jest's transform to transform CSS and files

`jest-preview` comes with pre-configured transformations to intercept CSS and files. This is a recommended way to configure. However, you can configure it yourself using exported transform functions as well. See [Advanced configurations](#advanced-configurations) for more.
`jest-preview` comes with pre-configured transformations to handle CSS and files. This is a recommended way to configure. However, you can configure it yourself using exported transform functions as well. See [Advanced configurations](#advanced-configurations) for more.

If you use [Sass](https://sass-lang.com/) in your project, make sure [sass](https://www.npmjs.com/package/sass) is already installed. Note that [Node Sass](https://www.npmjs.com/package/node-sass) and [LibSass](https://sass-lang.com/libsass) are [not supported](https://sass-lang.com/blog/libsass-is-deprecated).

Update `jest.config.js`:

```js
transform: {
"^.+\\.css$": "jest-preview/transforms/css",
"^.+\\.(css|scss|sass)$": "jest-preview/transforms/css",
"^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)": "jest-preview/transforms/file",
}
```
Expand Down Expand Up @@ -123,7 +125,8 @@ import { jestPreviewConfigure } from 'jest-preview';
jestPreviewConfigure({
externalCss: [
'demo/global.css',
'node_modules/@your-design-system/css/dist/index.min.css',
'demo/global.scss', // Sass
'node_modules/@your-design-system/css/dist/index.min.css', // css from node_modules
'node_modules/bootstrap/dist/css/bootstrap.min.css',
],
});
Expand Down
5 changes: 5 additions & 0 deletions demo/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styled from 'styled-components';
import logo2 from './assets/images/logo.svg';
import './App.css';
import './assets/css/App.css';
import './assets/_scss/style.scss';
import { cssModule } from './style.module.css';

function App() {
Expand All @@ -19,6 +20,10 @@ function App() {
This text is styled by global css which is not imported to App.tsx
</p>
<p className={cssModule}>This text is styled by CSS Modules</p>
<p className="global-configured-sass">
This text is styled by global configured SASS
</p>
<p className="imported-sass">This text is styled by imported SASS</p>
<p>
<button
data-testid="increase"
Expand Down
7 changes: 7 additions & 0 deletions demo/assets/_scss/global-style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import 'partials/partial-global-style'; // Example using @import

header {
.global-configured-sass {
color: yellow;
}
}
5 changes: 5 additions & 0 deletions demo/assets/_scss/partials/_partial-global-style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
header {
.global-configured-sass {
text-transform: uppercase;
}
}
5 changes: 5 additions & 0 deletions demo/assets/_scss/partials/_partial-style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
header {
.imported-sass {
text-transform: uppercase;
}
}
7 changes: 7 additions & 0 deletions demo/assets/_scss/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@use 'partials/partial-style'; // Example using @use

header {
.imported-sass {
color: pink;
}
}
1 change: 1 addition & 0 deletions demo/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import './global.css';
import './assets/_scss/global-style.scss';
import App from './App';

ReactDOM.render(
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-react/config/jest/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import '@testing-library/jest-dom/extend-expect';
import { jestPreviewConfigure } from 'jest-preview';

jestPreviewConfigure({
externalCss: ['src/index.css'],
externalCss: ['src/index.css', 'src/assets/_scss/global-style.scss'],
});

window.matchMedia = (query) => ({
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-react/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
modulePaths: ['<rootDir>/src'],
transform: {
'^.+\\.(ts|js|tsx|jsx)$': '@swc/jest',
'^.+\\.css$': 'jest-preview/transforms/css',
'^.+\\.(css|scss|sass)$': 'jest-preview/transforms/css',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is to remind not forget to update README.md on CSS transform

'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)':
'jest-preview/transforms/file',
},
Expand Down
81 changes: 81 additions & 0 deletions examples/vite-react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/vite-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"jest-watch-typeahead": "^1.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.6.0",
"sass": "^1.50.0",
"typescript": "^4.5.4",
"vite": "^2.8.0"
}
Expand Down
6 changes: 5 additions & 1 deletion examples/vite-react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import logo2 from './assets/images/logo.svg';
import './App.css';
import './assets/css/App.css';

import './assets/_scss/style.scss';
import styles from './style.module.css';

function App() {
Expand All @@ -18,7 +19,10 @@ function App() {
<p>Vite Example</p>
<StyledText>This text is styled by styled-components</StyledText>
<p className={styles.green}>This text is styled by CSS Modules</p>

<p className="global-configured-sass">
This text is styled by global configured SASS
</p>
<p className="imported-sass">This text is styled by imported SASS</p>
<p>
<button
data-testid="increase"
Expand Down
7 changes: 7 additions & 0 deletions examples/vite-react/src/assets/_scss/global-style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import 'partials/partial-global-style'; // Example using @import

header {
.global-configured-sass {
color: yellow;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
header {
.global-configured-sass {
text-transform: uppercase;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
header {
.imported-sass {
text-transform: uppercase;
}
}
7 changes: 7 additions & 0 deletions examples/vite-react/src/assets/_scss/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@use 'partials/partial-style'; // Example using @use

header {
.imported-sass {
color: pink;
}
}
5 changes: 3 additions & 2 deletions examples/vite-react/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom';
// TODO: Need an option for user to manually input css outside tested components
import './index.css';
import App from './App';

import './index.css';
import './assets/_scss/global-style.scss';

ReactDOM.render(
<React.StrictMode>
<App />
Expand Down
Loading