Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add mocks for packageVersion #811

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions packages/api/test/codegen/languages/typescript/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { SpyInstance } from 'vitest';

import fs from 'node:fs/promises';
import path from 'node:path';

Expand Down Expand Up @@ -70,22 +72,16 @@ function assertSDKFixture(file: string, fixture: string) {
const actual = actualFiles[filename];
const expectedFilePath = path.join(dir, filename);

// We have to wrap in our current package version into the `<<useragent>>` placeholder so
// we don't need to worry about committing package versions into source control or trying
// to mock out our `packageInfo` library, potentially causing sideeffects in other tests.
return fs
.readFile(expectedFilePath, 'utf8')
.then(expected => expected.replace('<<package version>>', packageInfo.PACKAGE_VERSION))
.then(async expected => {
if (actual !== expected && process.env.UPDATE_FIXTURES) {
// eslint-disable-next-line no-console
console.info('[sdk fixture updated]', expectedFilePath);
await fs.writeFile(expectedFilePath, actual.replace(packageInfo.PACKAGE_VERSION, '<<package version>>'));
return;
}

expect(actual).toBe(expected);
});
return fs.readFile(expectedFilePath, 'utf8').then(async expected => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was able to remove this find + replace logic in favor of a mock 🎊

if (actual !== expected && process.env.UPDATE_FIXTURES) {
// eslint-disable-next-line no-console
console.info('[sdk fixture updated]', expectedFilePath);
await fs.writeFile(expectedFilePath, actual);
return;
}

expect(actual).toBe(expected);
});
}),
);

Expand All @@ -96,12 +92,17 @@ function assertSDKFixture(file: string, fixture: string) {
}

describe('typescript', () => {
let packageInfoSpy: SpyInstance;

beforeEach(() => {
// @ts-expect-error deliberately setting this const to another value
packageInfoSpy = vi.spyOn(packageInfo, 'PACKAGE_VERSION', 'get').mockReturnValue('7.0.0-mock');
erunion marked this conversation as resolved.
Show resolved Hide resolved
vi.setSystemTime(new Date('2023-10-25'));
Storage.setStorageDir(uniqueTempDir());
});

afterEach(() => {
packageInfoSpy.mockReset();
vi.useRealTimers();
Storage.reset();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/commands/__snapshots__/list.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ language: js

source: @petstore/v1.0#n6kvf10vakpemvplx

installer version: 7.0.0-beta.3
installer version: 7.0.0-mock

created at: 2023-10-25T00:00:00.000Z"
`;
Expand Down
7 changes: 6 additions & 1 deletion packages/api/test/commands/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { describe, beforeEach, it, expect, vi, afterEach } from 'vitest';

import { SupportedLanguages } from '../../src/codegen/factory.js';
import installCmd from '../../src/commands/list.js';
import * as packageInfo from '../../src/packageInfo.js';
import Storage from '../../src/storage.js';

const baseCommand = ['api', 'list'];
Expand All @@ -18,6 +19,7 @@ describe('install command', () => {
let stdout: string[];
let stderr: string[];
let consoleLogSpy: SpyInstance;
let packageInfoSpy: SpyInstance;

const getCommandOutput = () => {
return [consoleLogSpy.mock.calls.join('\n\n')].filter(Boolean).join('\n\n');
Expand All @@ -34,13 +36,16 @@ describe('install command', () => {
});

consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
// @ts-expect-error deliberately setting this const to another value
packageInfoSpy = vi.spyOn(packageInfo, 'PACKAGE_VERSION', 'get').mockReturnValue('7.0.0-mock');
Storage.setStorageDir(uniqueTempDir());
vi.setSystemTime(new Date('2023-10-25'));
});

afterEach(() => {
consoleLogSpy.mockRestore();
consoleLogSpy.mockReset();
fetchMock.restore();
packageInfoSpy.mockReset();
Storage.reset();
vi.useRealTimers();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/alby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ alby.getMe()

Here's some additional info about the generated SDK:

`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z

--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/alby/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'alby/1.0.14 (api/<<package version>>)');
this.core = new APICore(definition, 'alby/1.0.14 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/metrotransit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ metrotransit.getNextripAgencies()

Here's some additional info about the generated SDK:

`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z

--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/metrotransit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'metrotransit/2 (api/<<package version>>)');
this.core = new APICore(definition, 'metrotransit/2 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/operationid-quirks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ operationidQuirks.quirky_OperationId_string()

Here's some additional info about the generated SDK:

`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z

--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/operationid-quirks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'operationid-quirks/1.0.0 (api/<<package version>>)');
this.core = new APICore(definition, 'operationid-quirks/1.0.0 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/optional-payload/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Add SDK setup information and usage examples here so your users get started in a

Here's some additional info about the generated SDK:

`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z

--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/optional-payload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'optional-payload/1.0.0 (api/<<package version>>)');
this.core = new APICore(definition, 'optional-payload/1.0.0 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ petstore.getInventory()

Here's some additional info about the generated SDK:

`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z

--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/petstore/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'petstore/1.0.0 (api/<<package version>>)');
this.core = new APICore(definition, 'petstore/1.0.0 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/readme/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ readme.getAPISpecification()

Here's some additional info about the generated SDK:

`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z

--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/readme/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'readme/4.355.0 (api/<<package version>>)');
this.core = new APICore(definition, 'readme/4.355.0 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/response-title-quirks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Add SDK setup information and usage examples here so your users get started in a

Here's some additional info about the generated SDK:

`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z

--->
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'response-title-quirks/1.0.0 (api/<<package version>>)');
this.core = new APICore(definition, 'response-title-quirks/1.0.0 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Add SDK setup information and usage examples here so your users get started in a

Here's some additional info about the generated SDK:

`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z

--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/simple/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'simple/1.0.0 (api/<<package version>>)');
this.core = new APICore(definition, 'simple/1.0.0 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/star-trek/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ starTrek.getAnimalSearch()

Here's some additional info about the generated SDK:

`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z

--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/star-trek/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'star-trek/1.0.0 (api/<<package version>>)');
this.core = new APICore(definition, 'star-trek/1.0.0 (api/7.0.0-mock)');
}

/**
Expand Down