Skip to content

Commit

Permalink
refactor(tests): use vitest (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo authored Jul 8, 2023
1 parent ccb28e8 commit c87b47e
Show file tree
Hide file tree
Showing 16 changed files with 790 additions and 2,487 deletions.
16 changes: 0 additions & 16 deletions babel.config.js

This file was deleted.

10 changes: 0 additions & 10 deletions jest.config.js

This file was deleted.

16 changes: 3 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"check:lint": "eslint 'src'",
"check:spell": "cspell --gitignore **/*.md",
"check:type": "tsc --noEmit",
"test": "NODE_OPTIONS=--experimental-vm-modules NODE_NO_WARNINGS=1 jest",
"test": "vitest",
"build:esm": "tsc -b tsconfig.esm.json && tsx scripts/esm-post-process.ts",
"build:cjs": "tsc -b tsconfig.cjs.json",
"build:umd": "rollup --configPlugin typescript --config rollup.config.ts",
Expand All @@ -111,13 +111,6 @@
"graphql": ">=0.11 <=16"
},
"devDependencies": {
"@babel/core": "^7.22.8",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
"@babel/plugin-proposal-optional-chaining": "^7.21.0",
"@babel/preset-env": "^7.22.7",
"@babel/preset-typescript": "^7.22.5",
"@cspell/cspell-types": "^6.31.1",
"@rollup/plugin-terser": "^0.4.3",
"@rollup/plugin-typescript": "^11.1.2",
Expand All @@ -127,14 +120,12 @@
"@types/express": "^4.17.17",
"@types/glob": "^8.1.0",
"@types/html-validator": "^5.0.3",
"@types/jest": "^29.5.2",
"@types/k6": "^0.45.0",
"@types/koa": "^2.13.6",
"@types/koa-mount": "^4.0.2",
"@typescript-eslint/eslint-plugin": "^5.61.0",
"@typescript-eslint/parser": "^5.61.0",
"@whatwg-node/fetch": "^0.9.7",
"babel-jest": "^29.6.1",
"cspell": "^6.31.1",
"eslint": "^8.44.0",
"eslint-config-prettier": "^8.8.0",
Expand All @@ -143,8 +134,6 @@
"glob": "^10.3.2",
"graphql": "^16.7.1",
"html-validator": "^6.0.1",
"jest": "^29.6.1",
"jest-jasmine2": "^29.6.1",
"koa": "^2.14.2",
"koa-mount": "^4.0.0",
"node-fetch": "^3.3.1",
Expand All @@ -157,6 +146,7 @@
"typedoc": "^0.24.8",
"typedoc-plugin-markdown": "^3.15.3",
"typescript": "^5.1.6",
"uWebSockets.js": "uNetworking/uWebSockets.js#v20.30.0"
"uWebSockets.js": "uNetworking/uWebSockets.js#v20.30.0",
"vitest": "^0.33.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`should not change globally unique audit ids 1`] = `
[
Expand Down
65 changes: 33 additions & 32 deletions src/__tests__/audits.ts → tests/audits.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { describe, it, expect } from 'vitest';
import {
Audit,
serverAudits,
AuditResult,
renderAuditResultsToHTML,
AuditFail,
} from '../audits';
} from '../src/audits';
import htmlValidator from 'html-validator';

it('should have globally unique audit ids', () => {
Expand Down Expand Up @@ -117,9 +118,9 @@ describe('Render audit results to HTML', () => {
<ul>
<li><b>6</b> audits in total</li>
<li><span style="font-family: monospace">✅</span> <b>2</b> pass</li>
<li><span style="font-family: monospace">⚠️</span> <b>2</b> warnings (optional)</li>
<li><span style="font-family: monospace">❌</span> <b>2</b> errors (required)</li>
<li><span style=\\"font-family: monospace\\">✅</span> <b>2</b> pass</li>
<li><span style=\\"font-family: monospace\\">⚠️</span> <b>2</b> warnings (optional)</li>
<li><span style=\\"font-family: monospace\\">❌</span> <b>2</b> errors (required)</li>
</ul>
<h2>Passing</h2>
Expand All @@ -134,29 +135,29 @@ describe('Render audit results to HTML', () => {
<li><code>warn1</code> SHOULD warn1
<details>
<summary>bad warn1</summary>
<pre><code class="lang-json">{
"statusText": "",
"status": 400,
"headers": {
"x-id": "warn1",
"content-type": "text/plain;charset=UTF-8"
<pre><code class=\\"lang-json\\">{
\\"statusText\\": \\"\\",
\\"status\\": 400,
\\"headers\\": {
\\"x-id\\": \\"warn1\\",
\\"content-type\\": \\"text/plain;charset=UTF-8\\"
},
"body": "Warning!"
\\"body\\": \\"Warning!\\"
}
</code></pre>
</details>
</li>
<li><code>warn2</code> SHOULD warn2
<details>
<summary>bad warn2</summary>
<pre><code class="lang-json">{
"statusText": "",
"status": 400,
"headers": {
"x-id": "warn2",
"content-type": "text/plain;charset=UTF-8"
<pre><code class=\\"lang-json\\">{
\\"statusText\\": \\"\\",
\\"status\\": 400,
\\"headers\\": {
\\"x-id\\": \\"warn2\\",
\\"content-type\\": \\"text/plain;charset=UTF-8\\"
},
"body": "Warning!"
\\"body\\": \\"Warning!\\"
}
</code></pre>
</details>
Expand All @@ -169,29 +170,29 @@ describe('Render audit results to HTML', () => {
<li><code>error1</code> MUST error1
<details>
<summary>bad error1</summary>
<pre><code class="lang-json">{
"statusText": "",
"status": 500,
"headers": {
"x-id": "error1",
"content-type": "text/plain;charset=UTF-8"
<pre><code class=\\"lang-json\\">{
\\"statusText\\": \\"\\",
\\"status\\": 500,
\\"headers\\": {
\\"x-id\\": \\"error1\\",
\\"content-type\\": \\"text/plain;charset=UTF-8\\"
},
"body": "Error!"
\\"body\\": \\"Error!\\"
}
</code></pre>
</details>
</li>
<li><code>error2</code> MUST error2
<details>
<summary>bad error2</summary>
<pre><code class="lang-json">{
"statusText": "",
"status": 500,
"headers": {
"x-id": "error2",
"content-type": "text/plain;charset=UTF-8"
<pre><code class=\\"lang-json\\">{
\\"statusText\\": \\"\\",
\\"status\\": 500,
\\"headers\\": {
\\"x-id\\": \\"error2\\",
\\"content-type\\": \\"text/plain;charset=UTF-8\\"
},
"body": "Error!"
\\"body\\": \\"Error!\\"
}
</code></pre>
</details>
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/client.ts → tests/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { it, expect } from 'vitest';
import fetch from 'node-fetch';
import { RequestHeaders } from '../handler';
import { createClient, NetworkError } from '../client';
import { RequestHeaders } from '../src/handler';
import { createClient, NetworkError } from '../src/client';
import { startTServer } from './utils/tserver';
import { texecute } from './utils/texecute';

Expand Down
File renamed without changes.
20 changes: 11 additions & 9 deletions src/__tests__/handler.ts → tests/handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jest } from '@jest/globals';
import { vi, it, expect } from 'vitest';
import { GraphQLError } from 'graphql';
import fetch from 'node-fetch';
import { Request } from '../handler';
import { Request } from '../src/handler';
import { startTServer } from './utils/tserver';

it.each(['schema', 'context', 'onSubscribe', 'onOperation'])(
Expand Down Expand Up @@ -34,7 +34,7 @@ it('should report graphql errors returned from onSubscribe', async () => {
});

it('should respond with result returned from onSubscribe', async () => {
const onOperationFn = jest.fn(() => {
const onOperationFn = vi.fn(() => {
// noop
});
const server = startTServer({
Expand All @@ -55,7 +55,9 @@ it('should respond with result returned from onSubscribe', async () => {
it.each(['schema', 'context', 'onSubscribe', 'onOperation'])(
'should provide the request context to %s',
async (option) => {
const optionFn = jest.fn<(req: Request<unknown, unknown>) => void>();
const optionFn = vi.fn((_req: Request<unknown, unknown>) => {
// noop
});

const context = {};
const server = startTServer({
Expand Down Expand Up @@ -114,7 +116,7 @@ it('should correctly serialise execution result errors', async () => {
"line": 1,
},
],
"message": "Variable "$num" got invalid value "foo"; Int cannot represent non-integer value: "foo"",
"message": "Variable \\"$num\\" got invalid value \\"foo\\"; Int cannot represent non-integer value: \\"foo\\"",
},
],
}
Expand Down Expand Up @@ -146,7 +148,7 @@ it('should append the provided validation rules array', async () => {
"line": 1,
},
],
"message": "Cannot query field "idontexist" on type "Query".",
"message": "Cannot query field \\"idontexist\\" on type \\"Query\\".",
},
],
}
Expand Down Expand Up @@ -185,12 +187,12 @@ it('should print plain errors in detail', async () => {
// missing body
});
await expect(result.text()).resolves.toMatchInlineSnapshot(
`"{"errors":[{"message":"Unparsable JSON body"}]}"`,
'"{\\"errors\\":[{\\"message\\":\\"Unparsable JSON body\\"}]}"',
);
});

it('should format errors using the formatter', async () => {
const formatErrorFn = jest.fn((_err) => new Error('Formatted'));
const formatErrorFn = vi.fn((_err) => new Error('Formatted'));
const server = startTServer({
formatError: formatErrorFn,
});
Expand Down Expand Up @@ -224,7 +226,7 @@ it('should respect plain errors toJSON implementation', async () => {
};
}
}
const formatErrorFn = jest.fn((_err) => new MyError('Custom toJSON'));
const formatErrorFn = vi.fn((_err) => new MyError('Custom toJSON'));
const server = startTServer({
formatError: formatErrorFn,
});
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/server.ts → tests/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { it } from 'vitest';
import fetch from 'node-fetch';
import { serverAudits } from '../audits/server';
import { serverAudits } from '../src/audits/server';

import { schema } from './fixtures/simple';
import { startTServer } from './utils/tserver';
Expand Down
23 changes: 12 additions & 11 deletions src/__tests__/use.ts → tests/use.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { fetch } from '@whatwg-node/fetch';
import http from 'http';
import express from 'express';
Expand All @@ -7,15 +8,15 @@ import mount from 'koa-mount';
import { createServerAdapter } from '@whatwg-node/server';
import uWS from 'uWebSockets.js';
import { startDisposableServer } from './utils/tserver';
import { serverAudits } from '../audits';
import { serverAudits } from '../src/audits';
import { schema } from './fixtures/simple';

import { createHandler as createHttpHandler } from '../use/http';
import { createHandler as createExpressHandler } from '../use/express';
import { createHandler as createFastifyHandler } from '../use/fastify';
import { createHandler as createFetchHandler } from '../use/fetch';
import { createHandler as createKoaHandler } from '../use/koa';
import { createHandler as createUWSHandler } from '../use/uWebSockets';
import { createHandler as createHttpHandler } from '../src/use/http';
import { createHandler as createExpressHandler } from '../src/use/express';
import { createHandler as createFastifyHandler } from '../src/use/fastify';
import { createHandler as createFetchHandler } from '../src/use/fetch';
import { createHandler as createKoaHandler } from '../src/use/koa';
import { createHandler as createUWSHandler } from '../src/use/uWebSockets';

describe('http', () => {
const [url, , dispose] = startDisposableServer(
Expand Down Expand Up @@ -48,7 +49,7 @@ describe('http', () => {
const res = await fetch(url + '?query={hello}');

await expect(res.text()).resolves.toMatchInlineSnapshot(
`"{"data":{"hello":"world"}}"`,
'"{\\"data\\":{\\"hello\\":\\"world\\"}}"',
);
expect(res.headers.get('x-test')).toBe('test-x');

Expand Down Expand Up @@ -95,7 +96,7 @@ describe('express', () => {
const res = await fetch(url + '?query={hello}');

await expect(res.text()).resolves.toMatchInlineSnapshot(
`"{"data":{"hello":"world"}}"`,
'"{\\"data\\":{\\"hello\\":\\"world\\"}}"',
);
expect(res.headers.get('x-test')).toBe('test-x');

Expand Down Expand Up @@ -154,7 +155,7 @@ describe('fastify', () => {
const res = await fetch(url + '?query={hello}');

await expect(res.text()).resolves.toMatchInlineSnapshot(
`"{"data":{"hello":"world"}}"`,
'"{\\"data\\":{\\"hello\\":\\"world\\"}}"',
);
expect(res.headers.get('x-test')).toBe('test-x');

Expand Down Expand Up @@ -214,7 +215,7 @@ describe('koa', () => {
const res = await fetch(url + '?query={hello}');

await expect(res.text()).resolves.toMatchInlineSnapshot(
`"{"data":{"hello":"world"}}"`,
'"{\\"data\\":{\\"hello\\":\\"world\\"}}"',
);
expect(res.headers.get('x-test')).toBe('test-x');

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/utils/texecute.ts → tests/utils/texecute.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ExecutionResult } from 'graphql';
import { RequestParams } from '../../common';
import { Client } from '../../client';
import { RequestParams } from '../../src/common';
import { Client } from '../../src/client';

export function texecute<D = unknown, E = unknown>(
client: Client,
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/utils/tserver.ts → tests/utils/tserver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, createHandler, HandlerOptions } from '../../handler';
import { afterAll } from 'vitest';
import http from 'http';
import net from 'net';
import { Request, createHandler, HandlerOptions } from '../../src/handler';
import { schema } from '../fixtures/simple';

type Dispose = () => Promise<void>;
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
"outDir": "./lib",
"declaration": false // already built by `tsconfig.esm.json`
},
"include": ["src"],
"exclude": ["src/__tests__"]
"include": ["src"]
}
3 changes: 1 addition & 2 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
"outDir": "./lib",
"declaration": true
},
"include": ["src"],
"exclude": ["src/__tests__"]
"include": ["src"]
}
Loading

0 comments on commit c87b47e

Please sign in to comment.