Skip to content

Commit

Permalink
test: add plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
changfeng committed May 31, 2023
1 parent bb7465b commit a9eef36
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"lint:fix": "pnpm run lint --fix",
"build": "tsup && tsx scripts/postbuild.mts",
"dev": "tsup --watch",
"test": "pnpm build && pnpm --filter app build",
"test": "vitest",
"release": "bumpp && pnpm publish",
"prepublishOnly": "pnpm run build"
},
Expand All @@ -82,6 +82,7 @@
"bumpp": "^9.1.0",
"eslint": "^8.41.0",
"eslint-define-config": "^1.20.0",
"execa": "^7.1.1",
"fast-glob": "^3.2.12",
"prettier": "^2.8.8",
"tsup": "^6.7.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export default createUnplugin<Options | undefined>(() => {
c.yellow(version.padEnd(longestVersionLength, ' ')),
);
const formattedImporters = importers
.filter((name) => name !== duplicatedPackage)
.filter((importer) => importer !== `${duplicatedPackage}@${version}`)
.map((name) => c.green(name))
.join(', ');
warningMessages.push(` - ${formattedVersion} imported by ${formattedImporters}`);
Expand Down
5 changes: 0 additions & 5 deletions tests/basic.test.ts

This file was deleted.

3 changes: 2 additions & 1 deletion tests/fixtures/mono/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"clean": "rimraf -rf dist",
"build:vite:esm": "pnpm clean && vite build -c vite.config.mts",
"build:vite:cjs": "pnpm clean && vite build -c vite.config.cts",
"build:rollup": "pnpm clean && rollup -c",
"build:rollup:esm": "pnpm clean && rollup -c rollup.config.mjs",
"build:rollup:cjs": "pnpm clean && rollup -c rollup.config.cjs",
"build": "pnpm build:vite:esm && pnpm build:vite:cjs && pnpm build:rollup"
},
"dependencies": {
Expand Down
20 changes: 20 additions & 0 deletions tests/fixtures/mono/app/rollup.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const rollupPluginCommonjs = require('@rollup/plugin-commonjs');
const rollupPluginJson = require('@rollup/plugin-json');
const rollupPLuginNodeResolve = require('@rollup/plugin-node-resolve');
const { defineConfig } = require('rollup');

const unpluginDetectDuplicatedDeps = require('../../../../dist/rollup.js');

module.exports = defineConfig({
input: 'index.js',
output: {
format: 'esm',
file: './dist/bundle.mjs',
},
plugins: [
rollupPluginCommonjs(),
rollupPLuginNodeResolve(),
rollupPluginJson(),
unpluginDetectDuplicatedDeps(),
],
});
5 changes: 5 additions & 0 deletions tests/fixtures/mono/app/stderr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[warn] multiple versions of axios is bundled!

axios:
- 0.27.2 imported by tests/fixtures/mono/packages/pkg2/index.js, axios@0.27.2
- 1.4.0 imported by axios@1.4.0
1 change: 1 addition & 0 deletions tests/fixtures/mono/app/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

42 changes: 42 additions & 0 deletions tests/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import fs from 'node:fs/promises';
import { resolve } from 'node:path';

import { execa } from 'execa';
import { expect, test } from 'vitest';

const testCwd = resolve(__dirname, './fixtures/mono/app');

const stdout = await fs.readFile(resolve(testCwd, 'stdout.txt'), 'utf8');
const stderr = await fs.readFile(resolve(testCwd, 'stderr.txt'), 'utf8');
const exec = async (command: string) => {
const [cmd, ...args] = command.split(' ');
const result = await execa(cmd, args, {
cwd: testCwd,
preferLocal: true,
});
return result;
};

test('vite esm', async () => {
const result = await exec('vite build -l error -c vite.config.mts');
expect(result.stdout).toBe(stdout);
expect(result.stderr).toBe(stderr);
});

test('vite cjs', async () => {
const result = await exec('vite build -l error -c vite.config.cts');
expect(result.stdout).toBe(stdout);
expect(result.stderr).toBe(stderr);
});

test('rollup esm', async () => {
const result = await exec('rollup --silent -c rollup.config.mjs');
expect(result.stdout).toBe(stdout);
expect(result.stderr).toBe(stderr);
});

test('rollup cjs', async () => {
const result = await exec('rollup --silent -c rollup.config.cjs');
expect(result.stdout).toBe(stdout);
expect(result.stderr).toBe(stderr);
});

0 comments on commit a9eef36

Please sign in to comment.