Skip to content

Commit

Permalink
build, feat: Add snapshot tests (#18)
Browse files Browse the repository at this point in the history
Add in initial snapshot tests for rollup, vite, and webpack.
  • Loading branch information
nicholas-codecov authored Dec 8, 2023
1 parent bc8e4ef commit 7fcb70b
Show file tree
Hide file tree
Showing 30 changed files with 569 additions and 179 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,60 @@ jobs:
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

integration-test:
name: Run Integration Tests (Node ${{ matrix.node-version }})
needs: install
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version: [
"18.19.0",
"20.10.0"
]
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 8
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- name: Cache node_modules
id: cache-node-modules
uses: actions/cache@v3
env:
cache-name: cache-codecov-js-bundle-plugin-node-modules
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-${{ env.cache-name }}-
- name: Install dependencies
run: pnpm install

- name: Build packages
run: pnpm run build

- name: Run unit tests
run: pnpm run test:e2e --maxWorkers=2


# fossa:
# name: Run Fossa
# runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ component_management:
name: Webpack plugin
paths:
- packages/webpack-plugin/**

ignore:
- ./examples/*
- ./integration-tests/*
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import path from "path";
import fs from "fs";
import { type Output } from "@codecov/bundler-plugin-core";
import { rollup } from "rollup";
// @ts-expect-error - no types
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import { codecovRollupPlugin } from "@codecov/rollup-plugin";

const expectedStats = {
version: "1",
plugin: { name: "codecov-rollup-bundle-analysis-plugin", version: "1.0.0" },
builtAt: 1701788687217,
duration: 7,
bundler: { name: "rollup", version: "4.6.0" },
assets: [{ name: "main-Kc6Ge1DG.js", size: 216 }],
chunks: [
{
id: "main",
uniqueId: "0-main",
entry: true,
initial: false,
files: ["main-Kc6Ge1DG.js"],
names: ["main"],
},
],
modules: [
{
name: "./src/getRandomNumber.js",
size: 98,
chunks: ["main"],
chunkUniqueIds: ["0-main"],
},
{
name: "./src/main.js",
size: 115,
chunks: ["main"],
chunkUniqueIds: ["0-main"],
},
],
};

describe("Generating rollup stats", () => {
let stats: Output;
const rollupPath = path.resolve(__dirname, "../../test-apps/rollup");
beforeAll(async () => {
await rollup({
input: `${rollupPath}/src/main.js`,
plugins: [
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
resolve(),
commonjs(),
codecovRollupPlugin({ enableBundleAnalysis: true, dryRun: true }),
],
}).then((bundle) =>
bundle.write({
dir: `${rollupPath}/dist`,
entryFileNames: "[name]-[hash].js",
}),
);

const statsFilePath = path.resolve(
rollupPath,
"dist/codecov-bundle-stats.json",
);

const statsData = fs.readFileSync(statsFilePath);
stats = JSON.parse(statsData.toString()) as Output;
});

afterAll(() => {
fs.rm(
path.resolve(rollupPath, "dist"),
{ recursive: true, force: true },
() => null,
);
});

it("sets the correct version", () => {
expect(stats.version).toStrictEqual(expectedStats.version);
});

it("sets the correct plugin information", () => {
expect(stats.plugin).toStrictEqual(expectedStats.plugin);
});

it("sets the correct bundler information", () => {
expect(stats.bundler).toStrictEqual(expectedStats.bundler);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import path from "path";
import fs from "fs";
import { type Output } from "@codecov/bundler-plugin-core";
import { build } from "vite";
import { codecovVitePlugin } from "@codecov/vite-plugin";

const expectedStats = {
version: "1",
plugin: { name: "codecov-vite-bundle-analysis-plugin", version: "1.0.0" },
builtAt: 1701788687217,
duration: 7,
bundler: { name: "rollup", version: "4.6.0" },
assets: [{ name: "main-Kc6Ge1DG.js", size: 216 }],
chunks: [
{
id: "main",
uniqueId: "0-main",
entry: true,
initial: false,
files: ["main-Kc6Ge1DG.js"],
names: ["main"],
},
],
modules: [
{
name: "./src/getRandomNumber.js",
size: 98,
chunks: ["main"],
chunkUniqueIds: ["0-main"],
},
{
name: "./src/main.js",
size: 115,
chunks: ["main"],
chunkUniqueIds: ["0-main"],
},
],
};

describe("Generating vite stats", () => {
let stats: Output;
const vitePath = path.resolve(__dirname, "../../test-apps/vite");
beforeAll(async () => {
await build({
clearScreen: false,
root: vitePath,
build: {
outDir: "dist",
rollupOptions: {
input: `${vitePath}/index.html`,
output: {
format: "cjs",
},
},
},
plugins: [
codecovVitePlugin({ enableBundleAnalysis: true, dryRun: true }),
],
});

const statsFilePath = path.resolve(
vitePath,
"dist/codecov-bundle-stats.json",
);

const statsData = fs.readFileSync(statsFilePath);
stats = JSON.parse(statsData.toString()) as Output;
});

afterAll(() => {
fs.rm(
path.resolve(vitePath, "dist"),
{ recursive: true, force: true },
() => null,
);
});

it("sets the correct version", () => {
expect(stats.version).toStrictEqual(expectedStats.version);
});

it("sets the correct plugin information", () => {
expect(stats.plugin).toStrictEqual(expectedStats.plugin);
});

it("sets the correct bundler information", () => {
expect(stats.bundler).toStrictEqual(expectedStats.bundler);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import path from "path";
import fs from "fs";
import { type Output } from "@codecov/bundler-plugin-core";
import { webpack } from "webpack";
import { codecovWebpackPlugin } from "@codecov/webpack-plugin";

const expectedStats = {
version: "1",
plugin: { name: "codecov-webpack-bundle-analysis-plugin", version: "1.0.0" },
builtAt: 1701788687217,
duration: 7,
bundler: { name: "webpack", version: "5.89.0" },
assets: [{ name: "main-Kc6Ge1DG.js", size: 216 }],
chunks: [
{
id: "main",
uniqueId: "0-main",
entry: true,
initial: false,
files: ["main-Kc6Ge1DG.js"],
names: ["main"],
},
],
modules: [
{
name: "./src/getRandomNumber.js",
size: 98,
chunks: ["main"],
chunkUniqueIds: ["0-main"],
},
{
name: "./src/main.js",
size: 115,
chunks: ["main"],
chunkUniqueIds: ["0-main"],
},
],
};

describe("Generating webpack stats", () => {
let stats: Output;
const webpackPath = path.resolve(__dirname, "../../test-apps/webpack");
beforeAll(async () => {
await new Promise<void>((resolve) => {
webpack(
{
cache: false,
entry: `${webpackPath}/src/main.js`,
output: {
path: `${webpackPath}/dist`,
filename: "main-[hash].js",
},
mode: "production",
plugins: [
codecovWebpackPlugin({ enableBundleAnalysis: true, dryRun: true }),
],
},
(err) => {
if (err) {
throw err;
}

resolve();
},
);
});

const statsFilePath = path.resolve(
webpackPath,
"dist/codecov-bundle-stats.json",
);

const statsData = fs.readFileSync(statsFilePath);
stats = JSON.parse(statsData.toString()) as Output;
});

afterAll(() => {
fs.rm(
path.resolve(webpackPath, "dist"),
{ recursive: true, force: true },
() => null,
);
});

it("sets the correct version", () => {
expect(stats.version).toStrictEqual(expectedStats.version);
});

it("sets the correct plugin information", () => {
expect(stats.plugin).toStrictEqual(expectedStats.plugin);
});

it("sets the correct bundler information", () => {
expect(stats.bundler).toStrictEqual(expectedStats.bundler);
});
});
25 changes: 2 additions & 23 deletions integration-tests/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageJson = require("./package.json") as { version: string };

const config = {
module.exports = {
testEnvironment: "node",
collectCoverageFrom: ["!**/node_modules/**"],
transform: {
"^.+\\.(t|j)sx?$": [
"@swc/jest",
{
jsc: {
transform: {
optimizer: {
globals: {
vars: {
__PACKAGE_VERSION__: packageJson.version,
},
},
},
},
},
},
],
"^.+\\.(t|j)sx?$": ["@swc/jest"],
},
};

export default config;
Loading

0 comments on commit 7fcb70b

Please sign in to comment.