Skip to content

Commit 7fcb70b

Browse files
authored
build, feat: Add snapshot tests (#18)
Add in initial snapshot tests for rollup, vite, and webpack.
1 parent bc8e4ef commit 7fcb70b

File tree

30 files changed

+569
-179
lines changed

30 files changed

+569
-179
lines changed

.github/workflows/ci.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,60 @@ jobs:
194194
env:
195195
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
196196

197+
integration-test:
198+
name: Run Integration Tests (Node ${{ matrix.node-version }})
199+
needs: install
200+
runs-on: ubuntu-latest
201+
strategy:
202+
fail-fast: false
203+
matrix:
204+
node-version: [
205+
"18.19.0",
206+
"20.10.0"
207+
]
208+
steps:
209+
- name: Checkout
210+
uses: actions/checkout@v4
211+
with:
212+
fetch-depth: 0
213+
214+
- name: Setup node
215+
uses: actions/setup-node@v3
216+
with:
217+
node-version: ${{ matrix.node-version }}
218+
219+
- name: Install pnpm
220+
uses: pnpm/action-setup@v2
221+
with:
222+
version: 8
223+
run_install: false
224+
225+
- name: Get pnpm store directory
226+
shell: bash
227+
run: |
228+
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
229+
230+
- name: Cache node_modules
231+
id: cache-node-modules
232+
uses: actions/cache@v3
233+
env:
234+
cache-name: cache-codecov-js-bundle-plugin-node-modules
235+
with:
236+
path: ${{ env.STORE_PATH }}
237+
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }}
238+
restore-keys: |
239+
${{ runner.os }}-${{ env.cache-name }}-
240+
241+
- name: Install dependencies
242+
run: pnpm install
243+
244+
- name: Build packages
245+
run: pnpm run build
246+
247+
- name: Run unit tests
248+
run: pnpm run test:e2e --maxWorkers=2
249+
250+
197251
# fossa:
198252
# name: Run Fossa
199253
# runs-on: ubuntu-latest

