Skip to content

Commit

Permalink
[node] Use vitest for unit tests (#11631)
Browse files Browse the repository at this point in the history
Similar to #11302, but for the `@vercel/node` package.
  • Loading branch information
TooTallNate committed May 23, 2024
1 parent 6529a9a commit 139e8cd
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .changeset/smooth-teachers-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
11 changes: 8 additions & 3 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
"scripts": {
"build": "node build.mjs",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --env node --verbose --bail --runInBand",
"test-unit": "pnpm test test/unit",
"test-e2e": "pnpm test test/integration",
"type-check": "tsc --noEmit"
"type-check": "tsc --noEmit",
"vitest-unit-run": "vitest",
"vitest-unit": "glob --absolute 'test/unit/**/*.test.ts' 'test/unit/**/*.test.mts'"
},
"files": [
"dist"
Expand Down Expand Up @@ -51,13 +52,17 @@
"@types/etag": "1.8.0",
"@types/jest": "29.5.0",
"@vercel/functions": "workspace:*",
"@vitest/expect": "1.4.0",
"content-type": "1.0.5",
"cookie": "0.4.0",
"cross-env": "7.0.3",
"execa": "3.2.0",
"fs-extra": "11.1.0",
"glob": "10.3.16",
"jest-junit": "16.0.0",
"source-map-support": "0.5.12",
"tree-kill": "1.2.2"
"tree-kill": "1.2.2",
"vite": "5.1.6",
"vitest": "1.3.1"
}
}
4 changes: 2 additions & 2 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ async function compile(
const babelCompileEnabled =
!isEdgeFunction || process.env.VERCEL_EDGE_NO_BABEL !== '1';
if (babelCompileEnabled && esmPaths.length) {
const babelCompile = require('./babel').compile;
const babelCompile = (await import('./babel.js')).compile;
for (const path of esmPaths) {
const pathDir = join(workPath, dirname(path));
if (!pkgCache.has(pathDir)) {
Expand Down Expand Up @@ -348,7 +348,7 @@ async function compile(
);
}
console.log(`Compiling "${filename}" from ESM to CommonJS...`);
const { code, map } = babelCompile(filename, source);
const { code, map } = babelCompile(filename, String(source));
shouldAddSourcemapSupport = true;
preparedFiles[path] = new FileBlob({
data: `${code}\n//# sourceMappingURL=${filename}.map`,
Expand Down
3 changes: 2 additions & 1 deletion packages/node/test/unit/dev.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { afterAll, describe, expect, test, vi } from 'vitest';
import { forkDevServer, readMessage } from '../../src/fork-dev-server';
import { resolve, extname } from 'path';
import { createServer } from 'http';
Expand All @@ -7,7 +8,7 @@ import { fetch } from 'undici';
import { promisify } from 'util';
import { setTimeout } from 'timers/promises';

jest.setTimeout(20 * 1000);
vi.setConfig({ testTimeout: 20 * 1000 });

const [NODE_MAJOR] = process.versions.node.split('.').map(v => Number(v));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, test, expect } from 'vitest';
import { Headers, Response, Request } from 'node-fetch';
import {
getUrl,
respond,
// @ts-ignore - this is a special patch file to allow importing from the template
} from '../../../src/edge-functions/edge-handler-template.mjs';
} from '../../../src/edge-functions/edge-handler-template.js';

describe('edge-handler-template', () => {
describe('getUrl()', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/node/test/unit/edge-handler/edge-matcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest';
import { prepareFilesystem } from '../test-utils';
import { build } from '../../../src';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createEdgeWasmPlugin } from '../../../dist/edge-functions/edge-wasm-plugin.mjs';
import { test, expect } from 'vitest';
import { createEdgeWasmPlugin } from '../../../src/edge-functions/edge-wasm-plugin.mjs';
import { prepareFilesystem } from '../test-utils';
import { build } from 'esbuild';
import { join } from 'path';
Expand Down
1 change: 1 addition & 0 deletions packages/node/test/unit/prepare-cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, test, expect } from 'vitest';
import path from 'path';
import assert from 'assert';
import { prepareCache } from '../../src';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest';
import { getBodyParser } from '../../../src/serverless-functions/helpers';

describe('serverless-functions/helpers', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/node/test/unit/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, expect, test } from 'vitest';
import { entrypointToOutputPath } from '../../src/utils';

describe('entrypointToOutputPath()', () => {
Expand Down
26 changes: 26 additions & 0 deletions packages/node/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// <reference types="vitest" />
import { defineConfig } from 'vite';

export default defineConfig({
test: {
// Use of process.chdir prohibits usage of the default "threads". https://vitest.dev/config/#forks
pool: 'forks',
env: {
// Vitest supresses color output when `process.env.CI` is true
// so override that behavior
// Issue: https://github.com/vitest-dev/vitest/issues/2732
// Fix: https://github.com/JoshuaKGoldberg/expect-no-axe-violations/pull/3/files
FORCE_COLOR: '1',
},
exclude: [
// default
'**/node_modules/**',
'**/dist/**',
'**/cypress/**',
'**/.{idea,git,cache,output,temp}/**',
'**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*',
// some artifacts in the fixtures have spec files that we're not using
'**/*.spec.js',
],
},
});
111 changes: 96 additions & 15 deletions pnpm-lock.yaml

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

0 comments on commit 139e8cd

Please sign in to comment.