Skip to content

Commit

Permalink
Refactor to use Vitest over Jest (#1306)
Browse files Browse the repository at this point in the history
* refactor: vitest setup

* refactor: apply grit transform and biome check

* refactor: to not import vitest globals

* fix: remove unused rule

* refactor: tests to esm

* fix: to import vitest

* fix: vitest run scripts

* fix: add coverage dependency

* chore: disable codecov

* fix: biome globals
  • Loading branch information
tdeekens authored Nov 27, 2024
1 parent 4ad33f6 commit 2e009a5
Show file tree
Hide file tree
Showing 26 changed files with 994 additions and 1,589 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ jobs:
run: pnpm build

- name: Test (with coverage)
run: pnpm test:ci:coverage
run: pnpm test:coverage

- uses: codecov/codecov-action@v5
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
verbose: true
# - uses: codecov/codecov-action@v5
# with:
# fail_ci_if_error: true
# token: ${{ secrets.CODECOV_TOKEN }}
# verbose: true
27 changes: 0 additions & 27 deletions .jestrc.test.json

This file was deleted.

13 changes: 1 addition & 12 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,6 @@
"bracketSpacing": true,
"quoteStyle": "single"
},
"globals": [
"it",
"describe",
"expect",
"jest",
"before",
"beforeAll",
"beforeEach",
"after",
"afterAll",
"afterEach"
]
"globals": []
}
}
12 changes: 5 additions & 7 deletions demo/apollo.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const { ApolloServer, gql } = require('apollo-server');
const {
createPlugin: createPromsterMetricsPlugin,
import {
createPlugin as createPromsterMetricsPlugin,
signalIsUp,
} = require('@promster/apollo');
const {
createServer: createPrometheusMetricsServer,
} = require('@promster/server');
} from '@promster/apollo';
import { createServer as createPrometheusMetricsServer } from '@promster/server';
import { ApolloServer, gql } from 'apollo-server';