codecov.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ component_management:
2828
name: Webpack plugin
2929
paths:
3030
- packages/webpack-plugin/**
31+
32+
ignore:
33+
- ./examples/*
34+
- ./integration-tests/*
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
2+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
3+
import path from "path";
4+
import fs from "fs";
5+
import { type Output } from "@codecov/bundler-plugin-core";
6+
import { rollup } from "rollup";
7+
// @ts-expect-error - no types
8+
import resolve from "@rollup/plugin-node-resolve";
9+
import commonjs from "@rollup/plugin-commonjs";
10+
import { codecovRollupPlugin } from "@codecov/rollup-plugin";
11+
12+
const expectedStats = {
13+
version: "1",
14+
plugin: { name: "codecov-rollup-bundle-analysis-plugin", version: "1.0.0" },
15+
builtAt: 1701788687217,
16+
duration: 7,
17+
bundler: { name: "rollup", version: "4.6.0" },
18+
assets: [{ name: "main-Kc6Ge1DG.js", size: 216 }],
19+
chunks: [
20+
{
21+
id: "main",
22+
uniqueId: "0-main",
23+
entry: true,
24+
initial: false,
25+
files: ["main-Kc6Ge1DG.js"],
26+
names: ["main"],
27+
},
28+
],
29+
modules: [
30+
{
31+
name: "./src/getRandomNumber.js",
32+
size: 98,
33+
chunks: ["main"],
34+
chunkUniqueIds: ["0-main"],
35+
},
36+
{
37+
name: "./src/main.js",
38+
size: 115,
39+
chunks: ["main"],
40+
chunkUniqueIds: ["0-main"],
41+
},
42+
],
43+
};
44+
45+
describe("Generating rollup stats", () => {
46+
let stats: Output;
47+
const rollupPath = path.resolve(__dirname, "../../test-apps/rollup");
48+
beforeAll(async () => {
49+
await rollup({
50+
input: `${rollupPath}/src/main.js`,
51+
plugins: [
52+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
53+
resolve(),
54+
commonjs(),
55+
codecovRollupPlugin({ enableBundleAnalysis: true, dryRun: true }),
56+
],
57+
}).then((bundle) =>
58+
bundle.write({
59+
dir: `${rollupPath}/dist`,
60+
entryFileNames: "[name]-[hash].js",
61+
}),
62+
);
63+
64+
const statsFilePath = path.resolve(
65+
rollupPath,
66+
"dist/codecov-bundle-stats.json",
67+
);
68+
69+
const statsData = fs.readFileSync(statsFilePath);
70+
stats = JSON.parse(statsData.toString()) as Output;
71+
});
72+
73+
afterAll(() => {
74+
fs.rm(
75+
path.resolve(rollupPath, "dist"),
76+
{ recursive: true, force: true },
77+
() => null,
78+
);
79+
});
80+
81+
it("sets the correct version", () => {
82+
expect(stats.version).toStrictEqual(expectedStats.version);
83+
});
84+
85+
it("sets the correct plugin information", () => {
86+
expect(stats.plugin).toStrictEqual(expectedStats.plugin);
87+
});
88+
89+
it("sets the correct bundler information", () => {
90+
expect(stats.bundler).toStrictEqual(expectedStats.bundler);
91+
});
92+
});
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
2+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
3+
import path from "path";
4+
import fs from "fs";
5+
import { type Output } from "@codecov/bundler-plugin-core";
6+
import { build } from "vite";
7+
import { codecovVitePlugin } from "@codecov/vite-plugin";
8+
9+
const expectedStats = {
10+
version: "1",
11+
plugin: { name: "codecov-vite-bundle-analysis-plugin", version: "1.0.0" },
12+
builtAt: 1701788687217,
13+
duration: 7,
14+
bundler: { name: "rollup", version: "4.6.0" },
15+
assets: [{ name: "main-Kc6Ge1DG.js", size: 216 }],
16+
chunks: [
17+
{
18+
id: "main",
19+
uniqueId: "0-main",
20+
entry: true,
21+
initial: false,
22+
files: ["main-Kc6Ge1DG.js"],
23+
names: ["main"],
24+
},
25+
],
26+
modules: [
27+
{
28+
name: "./src/getRandomNumber.js",
29+
size: 98,
30+
chunks: ["main"],
31+
chunkUniqueIds: ["0-main"],
32+
},
33+
{
34+
name: "./src/main.js",
35+
size: 115,
36+
chunks: ["main"],
37+
chunkUniqueIds: ["0-main"],
38+
},
39+
],
40+
};
41+
42+
describe("Generating vite stats", () => {
43+
let stats: Output;
44+
const vitePath = path.resolve(__dirname, "../../test-apps/vite");
45+
beforeAll(async () => {
46+
await build({
47+
clearScreen: false,
48+
root: vitePath,
49+
build: {
50+
outDir: "dist",
51+
rollupOptions: {
52+
input: `${vitePath}/index.html`,
53+
output: {
54+
format: "cjs",
55+
},
56+
},
57+
},
58+
plugins: [
59+
codecovVitePlugin({ enableBundleAnalysis: true, dryRun: true }),
60+
],
61+
});
62+
63+
const statsFilePath = path.resolve(
64+
vitePath,
65+
"dist/codecov-bundle-stats.json",
66+
);
67+
68+
const statsData = fs.readFileSync(statsFilePath);
69+
stats = JSON.parse(statsData.toString()) as Output;
70+
});
71+
72+
afterAll(() => {
73+
fs.rm(
74+
path.resolve(vitePath, "dist"),
75+
{ recursive: true, force: true },
76+
() => null,
77+
);
78+
});
79+
80+
it("sets the correct version", () => {
81+
expect(stats.version).toStrictEqual(expectedStats.version);
82+
});
83+
84+
it("sets the correct plugin information", () => {
85+
expect(stats.plugin).toStrictEqual(expectedStats.plugin);
86+
});
87+
88+
it("sets the correct bundler information", () => {
89+
expect(stats.bundler).toStrictEqual(expectedStats.bundler);
90+
});
91+
});
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
2+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
3+
import path from "path";
4+
import fs from "fs";
5+
import { type Output } from "@codecov/bundler-plugin-core";
6+
import { webpack } from "webpack";
7+
import { codecovWebpackPlugin } from "@codecov/webpack-plugin";
8+
9+
const expectedStats = {
10+
version: "1",
11+
plugin: { name: "codecov-webpack-bundle-analysis-plugin", version: "1.0.0" },
12+
builtAt: 1701788687217,
13+
duration: 7,
14+
bundler: { name: "webpack", version: "5.89.0" },
15+
assets: [{ name: "main-Kc6Ge1DG.js", size: 216 }],
16+
chunks: [
17+
{
18+
id: "main",
19+
uniqueId: "0-main",
20+
entry: true,
21+
initial: false,
22+
files: ["main-Kc6Ge1DG.js"],
23+
names: ["main"],
24+
},
25+
],
26+
modules: [
27+
{
28+
name: "./src/getRandomNumber.js",
29+
size: 98,
30+
chunks: ["main"],
31+
chunkUniqueIds: ["0-main"],
32+
},
33+
{
34+
name: "./src/main.js",
35+
size: 115,
36+
chunks: ["main"],
37+
chunkUniqueIds: ["0-main"],
38+
},
39+
],
40+
};
41+
42+
describe("Generating webpack stats", () => {
43+
let stats: Output;
44+
const webpackPath = path.resolve(__dirname, "../../test-apps/webpack");
45+
beforeAll(async () => {
46+
await new Promise<void>((resolve) => {
47+
webpack(
48+
{
49+
cache: false,
50+
entry: `${webpackPath}/src/main.js`,
51+
output: {
52+
path: `${webpackPath}/dist`,
53+
filename: "main-[hash].js",
54+
},
55+
mode: "production",
56+
plugins: [
57+
codecovWebpackPlugin({ enableBundleAnalysis: true, dryRun: true }),
58+
],
59+
},
60+
(err) => {
61+
if (err) {
62+
throw err;
63+
}
64+
65+
resolve();
66+
},
67+
);
68+
});
69+
70+
const statsFilePath = path.resolve(
71+
webpackPath,
72+
"dist/codecov-bundle-stats.json",
73+
);
74+
75+
const statsData = fs.readFileSync(statsFilePath);
76+
stats = JSON.parse(statsData.toString()) as Output;
77+
});
78+
79+
afterAll(() => {
80+
fs.rm(
81+
path.resolve(webpackPath, "dist"),
82+
{ recursive: true, force: true },
83+
() => null,
84+
);
85+
});
86+
87+
it("sets the correct version", () => {
88+
expect(stats.version).toStrictEqual(expectedStats.version);
89+
});
90+
91+
it("sets the correct plugin information", () => {
92+
expect(stats.plugin).toStrictEqual(expectedStats.plugin);
93+
});
94+
95+
it("sets the correct bundler information", () => {
96+
expect(stats.bundler).toStrictEqual(expectedStats.bundler);
97+
});
98+
});

integration-tests/jest.config.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
1-
// eslint-disable-next-line @typescript-eslint/no-var-requires
2-
const packageJson = require("./package.json") as { version: string };
3-
4-
const config = {
1+
module.exports = {
52
testEnvironment: "node",
6-
collectCoverageFrom: ["!**/node_modules/**"],
73
transform: {
8-
"^.+\\.(t|j)sx?$": [
9-
"@swc/jest",
10-
{
11-
jsc: {
12-
transform: {
13-
optimizer: {
14-
globals: {
15-
vars: {
16-
__PACKAGE_VERSION__: packageJson.version,
17-
},
18-
},
19-
},
20-
},
21-
},
22-
},
23-
],
4+
"^.+\\.(t|j)sx?$": ["@swc/jest"],
245
},
256
};
26-
27-
export default config;

0 commit comments

Comments
 (0)