Skip to content

Commit aee98a3

Browse files
committed
Add more tests to the HTML plugin
1 parent 6b3e273 commit aee98a3

16 files changed

+2058
-135
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
"types": "pnpm tsc",
2929
"version-packages": "changeset version",
3030
"publish-packages": "turbo run compile lint test && changeset version && changeset publish",
31-
"watch": "pnpm compile && dotenv -- turbo run watch --filter=./programs/* --concurrency=5",
32-
"prepare": "husky"
31+
"watch": "pnpm compile && dotenv -- turbo run watch --filter=./programs/* --concurrency=5"
3332
},
3433
"devDependencies": {
3534
"@changesets/cli": "^2.28.1",

programs/cli/vitest.config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {defineConfig} from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
coverage: {
6+
exclude: [
7+
'**/messages.ts',
8+
'**/node_modules/**',
9+
'**/dist/**',
10+
'**/types/**',
11+
'**/*.d.ts',
12+
'**/*.test.ts',
13+
'**/*.spec.ts'
14+
]
15+
}
16+
}
17+
})

programs/create/vitest.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export default defineConfig({
2020
'**/*.d.ts',
2121
'**/*.test.ts',
2222
'**/*.spec.ts',
23-
'**/types/**'
23+
'**/types/**',
24+
'**/messages.ts'
2425
]
2526
}
2627
}

programs/develop/vitest.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export default defineConfig({
2323
'**/*.d.ts',
2424
'**/*.test.ts',
2525
'**/*.spec.ts',
26-
'**/types/**'
26+
'**/types/**',
27+
'**/messages.ts'
2728
]
2829
}
2930
}

programs/develop/webpack/plugin-extension/feature-html/__spec__/steps/add-assets-to-compilation.spec.ts

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from 'fs'
22
import * as path from 'path'
3+
import type {Compilation, Compiler} from '@rspack/core'
34
import {describe, it, expect, vi, beforeEach} from 'vitest'
45
import {AddAssetsToCompilation} from '../../steps/add-assets-to-compilation'
56
import * as utils from '../../html-lib/utils'
@@ -38,58 +39,61 @@ vi.mock('@rspack/core', () => {
3839
})
3940

