Skip to content

Commit

Permalink
test: test case for compose/from path resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjin committed Jul 25, 2021
1 parent 66463ff commit a0df1f8
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion packages/vite/src/node/__tests__/plugins/css.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { cssUrlRE } from '../../plugins/css'
import { cssUrlRE, cssPlugin } from '../../plugins/css'
import { resolveConfig } from '../../config'
import fs from 'fs'
import path from 'path'

describe('search css url function', () => {
test('some spaces before it', () => {
Expand Down Expand Up @@ -41,3 +44,71 @@ describe('search css url function', () => {
).toBe(true)
})
})

describe('css path resolutions', () => {
const mockedProjectPath = '/foo/bar/project'
const mockedBarCssRelativePath = '/css/bar.module.css'
const mockedFooCssRelativePath = '/css/foo.module.css'

test('cssmodule compose/from path resolutions', async () => {
const config = await resolveConfig(
{
resolve: {
alias: [
{
find: '@',
replacement: mockedProjectPath
}
]
}
},
'serve'
)

const { transform, buildStart } = cssPlugin(config)

await buildStart.call({})

const mockFs = jest
.spyOn(fs, 'readFile')
// @ts-ignore jest.spyOn not recognize overrided `fs.readFile` definition.
.mockImplementationOnce((p, encoding, callback) => {
expect(p).toBe(path.join(mockedProjectPath, mockedBarCssRelativePath))
expect(encoding).toBe('utf-8')
callback(
null,
Buffer.from(`
.bar {
display: block;
background: #f0f;
}
`)
)
})

const { code } = await transform.call(
{
addWatchFile() {
return
}
},
`
.foo {
composes: bar from '@${mockedBarCssRelativePath}';
}
`,
path.join(mockedProjectPath, mockedFooCssRelativePath)
)

expect(code).toBe(`
._bar_soicv_2 {
display: block;
background: #f0f;
}
._foo_ede2y_2 {
}
`)

mockFs.mockReset()
})
})

0 comments on commit a0df1f8

Please sign in to comment.