-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unitest mock css frame work
- Loading branch information
Showing
6 changed files
with
697 additions
and
6 deletions.
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
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
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,97 @@ | ||
import { describe, it, expect, vi } from 'vitest' | ||
import fs from 'fs' | ||
import { exec } from 'child_process' | ||
import readline from 'readline' | ||
import { | ||
installPackage, | ||
createTailwindConfig, | ||
createPostCSSConfig, | ||
initialize, | ||
} from './init-tailwind.js' | ||
|
||
vi.mock('fs') | ||
vi.mock('child_process') | ||
vi.mock('readline') | ||
|
||
describe('init-tailwind.js', () => { | ||
describe('installPackage', () => { | ||
it('should install package successfully', async () => { | ||
exec.mockImplementation((cmd, callback) => callback(null)) | ||
await expect( | ||
installPackage('nt-stylesheet'), | ||
).toStrictEqual(Promise.resolve()) | ||
expect(exec).toHaveBeenCalledWith( | ||
'npm install nt-stylesheet', | ||
expect.any(Function), | ||
) | ||
}) | ||
|
||
it('should fail to install package', async () => { | ||
const error = new Error('Installation failed') | ||
exec.mockImplementation((cmd, callback) => | ||
callback(error), | ||
) | ||
await expect( | ||
installPackage('nt-stylesheet'), | ||
).rejects.toThrow('Installation failed') | ||
}) | ||
}) | ||
|
||
describe('createTailwindConfig', () => { | ||
it('should create tailwind.config.js file', () => { | ||
fs.writeFile.mockImplementation( | ||
(path, content, callback) => callback(null), | ||
) | ||
createTailwindConfig() | ||
expect(fs.writeFile).toHaveBeenCalledWith( | ||
'tailwind.config.js', | ||
expect.stringContaining('module.exports = {'), | ||
expect.any(Function), | ||
) | ||
}) | ||
}) | ||
|
||
describe('createPostCSSConfig', () => { | ||
it('should create postcss.config.js file', () => { | ||
fs.writeFile.mockImplementation( | ||
(path, content, callback) => callback(null), | ||
) | ||
createPostCSSConfig() | ||
expect(fs.writeFile).toHaveBeenCalledWith( | ||
'postcss.config.js', | ||
expect.stringContaining('module.exports = {'), | ||
expect.any(Function), | ||
) | ||
}) | ||
}) | ||
|
||
describe('initialize', () => { | ||
it('should initialize successfully', async () => { | ||
exec.mockImplementation((cmd, callback) => callback(null)) | ||
readline.createInterface.mockReturnValue({ | ||
question: (query, callback) => callback('y'), | ||
close: vi.fn(), | ||
}) | ||
fs.writeFile.mockImplementation( | ||
(path, content, callback) => callback(null), | ||
) | ||
|
||
await initialize() | ||
|
||
expect(exec).toHaveBeenCalledWith( | ||
'npm install nt-stylesheet', | ||
expect.any(Function), | ||
) | ||
expect(fs.writeFile).toHaveBeenCalledWith( | ||
'tailwind.config.js', | ||
expect.stringContaining('module.exports = {'), | ||
expect.any(Function), | ||
) | ||
expect(fs.writeFile).toHaveBeenCalledWith( | ||
'postcss.config.js', | ||
expect.stringContaining('module.exports = {'), | ||
expect.any(Function), | ||
) | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
|
@@ -4,7 +4,8 @@ | |
"description": "Style sheet for the NT Design System", | ||
"packageManager": "[email protected]", | ||
"scripts": { | ||
"build": "pnpm vite build" | ||
"build": "pnpm vite build", | ||
"test": "npx vitest" | ||
}, | ||
"bin": { | ||
"nt-stylesheet": "bin/init-tailwind.js" | ||
|
@@ -40,7 +41,8 @@ | |
"nx": "^20.2.2", | ||
"typescript": "^5.7.2", | ||
"vite": "^6.0.3", | ||
"vite-plugin-sass-dts": "^1.3.29" | ||
"vite-plugin-sass-dts": "^1.3.29", | ||
"vitest": "^2.1.8" | ||
}, | ||
"dependencies": { | ||
"autoprefixer": "^10.4.20", | ||
|
Oops, something went wrong.