4041
describe('AddAssetsToCompilation', () => {
41-
let compilation: any
42-
let compiler: any
43-
let emitAssetSpy: any
44-
let getAssetSpy: any
45-
let warningsPushSpy: any
46-
let existsSyncSpy: any
47-
let readFileSyncSpy: any
48-
let processAssetsCallback: any
42+
let compilation: Partial<Compilation>
43+
let compiler: Partial<Compiler>
44+
let emitAssetSpy: ReturnType<typeof vi.fn>
45+
let getAssetSpy: ReturnType<typeof vi.fn>
46+
let warningsPushSpy: ReturnType<typeof vi.fn>
47+
let existsSyncSpy: ReturnType<typeof vi.spyOn>
48+
let readFileSyncSpy: ReturnType<typeof vi.spyOn>
49+
let processAssetsCallback: () => void
4950

5051
beforeEach(() => {
5152
vi.clearAllMocks()
5253

5354
emitAssetSpy = vi.fn()
5455
getAssetSpy = vi.fn()
5556
warningsPushSpy = vi.fn()
56-
existsSyncSpy = vi.spyOn(fs, 'existsSync').mockReturnValue(true)
57+
existsSyncSpy = vi.spyOn(fs, 'existsSync').mockReturnValue(true) as any
5758
readFileSyncSpy = vi
5859
.spyOn(fs, 'readFileSync')
59-
.mockReturnValue(Buffer.from('file-content'))
60+
.mockReturnValue(Buffer.from('file-content')) as any
6061

6162
compilation = {
6263
hooks: {
6364
processAssets: {
64-
tap: (_opts: any, fn: any) => {
65-
processAssetsCallback = fn
65+
tap: (_opts: unknown, fn: (assets: Record<string, any>) => void) => {
66+
processAssetsCallback = () => fn({})
6667
}
67-
}
68-
},
68+
} as any
69+
} as any,
6970
getAsset: getAssetSpy,
7071
emitAsset: emitAssetSpy,
71-
warnings: {push: warningsPushSpy},
72+
warnings: {push: warningsPushSpy} as any,
7273
errors: []
7374
}
7475

7576
compiler = {
7677
hooks: {
7778
thisCompilation: {
78-
tap: (_name: string, fn: any) => {
79-
fn(compilation)
79+
tap: (
80+
_name: string,
81+
fn: (compilation: Compilation, params: any) => void
82+
) => {
83+
fn(compilation as Compilation, {})
8084
}
81-
}
82-
}
85+
} as any
86+
} as any
8387
}
8488

8589
// Default path mocking for all tests
86-
vi.spyOn(path, 'join').mockImplementation((...args) => {
90+
vi.spyOn(path, 'join').mockImplementation((...args: string[]) => {
8791
return args.filter(Boolean).join('/')
8892
})
89-
vi.spyOn(path.posix, 'join').mockImplementation((...args) => {
93+
vi.spyOn(path.posix, 'join').mockImplementation((...args: string[]) => {
9094
return args.filter(Boolean).join('/')
9195
})
92-
vi.spyOn(path, 'basename').mockImplementation((p) => {
96+
vi.spyOn(path, 'basename').mockImplementation((p: string) => {
9397
if (p === 'resource.html') return 'resource.html'
9498
if (p === 'nested/page.html') return 'page.html'
9599
return p.split('/').pop() || ''
@@ -107,7 +111,7 @@ describe('AddAssetsToCompilation', () => {
107111
if (name === 'resource.html') {
108112
return {
109113
source: {
110-
source: () => '<html><img src="foo.png"></html>'
114+
source: () => '<html><img src="extensionjs.png"></html>'
111115
}
112116
}
113117
}
@@ -117,18 +121,18 @@ describe('AddAssetsToCompilation', () => {
117121
vi.mocked(utils.getAssetsFromHtml).mockReturnValue({
118122
css: [],
119123
js: [],
120-
static: ['foo.png']
124+
static: ['extensionjs.png']
121125
})
122126
vi.mocked(utils.isFromIncludeList).mockReturnValue(false)
123127
vi.mocked(webpackUtils.shouldExclude).mockReturnValue(false)
124128

125-
plugin.apply(compiler)
126-
processAssetsCallback()
129+
plugin.apply(compiler as Compiler)
130+
if (processAssetsCallback) processAssetsCallback()
127131

128-
expect(emitAssetSpy).toHaveBeenCalled()
129-
expect(readFileSyncSpy).toHaveBeenCalledWith('foo.png')
132+
expect(emitAssetSpy).toHaveBeenCalledTimes(1)
133+
expect(readFileSyncSpy).toHaveBeenCalledWith('extensionjs.png')
130134
expect(emitAssetSpy).toHaveBeenCalledWith(
131-
'assets/foo.png',
135+
'assets/extensionjs.png',
132136
expect.any(Object)
133137
)
134138
expect(warningsPushSpy).not.toHaveBeenCalled()
@@ -160,7 +164,7 @@ describe('AddAssetsToCompilation', () => {
160164
vi.mocked(utils.isFromIncludeList).mockReturnValue(false)
161165
vi.mocked(webpackUtils.shouldExclude).mockReturnValue(false)
162166

163-
plugin.apply(compiler)
167+
plugin.apply(compiler as Compiler)
164168
processAssetsCallback()
165169

166170
expect(path.posix.join).toHaveBeenCalledWith(
@@ -203,8 +207,8 @@ describe('AddAssetsToCompilation', () => {
203207
vi.mocked(webpackUtils.shouldExclude).mockReturnValue(false)
204208
existsSyncSpy.mockReturnValue(false)
205209

206-
plugin.apply(compiler)
207-
processAssetsCallback()
210+
plugin.apply(compiler as Compiler)
211+
if (processAssetsCallback) processAssetsCallback()
208212

209213
expect(warningsPushSpy).toHaveBeenCalled()
210214
expect(emitAssetSpy).not.toHaveBeenCalled()
@@ -238,7 +242,7 @@ describe('AddAssetsToCompilation', () => {
238242
})
239243
vi.mocked(webpackUtils.shouldExclude).mockReturnValue(false)
240244

241-
plugin.apply(compiler)
245+
plugin.apply(compiler as Compiler)
242246
processAssetsCallback()
243247

244248
expect(warningsPushSpy).not.toHaveBeenCalled()

0 commit comments

Comments
 (0)