Skip to content

Commit

Permalink
feat(test): moving unit tests over to vitest (#691)
Browse files Browse the repository at this point in the history
* feat: moving unit tests over to vitest

* fix: cleaning up eslint configs

* Update packages/httpsnippet-client-api/package.json

Co-authored-by: Kanad Gupta <[email protected]>

* Update packages/api/test/helpers/vitest.matchers.ts

Co-authored-by: Kanad Gupta <[email protected]>

* Update packages/api/package.json

Co-authored-by: Kanad Gupta <[email protected]>

* Update packages/api/.npmignore

Co-authored-by: Kanad Gupta <[email protected]>

* fix: pr feedback

---------

Co-authored-by: Kanad Gupta <[email protected]>
  • Loading branch information
erunion and kanadgupta authored Aug 14, 2023
1 parent 243c413 commit 00bedb0
Show file tree
Hide file tree
Showing 36 changed files with 1,658 additions and 531 deletions.
1,774 changes: 1,458 additions & 316 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"build": "npx lerna run build --stream",
"build-docs": "node bin/build-markdown.js",
"clean": "npx lerna clean",
"lint": "eslint . --ext .js,.ts; npm run lint:types; npm run prettier",
"lint": "npm run lint:types; npm run lint:js; npm run prettier",
"lint:js": "eslint . --ext .js,.ts",
"lint:types": "npm run lint:types --if-present --workspaces",
"prepare": "husky install",
"prettier": "prettier --check .",
Expand All @@ -31,7 +32,7 @@
"devDependencies": {
"@commitlint/cli": "^17.6.6",
"@commitlint/config-conventional": "^17.6.6",
"@readme/eslint-config": "^11.0.1",
"@readme/eslint-config": "^12.0.0",
"alex": "^11.0.0",
"eslint": "^8.44.0",
"husky": "^8.0.3",
Expand Down
18 changes: 1 addition & 17 deletions packages/api/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"extends": ["@readme/eslint-config/docs"],
"globals": {
"fetch": true,
"Response": true
Expand All @@ -8,22 +7,7 @@
"ecmaVersion": 2020
},
"rules": {
// @todo fix these eventually
"@typescript-eslint/no-explicit-any": "off",

// We use `@fixme` in some spots and it's fine.
"jsdoc/check-tag-names": "off",

// Though we aren't requiring JSDoc blocks to be present, if they are they should be properly
// formatted.
"jsdoc/require-jsdoc": "off",
"jsdoc/tag-lines": "off",

// TypeScript types are better suited for these cases instead.
"jsdoc/require-param": "off",
"jsdoc/require-param-type": "off",
"jsdoc/require-returns": "off",
"jsdoc/require-returns-type": "off"
"@typescript-eslint/no-explicit-any": "off" // @todo fix these eventually
},
"overrides": [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/api/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ test/
.eslint*
.gitignore
.prettier*
jest.config.js
vitest*
example.js
21 changes: 0 additions & 21 deletions packages/api/jest.config.js

This file was deleted.

12 changes: 5 additions & 7 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"lint:types": "tsc --noEmit",
"prebuild": "rm -rf dist/; npm run version",
"prepack": "npm run build",
"test": "jest --coverage $(find test -name '*.test.ts' -not -path '*/smoketest.test.ts')",
"test:smoke": "npx jest test/cli/codegen/languages/typescript/smoketest.test.ts",
"test": "vitest --coverage",
"test:smoke": "vitest --config=vitest-smoketest.config.ts ",
"version": "node -p \"'// This file is automatically updated by the build script.\\nexport const PACKAGE_NAME = \\'' + require('./package.json').name + '\\';\\nexport const PACKAGE_VERSION = \\'' + require('./package.json').version + '\\';'\" > src/packageInfo.ts; git add src/packageInfo.ts"
},
"repository": {
Expand Down Expand Up @@ -75,7 +75,6 @@
"@readme/oas-examples": "^5.9.0",
"@types/caseless": "^0.12.2",
"@types/find-cache-dir": "^3.2.1",
"@types/jest": "^29.5.2",
"@types/js-yaml": "^4.0.5",
"@types/lodash.camelcase": "^4.3.7",
"@types/lodash.deburr": "^4.1.7",
Expand All @@ -87,14 +86,13 @@
"@types/semver": "^7.3.13",
"@types/ssri": "^7.1.1",
"@types/validate-npm-package-name": "^4.0.0",
"@vitest/coverage-v8": "^0.34.1",
"fetch-mock": "^9.11.0",
"jest": "^29.6.1",
"jest-extended": "^4.0.0",
"oas-normalize": "^8.3.2",
"ts-jest": "^29.1.1",
"type-fest": "^3.5.4",
"typescript": "^4.9.5",
"unique-temp-dir": "^1.0.0"
"unique-temp-dir": "^1.0.0",
"vitest": "^0.34.1"
},
"prettier": "@readme/eslint-config/prettier"
}
4 changes: 2 additions & 2 deletions packages/api/test/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "@readme/eslint-config/testing",
"extends": "@readme/eslint-config/testing/vitest",
"rules": {
"jest/no-conditional-expect": "off"
"vitest/no-conditional-expect": "off"
}
}
19 changes: 11 additions & 8 deletions packages/api/test/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { OASDocument } from 'oas/dist/rmoas.types';