const typeDefs = gql`
type Book {
Expand Down
8 changes: 3 additions & 5 deletions demo/express.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const express = require('express');
const { createMiddleware, signalIsUp } = require('@promster/express');
const {
createServer: createPrometheusMetricsServer,
} = require('@promster/server');
import { createMiddleware, signalIsUp } from '@promster/express';
import { createServer as createPrometheusMetricsServer } from '@promster/server';
import express from 'express';

async function launchServer() {
const app = express();
Expand Down
Empty file removed jest-runner-test.config.js
Empty file.
13 changes: 4 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@
"changeset:version-and-format": "changeset version && pnpm format '**/package.json' && pnpm install --no-frozen-lockfile",
"changeset": "changeset",
"clean": "manypkg exec rm -rf build dist tsconfig.tsbuildinfo ts-build-cache",
"develop": "jest --projects .jestrc.test.json --watch",
"biome:check": "biome check --write",
"biome:fix": "biome lint --write",
"format": "biome format --write",
"lint": "biome lint",
"prerelease": "cross-env NODE_ENV=production pnpm auth && pnpm build",
"release": "changeset publish",
"setup": "husky install && preconstruct dev && manypkg check && check-node-version --package --print",
"test:ci:coverage": "cross-env NODE_ENV=test npm test -- --no-watchman --coverage",
"test:ci": "cross-env NODE_ENV=test npm test -- --no-watchman",
"test:coverage": "cross-env NODE_ENV=test npm test -- --coverage",
"test:watch": "cross-env NODE_ENV=test npm test -- --watch",
"test": "cross-env NODE_ENV=test jest --config .jestrc.test.json --forceExit --runInBand",
"test": "cross-env NODE_ENV=test vitest run",
"typecheck": "tsc --build"
},
"author": "Tobias Deekens <nerd@tdeekens.name>",
Expand Down Expand Up @@ -61,14 +57,13 @@
"@tsconfig/node18": "18.2.4",
"@types/node": "20.16.13",
"@types/semver": "7.5.8",
"@vitest/coverage-v8": "2.1.5",
"check-node-version": "4.2.1",
"cross-env": "7.0.3",
"husky": "9.0.11",
"jest": "29.7.0",
"jest-watch-typeahead": "2.2.2",
"lint-staged": "15.2.10",
"rimraf": "5.0.10",
"ts-jest": "29.1.5",
"typescript": "5.6.2"
"typescript": "5.6.2",
"vitest": "2.1.5"
}
}
16 changes: 8 additions & 8 deletions packages/apollo/src/plugin/plugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { ApolloServer, gql } = require('apollo-server');
const parsePrometheusTextFormat = require('parse-prometheus-text-format');
const { createPlugin: createPromsterMetricsPlugin } = require('./plugin');
const {
createServer: createPrometheusMetricsServer,
} = require('@promster/server');
const { getDirective, mapSchema, MapperKind } = require('@graphql-tools/utils');
const { makeExecutableSchema } = require('@graphql-tools/schema');
import { makeExecutableSchema } from '@graphql-tools/schema';
import { afterAll, beforeAll, expect, it } from 'vitest';

import { MapperKind, getDirective, mapSchema } from '@graphql-tools/utils';
import { createServer as createPrometheusMetricsServer } from '@promster/server';
import { ApolloServer, gql } from 'apollo-server';
import parsePrometheusTextFormat from 'parse-prometheus-text-format';
import { createPlugin as createPromsterMetricsPlugin } from './plugin';

function throwErrorDirectiveTransformer(schema, directiveName = 'error') {
return mapSchema(schema, {
Expand Down
12 changes: 6 additions & 6 deletions packages/express/src/middleware/middleware.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const express = require('express');
const parsePrometheusTextFormat = require('parse-prometheus-text-format');
const { createMiddleware } = require('./middleware');
const {
createServer: createPrometheusMetricsServer,
} = require('@promster/server');
import { createServer as createPrometheusMetricsServer } from '@promster/server';
import { afterAll, beforeAll, expect, it } from 'vitest';

import express from 'express';
import parsePrometheusTextFormat from 'parse-prometheus-text-format';
import { createMiddleware } from './middleware';

const metricsPort = '1338';
const appPort = '3001';
Expand Down
12 changes: 6 additions & 6 deletions packages/fastify/src/plugin/plugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const parsePrometheusTextFormat = require('parse-prometheus-text-format');
const {
createServer: createPrometheusMetricsServer,
} = require('@promster/server');
const Fastify = require('fastify');
const { plugin } = require('./plugin');
import { createServer as createPrometheusMetricsServer } from '@promster/server';
import { afterAll, beforeAll, expect, it } from 'vitest';

import Fastify from 'fastify';
import parsePrometheusTextFormat from 'parse-prometheus-text-format';
import { plugin } from './plugin';

const metricsPort = '1339';
const appPort = '3002';
Expand Down
14 changes: 7 additions & 7 deletions packages/hapi/src/plugin/plugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const Hapi = require('@hapi/hapi');
const parsePrometheusTextFormat = require('parse-prometheus-text-format');
const {
createServer: createPrometheusMetricsServer,
} = require('@promster/server');
const {
import Hapi from '@hapi/hapi';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';

import { createServer as createPrometheusMetricsServer } from '@promster/server';
import parsePrometheusTextFormat from 'parse-prometheus-text-format';
import {
createPlugin,
getAreServerEventsSupported,
getDoesResponseNeedInvocation,
} = require('./plugin');
} from './plugin';

const metricsPort = '1340';
const appPort = '3011';
Expand Down
14 changes: 7 additions & 7 deletions packages/marblejs/src/middleware/middleware.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { httpListener, createServer, r } = require('@marblejs/http');
const { mapTo } = require('rxjs/operators');
const parsePrometheusTextFormat = require('parse-prometheus-text-format');
const {
createServer: createPrometheusMetricsServer,
} = require('@promster/server');
const { createMiddleware } = require('./middleware');
import { createServer, httpListener, r } from '@marblejs/http';
import { afterAll, beforeAll, expect, it } from 'vitest';

import { createServer as createPrometheusMetricsServer } from '@promster/server';
import parsePrometheusTextFormat from 'parse-prometheus-text-format';
import { mapTo } from 'rxjs/operators';
import { createMiddleware } from './middleware';

const metricsPort = '1341';
const appPort = '3004';
Expand Down
15 changes: 8 additions & 7 deletions packages/metrics/src/create-gc-metrics/create-gc-metrics.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
jest.mock('../client', () => ({
configure: jest.fn(),
vi.mock('../client', () => ({
configure: vi.fn(),
Prometheus: {
Gauge: jest.fn(),
Counter: jest.fn(),
Summary: jest.fn(),
Histogram: jest.fn(),
Gauge: vi.fn(),
Counter: vi.fn(),
Summary: vi.fn(),
Histogram: vi.fn(),
},
}));

const { createGcMetrics } = require('./create-gc-metrics');
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { createGcMetrics } from './create-gc-metrics';

describe('createGcMetrics', () => {
let metrics;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
jest.mock('../client', () => ({
configure: jest.fn(),
vi.mock('../client', () => ({
configure: vi.fn(),
Prometheus: {
Gauge: jest.fn(),
Counter: jest.fn(),
Summary: jest.fn(),
Histogram: jest.fn(),
Gauge: vi.fn(),
Counter: vi.fn(),
Summary: vi.fn(),
Histogram: vi.fn(),
},
}));

const { createHttpMetrics } = require('./create-http-metrics');
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { createHttpMetrics } from './create-http-metrics';

describe('createHttpMetrics', () => {
let metrics;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
const { createRequestRecorder } = require('./create-request-recorder');
const { timing } = require('../timing');
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { timing } from '../timing';

import { createRequestRecorder } from './create-request-recorder';

describe('createRequestRecorder', () => {
const createHttpMetrics = () => ({
httpRequestDurationInSeconds: [
{
observe: jest.fn(),
observe: vi.fn(),
},
],
httpRequestDurationPerPercentileInSeconds: [
{
observe: jest.fn(),
observe: vi.fn(),
},
],
httpRequestsTotal: [
{
inc: jest.fn(),
inc: vi.fn(),
},
],
httpRequestContentLengthInBytes: [
{
observe: jest.fn(),
observe: vi.fn(),
},
],
httpResponseContentLengthInBytes: [
{
observe: jest.fn(),
observe: vi.fn(),
},
],
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { endMeasurementFrom } = require('./end-measurement-from');
import { beforeEach, describe, expect, it } from 'vitest';
import { endMeasurementFrom } from './end-measurement-from';

describe('endMeasurementFrom', () => {
const start = [1, 2];
Expand Down
3 changes: 2 additions & 1 deletion packages/metrics/src/normalizers/method/method.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { normalizeMethod } = require('./method');
import { beforeEach, describe, expect, it } from 'vitest';
import { normalizeMethod } from './method';

describe('normalizeMethod', () => {
const method = 'GET';
Expand Down
3 changes: 2 additions & 1 deletion packages/metrics/src/normalizers/path/path.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { normalizePath } = require('./path');
import { beforeEach, describe, expect, it } from 'vitest';
import { normalizePath } from './path';

describe('normalizePath', () => {
let normalizedPath;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { normalizeStatusCode } = require('./status-code');
import { beforeEach, describe, expect, it } from 'vitest';
import { normalizeStatusCode } from './status-code';

describe('normalizeStatusCode', () => {
let normalizedStatusCode;
Expand Down
3 changes: 2 additions & 1 deletion packages/metrics/src/sort-labels/sort-labels.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { sortLabels } = require('./sort-labels');
import { beforeEach, describe, expect, it } from 'vitest';
import { sortLabels } from './sort-labels';

describe('sortLabels', () => {
const unsorted = { b: 'c', a: 'b' };
Expand Down
10 changes: 6 additions & 4 deletions packages/metrics/src/summary/summary.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
jest.mock('../client', () => ({
vi.mock('../client', () => ({
defaultRegister: {
metrics: jest.fn(async () => Promise.resolve('metrics')),
metrics: vi.fn(async () => Promise.resolve('metrics')),
contentType: 'application/test',
},
}));

const { defaultRegister } = require('../client');
const { getSummary, getContentType } = require('./summary');
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { defaultRegister } from '../client';

import { getContentType, getSummary } from './summary';

describe('getSummary', () => {
let summary;
Expand Down
3 changes: 2 additions & 1 deletion packages/metrics/src/timing/timing.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { default: timing, Timing } = require('./timing');
import { describe, expect, it } from 'vitest';
import { Timing, default as timing } from './timing';

describe('timing', () => {
it('should return an instance of Timing', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"tslib": "2.6.3"
},
"devDependencies": {
"@types/node": "20.16.13"
"@types/node": "20.16.13",
"parse-prometheus-text-format": "1.1.1"
}
}
8 changes: 5 additions & 3 deletions packages/server/src/server/server.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const parsePrometheusTextFormat = require('parse-prometheus-text-format');
const { createGcMetrics } = require('@promster/metrics');
const { createServer } = require('./server');
import { createGcMetrics } from '@promster/metrics';
import { afterAll, beforeAll, expect, it } from 'vitest';

import parsePrometheusTextFormat from 'parse-prometheus-text-format';
import { createServer } from './server';

const metricsPort = '1342';
const metricsServerUrl = `http://localhost:${metricsPort}`;
Expand Down
Loading

0 comments on commit 2e009a5

Please sign in to comment.