Skip to content

Commit 916fdb5

Browse files
committed
test: Vite - add config tests
1 parent 39f6666 commit 916fdb5

File tree

7 files changed

+75
-2
lines changed

7 files changed

+75
-2
lines changed

test/package/vite/case-config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* eslint-disable @typescript-eslint/no-require-imports */
2+
const { defineConfig } = require('vite');
3+
const webpackStatsPlugin = require('rollup-plugin-webpack-stats');
4+
5+
module.exports = defineConfig({
6+
build: {
7+
rollupOptions: {
8+
output: {
9+
assetFileNames: 'assets/[name][extname]',
10+
chunkFileNames: 'assets/[name].js',
11+
entryFileNames: 'assets/[name].js',
12+
},
13+
},
14+
},
15+
plugins: [webpackStatsPlugin()],
16+
});

test/package/vite/case-config.mjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { defineConfig } from 'vite';
2+
import webpackStats from 'rollup-plugin-webpack-stats';
3+
4+
export default defineConfig({
5+
build: {
6+
rollupOptions: {
7+
output: {
8+
assetFileNames: 'assets/[name][extname]',
9+
chunkFileNames: 'assets/[name].js',
10+
entryFileNames: 'assets/[name].js',
11+
},
12+
},
13+
},
14+
plugins: [webpackStats()],
15+
});

test/package/vite/case-config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { defineConfig } from 'vite';
2+
import webpackStats from 'rollup-plugin-webpack-stats';
3+
4+
export default defineConfig({
5+
build: {
6+
rollupOptions: {
7+
output: {
8+
assetFileNames: 'assets/[name][extname]',
9+
chunkFileNames: 'assets/[name].js',
10+
entryFileNames: 'assets/[name].js',
11+
},
12+
},
13+
},
14+
plugins: [webpackStats()],
15+
});

test/package/vite/case-options.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default defineConfig([
2424
...baseConfig,
2525
build: {
2626
...baseConfig.build,
27+
outDir: 'dist2',
2728
},
2829
plugins: [
2930
// A contrived demo to show that plugin options can access vite outputOptions

test/package/vite/configs.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { promisify } from 'node:util';
2+
import { exec as execCallback} from 'node:child_process';
3+
import { describe, expect, test } from 'vitest';
4+
5+
const exec = promisify(execCallback);
6+
7+
const testCases = [
8+
{ type: 'commonjs', run: 'npm run build-commonjs' },
9+
{ type: 'esm', run: 'npm run build-esm' },
10+
{ type: 'ts', run: 'npm run build-ts' },
11+
];
12+
13+
describe('package - vite configs', () => {
14+
testCases.forEach((testCase) => {
15+
test(`should build successfully with rollup ${testCase.type} config`, async () => {
16+
const { stdout } = await exec(`cross-env NO_COLOR=true ${testCase.run}`);
17+
18+
expect(stdout).toMatch('Stats saved to');
19+
expect(stdout).toMatch('built in');
20+
});
21+
});
22+
});

test/package/vite/options.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { vol } from 'memfs';
66

77
import viteConfigs from './case-options.mjs';
88

9-
describe('vite options', () => {
9+
describe('package - vite options', () => {
1010
beforeEach(() => {
1111
vol.reset();
1212
});
@@ -15,7 +15,7 @@ describe('vite options', () => {
1515
const config = viteConfigs[0];
1616
await vite(config);
1717

18-
const actual = await fs.readFile(path.join(config.output.dir, 'webpack-stats.json'), 'utf8');
18+
const actual = await fs.readFile(path.join(config.build.outDir, 'webpack-stats.json'), 'utf8');
1919
const stats = JSON.parse(actual);
2020
expect(stats).toMatchObject({
2121
assets: [

test/package/vite/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7+
"build-commonjs": "vite build -c case-config.js",
8+
"build-esm": "vite build -c case-config.mjs",
9+
"build-ts": "vite build -c case-config.ts",
710
"test": "vitest"
811
},
912
"keywords": [],
1013
"license": "ISC",
1114
"devDependencies": {
15+
"cross-env": "^7.0.0",
1216
"rollup-plugin-webpack-stats": "*",
1317
"vite": "^7.0.0"
1418
}

0 commit comments

Comments
 (0)