import fetchMock from 'fetch-mock';
import uniqueTempDir from 'unique-temp-dir';
import { describe, beforeAll, beforeEach, afterEach, it, expect, vi } from 'vitest';

import api from '../src';
import Cache from '../src/cache';
Expand Down Expand Up @@ -31,7 +32,7 @@ describe('#auth()', () => {
fetchMock.restore();
});

describe('API Keys', () => {
describe('API keys', () => {
describe('in: query', () => {
it('should allow you to supply auth', async () => {
fetchMock.get(
Expand Down Expand Up @@ -135,7 +136,7 @@ describe('#auth()', () => {
});

it('should allow multiple calls to share an API key', async () => {
const endpointCall = jest.fn();
const endpointCall = vi.fn();
fetchMock.get(
{
url: 'https://httpbin.org/anything/apiKey',
Expand All @@ -149,7 +150,7 @@ describe('#auth()', () => {

sdk.auth(apiKey);

await sdk.getAnythingApikey().then(() => expect(endpointCall).toHaveBeenCalledOnce());
await sdk.getAnythingApikey().then(() => expect(endpointCall).toHaveBeenCalledTimes(1));
await sdk.getAnythingApikey().then(() => expect(endpointCall).toHaveBeenCalledTimes(2));
});

Expand Down Expand Up @@ -183,16 +184,18 @@ describe('#auth()', () => {
});

describe('quirks', () => {
let authQuirksOas;
let quirks;

beforeAll(async () => {
const authQuirksOas = await loadSpec(require.resolve('./__fixtures__/definitions/auth-quirks.json'));
authQuirksOas = await loadSpec(require.resolve('./__fixtures__/definitions/auth-quirks.json'));
quirks = api(authQuirksOas as unknown as OASDocument);
});

// Because the `POST /anything` operation allows either an OAuth2 token or Basic Auth the
// quirks case we're testing is that you should be able to supply either a single OAuth2 token
// or a username+password and it should be able to intelligently handle both.
// eslint-disable-next-line jest/no-standalone-expect
// Because the `POST /anything` operation allows either an OAuth2 token or Basic Auth the
// quirks case we're testing is that you should be able to supply either a single OAuth2 token
// or a username+password and it should be able to intelligently handle both.
it('should have an expected security setting definition for this quirks case', () => {
expect(authQuirksOas.paths['/anything'].post.security).toStrictEqual([
{ oauth2: ['write:things'] },
{ basicAuth: [] },
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/cache-custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs/promises';
import path from 'path';

import fetchMock from 'fetch-mock';
import { describe, beforeEach, afterEach, it, expect } from 'vitest';

import api from '../src';
import Cache from '../src/cache';
Expand Down
8 changes: 5 additions & 3 deletions packages/api/test/cache-tmp.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os from 'os';

import { describe, it, expect, vi } from 'vitest';

import Cache from '../src/cache';

jest.mock('find-cache-dir', () => {
return () => undefined;
});
vi.mock('find-cache-dir', () => ({
default: () => undefined,
}));

describe('cache (temp dir handling)', () => {
// Since this test is mocking out the `find-cache-dir` module for a single test, it needs to be
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'path';

import fetchMock from 'fetch-mock';
import uniqueTempDir from 'unique-temp-dir';
import { describe, beforeAll, it, expect } from 'vitest';

import Cache from '../src/cache';

Expand Down
4 changes: 2 additions & 2 deletions packages/api/test/cli/codegen/languages/typescript.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "assertSDKFixture"] }] */
import type { TSGeneratorOptions } from '../../../../src/cli/codegen/languages/typescript';

import { promises as fs } from 'fs';
Expand All @@ -7,6 +6,7 @@ import path from 'path';
import fetchMock from 'fetch-mock';
import Oas from 'oas';
import uniqueTempDir from 'unique-temp-dir';
import { describe, beforeEach, afterEach, it, expect, vi } from 'vitest';

import TSGenerator from '../../../../src/cli/codegen/languages/typescript';
import Storage from '../../../../src/cli/storage';
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('typescript', () => {
});

it('should install a `package.json` and the required packages', async () => {
const logger = jest.fn();
const logger = vi.fn();

const file = require.resolve('@readme/oas-examples/3.0/json/petstore.json');
const oas = await loadSpec(file).then(Oas.init);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
import Oas from 'oas';
import OASNormalize from 'oas-normalize';
import { describe, it, expect } from 'vitest';

import TSGenerator from '../../../../../src/cli/codegen/languages/typescript';
import realWorldAPIs from '../../../../datasets/real-world-apis.json';
Expand Down Expand Up @@ -54,6 +55,7 @@ if (args.chunks && args.chunk) {
}

describe('typescript smoketest', () => {
// eslint-disable-next-line vitest/require-hook
dataset.forEach(({ name, url }) => {
// The test timeout is huge on this because CI can be slow as some API definitions are huge and
// can take a while to download + codegen.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it, expect } from 'vitest';

import { docblockEscape, generateTypeName, wordWrap } from '../../../../../src/cli/codegen/languages/typescript/util';

describe('ts codegen utils', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/cli/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import path from 'path';
import fetchMock from 'fetch-mock';
import 'isomorphic-fetch';
import uniqueTempDir from 'unique-temp-dir';
import { describe, beforeAll, beforeEach, afterEach, it, expect } from 'vitest';

import Storage from '../../src/cli/storage';
import { PACKAGE_VERSION } from '../../src/packageInfo';
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { OASDocument } from 'oas/dist/rmoas.types';

import fetchMock from 'fetch-mock';
import uniqueTempDir from 'unique-temp-dir';
import { describe, beforeAll, beforeEach, afterEach, it, expect } from 'vitest';

import api from '../src';
import Cache from '../src/cache';
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/core/getJSONSchemaDefaults.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Oas from 'oas';
import { describe, it, expect } from 'vitest';

import getJSONSchemaDefaults from '../../src/core/getJSONSchemaDefaults';
import loadSpec from '../helpers/load-spec';
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/core/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'assert';

import fetchMock from 'fetch-mock';
import Oas from 'oas';
import { describe, beforeEach, afterEach, it, expect } from 'vitest';

import APICore from '../../src/core';
import FetchError from '../../src/core/errors/fetchError';
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/core/parseResponse.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'isomorphic-fetch';
import { describe, beforeEach, it, expect } from 'vitest';

import parseResponse from '../../src/core/parseResponse';

Expand Down
1 change: 1 addition & 0 deletions packages/api/test/core/prepareAuth.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { OASDocument } from 'oas/dist/rmoas.types';

import Oas from 'oas';
import { describe, beforeAll, it, expect } from 'vitest';

import prepareAuth from '../../src/core/prepareAuth';
import loadSpec from '../helpers/load-spec';
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/core/prepareParams.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';

import Oas from 'oas';
import { describe, beforeEach, it, expect } from 'vitest';

import prepareParams from '../../src/core/prepareParams';
import payloadExamples from '../__fixtures__/definitions/payloads.json';
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/dist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { OASDocument } from 'oas/dist/rmoas.types';

import fetchMock from 'fetch-mock';
import uniqueTempDir from 'unique-temp-dir';
import { describe, beforeAll, afterEach, it, expect } from 'vitest';

import api from '../dist';
import Cache from '../src/cache';
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'assert';
import fs from 'fs/promises';

import fetchMock from 'fetch-mock';
import { describe, beforeAll, it, expect } from 'vitest';

import Fetcher from '../src/fetcher';

Expand Down
4 changes: 1 addition & 3 deletions packages/api/test/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import 'jest-extended';

import './helpers/jest.matchers';
import './helpers/vitest.matchers';
Loading

0 comments on commit 00bedb0

Please sign in to comment.