|
| 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 | +}) |
0 commit comments