Skip to content

Commit 95aef39

Browse files
committed
Replace jest with vitest
1 parent 8b72280 commit 95aef39

File tree

4 files changed

+143
-150
lines changed

4 files changed

+143
-150
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"lint": "eslint src tests",
1414
"postpublish": "yarn clean",
1515
"prepublishOnly": "yarn lint && yarn test && yarn clean && yarn build",
16-
"test": "jest"
16+
"test": "vitest"
1717
},
1818
"dependencies": {
1919
"del": "^8.0.0"

tests/index.test.js

Lines changed: 0 additions & 149 deletions
This file was deleted.

tests/index.test.ts

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { ensureFile, pathExists, remove } from 'fs-extra'
2+
import { setTimeout } from 'node:timers/promises'
3+
import { replaceInFile } from 'replace-in-file'
4+
import { rollup, watch } from 'rollup'
5+
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
6+
import del, { type Options } from '../src/index.js'
7+
8+
process.chdir('tests/sample')
9+
10+
async function build(options?: Options) {
11+
await rollup({
12+
input: 'src/index.js',
13+
plugins: [
14+
del(options)
15+
]
16+
})
17+
}
18+
19+
beforeEach(async () => {
20+
await ensureFile('dist/app.js')
21+
await ensureFile('dist/app.css')
22+
})
23+
24+
afterEach(async () => {
25+
await remove('dist')
26+
})
27+
28+
describe('Targets', () => {
29+
test('Empty', async () => {
30+
await build()
31+
32+
expect(await pathExists('dist/app.js')).toBe(true)
33+
expect(await pathExists('dist/app.css')).toBe(true)
34+
})
35+
36+
test('Files', async () => {
37+
await build({
38+
targets: 'dist/*.{js,css}'
39+
})
40+
41+
expect(await pathExists('dist')).toBe(true)
42+
expect(await pathExists('dist/app.js')).toBe(false)
43+
expect(await pathExists('dist/app.css')).toBe(false)
44+
})
45+
46+
test('Folders', async () => {
47+
await build({
48+
targets: 'dist'
49+
})
50+
51+
expect(await pathExists('dist')).toBe(false)
52+
})
53+
})
54+
55+
describe('Options', () => {
56+
const log = vi.spyOn(console, 'log').mockImplementation(() => null)
57+
58+
afterEach(() => {
59+
vi.spyOn(console, 'log').mockClear()
60+
})
61+
62+
test('Verbose', async () => {
63+
await build({
64+
targets: 'dist',
65+
verbose: true
66+
})
67+
68+
expect(await pathExists('dist')).toBe(false)
69+
expect(log).toHaveBeenCalledTimes(2)
70+
expect(log).toHaveBeenCalledWith('Deleted files and folders: 1')
71+
expect(log).toHaveBeenCalledWith(`${__dirname}/sample/dist`)
72+
})
73+
74+
test('Verbose, no targets', async () => {
75+
await build({
76+
targets: 'build',
77+
verbose: true
78+
})
79+
80+
expect(await pathExists('dist')).toBe(true)
81+
expect(log).toHaveBeenCalledTimes(1)
82+
expect(log).toHaveBeenCalledWith('Deleted files and folders: 0')
83+
})
84+
85+
test('DryRun', async () => {
86+
await build({
87+
dryRun: true,
88+
targets: 'dist'
89+
})
90+
91+
expect(await pathExists('dist')).toBe(true)
92+
expect(log).toHaveBeenCalledTimes(2)
93+
expect(log).toHaveBeenCalledWith('Expected files and folders to be deleted: 1')
94+
expect(log).toHaveBeenCalledWith(`${__dirname}/sample/dist`)
95+
})
96+
97+
test('Run once', async () => {
98+
expect(await pathExists('dist/app.js')).toBe(true)
99+
100+
const watcher = watch({
101+
input: 'src/index.js',
102+
output: {
103+
dir: 'dist',
104+
format: 'es'
105+
},
106+
plugins: [
107+
del({
108+
runOnce: true,
109+
targets: 'dist'
110+
})
111+
]
112+
})
113+
114+
await setTimeout(100)
115+
116+
expect(await pathExists('dist/app.js')).toBe(false)
117+
expect(await pathExists('dist/index.js')).toBe(true)
118+
119+
await ensureFile('dist/app.js')
120+
121+
expect(await pathExists('dist/app.js')).toBe(true)
122+
123+
await replaceInFile({
124+
files: 'src/index.js',
125+
from: /hey/g,
126+
to: 'ho'
127+
})
128+
129+
await setTimeout(100)
130+
131+
expect(await pathExists('dist/app.js')).toBe(true)
132+
expect(await pathExists('dist/index.js')).toBe(true)
133+
134+
await watcher.close()
135+
136+
await replaceInFile({
137+
files: 'src/index.js',
138+
from: /ho/g,
139+
to: 'hey'
140+
})
141+
})
142+
})
File renamed without changes.

0 commit comments

Comments
 (0)