8000 test: add test for rspack (#496) · unjs/unplugin@2f86391 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f86391

Browse files
authored
test: add test for rspack (#496)
* test: add test for rspack Signed-off-by: ysknsid25 <kengo071225@gmail.com> * test: add test for rspack Signed-off-by: ysknsid25 <kengo071225@gmail.com> * test: add test for rspack Signed-off-by: ysknsid25 <kengo071225@gmail.com> --------- Signed-off-by: ysknsid25 <kengo071225@gmail.com>
1 parent f514bf8 commit 2f86391

File tree

4 files changed

+266
-1
lines changed

4 files changed

+266
-1
lines changed

src/rspack/context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export function createBuildContext(compiler: Compiler, compilation: Compilation,
1515
}
1616
},
1717
addWatchFile(file) {
18-
compilation.fileDependencies.add(resolve(process.cwd(), file))
18+
const cwd = process.cwd()
19+
compilation.fileDependencies.add(resolve(cwd, file))
1920
},
8000
2021
getWatchFiles() {
2122
return Array.from(compilation.fileDependencies)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Buffer } from 'node:buffer'
2+
import { describe, expect, it, vi } from 'vitest'
3+
import { createBuildContext, createContext } from '../../../src/rspack/context'
4+
5+
describe('createBuildContext', () => {
6+
it('getNativeBuildContext - should return expected', () => {
7+
const compiler = { name: 'testCompiler' }
8+
const compilation = { name: 'testCompilation' }
9+
const loaderContext = { name: 'testLoaderContext' }
10+
11+
const buildContext = createBuildContext(compiler as any, compilation as any, loaderContext as any)
12+
13+
expect(buildContext.getNativeBuildContext!()).toEqual({
14+
framework: 'rspack',
15+
compiler,
16+
compilation,
17+
loaderContext,
18+
})
19+
})
20+
21+
it('emitFile - should return expected', () => {
22+
const emitAssetMock = vi.fn()
23+
const RawSourceMock = vi.fn(content => ({ content }))
24+
const compiler = { name: 'testCompiler' }
25+
const compilation = {
26+
name: 'testCompilation',
27+
compiler: {
28+
webpack: {
29+
sources: {
30+
RawSource: RawSourceMock,
31+
},
32+
},
33+
},
34+
emitAsset: emitAssetMock,
35+
}
36+
const loaderContext = { name: 'testLoaderContext' }
37+
38+
const buildContext = createBuildContext(compiler as any, compilation as any, loaderContext as any)
39+
40+
buildContext.emitFile({
41+
fileName: 'testFile.js',
42+
source: 'testSource',
43+
} as any)
44+
expect(emitAssetMock).toHaveBeenCalledWith(
45+
'testFile.js',
46+
{
47+
content: 'testSource',
48+
},
49+
)
50+
emitAssetMock.mockClear()
51+
52+
buildContext.emitFile({
53+
name: 'testFile.js',
54+
source: Buffer.from('testBufferSource'),
55+
} as any)
56+
expect(emitAssetMock).toHaveBeenCalledWith(
57+
'testFile.js',
58+
{
59+
content: Buffer.from('testBufferSource'),
60+
},
61+
)
62+
emitAssetMock.mockClear()
63+
})
64+
65+
it('createContext - should return expected', () => {
66+
const loaderContext = {
67+
emitError: vi.fn(),
68+
emitWarning: vi.fn(),
69+
}
70+
71+
const context = createContext(loaderContext as any)
72+
73+
context.error('testError')
74+
expect(loaderContext.emitError).toHaveBeenCalledWith(new Error('testError'))
75+
76+
context.error({ message: 'testError' })
77+
expect(loaderContext.emitError).toHaveBeenCalledWith(new Error('testError'))
78+
79+
context.warn('testWarning')
80+
expect(loaderContext.emitWarning).toHaveBeenCalledWith(new Error('testWarning'))
81+
})
82+
})
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
import load from '../../../../src/rspack/loaders/load'
3+
4+
describe('load', () => {
5+
it('should call callback with source and map when plugin.load is not defined', async () => {
6+
const asyncMock = vi.fn()
7+
const query = { plugin: {} }
8+
await load.call({ async: () => asyncMock, query } as any, 'source', 'map')
9+
10+
expect(asyncMock).toHaveBeenCalledWith(null, 'source', 'map')
11+
})
12+
13+
it('should call callback with transformed code and map when handler returns an object', async () => {
14+
const asyncMock = vi.fn()
15+
const handlerMock = vi.fn().mockResolvedValue({ code: 'transformedCode', map: 'transformedMap' })
16+
const query = {
17+
plugin: {
18+
load: handlerMock,
19+
},
20+
}
21+
22+
await load.call(
23+
{
24+
async: () => asyncMock,
25+
query,
26+
resource: 'resourceId',
27+
} as any,
28+
'source',
29+
'map',
30+
)
31+
32+
expect(handlerMock).toHaveBeenCalled()
33+
expect(asyncMock).toHaveBeenCalledWith(null, 'transformedCode', 'transformedMap')
34+
})
35+
36+
it('should call callback with transformed code when handler returns a string', async () => {
37+
const asyncMock = vi.fn()
38+
const handlerMock = vi.fn().mockResolvedValue('transformedCode')
39+
const query = {
40+
plugin: {
41+
load: handlerMock,
42+
},
43+
}
44+
45+
await load.call(
46+
{
47+
async: () => asyncMock,
48+
query,
49+
resource: 'resourceId',
50+
} as any,
51+
'source',
52+
'map',
53+
)
54+
55+
expect(handlerMock).toHaveBeenCalled()
56+
expect(asyncMock).toHaveBeenCalledWith(null, 'transformedCode', 'map')
57+
})
58+
59+
it('should call callback with source and map when handler returns null', async () => {
60+
const asyncMock = vi.fn()
61+
const handlerMock = vi.fn().mockResolvedValue(null)
62+
const query = {
63+
plugin: {
64+
load: handlerMock,
65+
},
66+
}
67+
68+
await load.call(
69+
{
70+
async: () => asyncMock,
71+
query,
72+
resource: 'resourceId',
73+
} as any,
74+
'source',
75+
'map',
76+
)
77+
78+
expect(handlerMock).toHaveBeenCalled()
79+
expect(asyncMock).toHaveBeenCalledWith(null, 'source', 'map')
80+
})
81+
82+
it('should call callback with source and map when handler returns object', async () => {
83+
const asyncMock = vi.fn()
84+
const handlerMock = vi.fn().mockResolvedValue({
85+
code: 'code',
86+
map: 'resmap',
87+
})
88+
const query = {
89+
plugin: {
90+
load: handlerMock,
91+
},
92+
}
93+
94+
await load.call(
95+
{
96+
async: () => asyncMock,
97+
query,
98+
resource: 'resourceId',
99+
} as any,
100+
'source',
101+
'map',
102+
)
103+
104+
expect(handlerMock).toHaveBeenCalled()
105+
expect(asyncMock).toHaveBeenCalledWith(null, 'code', 'resmap')
106+
})
107+
})
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
import transform from '../../../../src/rspack/loaders/transform'
3+
4+
describe('transform', () => {
5+
it('should call callback with source and map if plugin.transform is not defined', async () => {
6+
const mockCallback = vi.fn()
7+
const mockLoaderContext = {
8+
async: () => mockCallback,
9+
query: {},
10+
} as any
11+
12+
const source = 'test source'
13+
const map F438 = 'test map'
14+
15+
await transform.call(mockLoaderContext, source, map)
16+
17+
expect(mockCallback).toHaveBeenCalledWith(null, source, map)
18+
})
19+
20+
it('should call callback with an error if handler throws an error', async () => {
21+
const mockCallback = vi.fn()
22+
const mockLoaderContext = {
23+
async: () => mockCallback,
24+
query: {
25+
plugin: {
26+
transform: {
27+
handler: vi.fn().mockRejectedValue(new Error('Handler error')),
28+
filter: vi.fn().mockReturnValue(true),
29+
},
30+
},
31+
},
32+
resource: 'test resource',
33+
} as any
34+
35+
const source = 'test source'
36+
const map = 'test map'
37+
38+
vi.mock('../../../../src/utils/filter', () => ({
39+
normalizeObjectHook: vi.fn(() => ({ handler: vi.fn().mockRejectedValue(new Error('Handler error')), filter: vi.fn().mockReturnValue(true) })),
40+
}))
41+
42+
await transform.call(mockLoaderContext, source, map)
43+
44+
expect(mockCallback).toHaveBeenCalledWith(expect.any(Error))
45+
expect(mockCallback.mock.calls[0][0].message).toBe('Handler error')
46+
})
47+
48+
it('should call callback with an error if handler throws string', async () => {
49+
const mockCallback = vi.fn()
50+
const mockLoaderContext = {
51+
async: () => mockCallback,
52+
query: {
53+
plugin: {
54+
transform: {
55+
handler: vi.fn().mockRejectedValue('Handler error'),
56+
filter: vi.fn().mockReturnValue(true),
57+
},
58+
},
59+
},
60+
resource: 'test resource',
61+
} as any
62+
63+
const source = 'test source'
64+
const map = 'test map'
65+
66+
vi.mock('../../../../src/utils/filter', () => ({
67+
normalizeObjectHook: vi.fn(() => ({ handler: vi.fn().mockRejectedValue(new Error('Handler error')), filter: vi.fn().mockReturnValue(true) })),
68+
}))
69+
70+
await transform.call(mockLoaderContext, source, map)
71+
72+
expect(mockCallback).toHaveBeenCalledWith(expect.any(Error))
73+
expect(mockCallback.mock.calls[0][0].message).toBe('Handler error')
74+
})
75+
})

0 commit comments

Comments
 (0)
0